str properties (data members)

str variables have the following data members:

lpstr - pointer to string buffer.

len - length of string, not including the terminating null character.

nc - number of allocated bytes, not including the terminating null character (actually str allocates nc+3 bytes).

flags - optimization flags:

1 str functions should never free extra bytes. However some functions may ignore it, and the reason in most cases is assigning a null string.
2 str functions should allocate more extra bytes to avoid frequent reallocations.

 

 

Remarks

When a str variable is just declared without initializing, all data members are 0.

 

When you want to know string length, you should use its len property instead of the len function.

 

When various operations are performed with a str variable, it's data members are managed automatically. You should not modify them manually. There are two situations when you can modify them:

 

1. You can modify flags to improve performance when multiple operations are performed on a str variable.

2. If you want to steal string buffer from a str variable, set lpstr to 0. To free the stealed memory, use q_free.

 

In C++, str definition woul look like:

 

class str
{
public:
	LPSTR lpstr;
	int len, nc;
	BYTE flags;
//member functions
...
};

 

In QM, str definition woul look like:

 

class str lpstr'lpstr len nc byte'flags

 

Example

str s = "string"
int i = s.len
 now i is 6
s.flags = 1
 now s buffer can grow, but not shrink