Recently, a small problem in a program design, and finally solved it with an imperfect way, and it may be more interesting:
In writing my program (Flashime, http://www.d2ksoft.com, there is a class that remembers the nearest input to the current editor "string (old string).
When I entered a new string next time, I must first compare whether the content in the clipboard is the same before entering a new string. If so, there are some operations, if not, there are other operations (the details of the operation are not critical). Then I entered the new string, and then update the contents of the old string with the contents of the new string.
I use a CManageStr class to store old strings, starting this class cmanageStr is designed like this: Class CmanageStr {
Public: cmanagest (): _ buf (0) {} ~ cmanageStr () {free (_buf); _buf = 0;} BOOL Compare_with_clip (); update_from_new (const char * infut); {free (_buf); _buf = 0; _BUF = malloc (Strlen 1); strcpy (_buf, input);} private: char * _buf;
} Because because you can't know how big the recently entered strings, the beginning of the idea is to store the old string with dynamic allocation of memory.
However, due to the development of the procedure of the input method, the CManageStr type object will be shared by multiple processes. The pointer clearly cannot be shared by multiple processes. This cannot use a data member of a pointer type such as _buf.
Carefully consider why you want to store the most recently entered string? In fact, the purpose is only to compare the string in the clipboard, and the results of the comparison only need to know if the two strings are the same. If you can relax the definition of "the same", you can redesign the data of CManagesTR to make the CManagestr object can be shared by multiple processes.
For example, add an ASCII code of all characters in the string to check Sum. To compare whether the two strings are equal, as long as they compare whether their Checksum is equal. I can also go to the previous 128 bit of each string to get a child string. Then compare these two substrings. In my program, I combined with these two ways. Of course, this can only guarantee that there is a lot of probability to get the results I want. But considering the specific application environment of our program, a big probability is correct (rather than absolutely correct) is also acceptable.
Class CmanageStr {
public: CManageStr (): _ latest_checksum (0) {} ~ CManageStr () {} bool compare_with_clip (); update_from_new (const char * input); {sample (input, _latest_prefix, _latest_checksum);} private: void sample (const char * Input, Char * Prefix, Unsigned long & checksum) {...} private: char _latest_prefix [128]; unsigned long_late_checksum;}