Hi,

* Bert Wesarg wrote (2008-01-06 01:55):
>On Jan 6, 2008 1:40 AM, Thorsten Haude <[EMAIL PROTECTED]> wrote:
>> I just stumbled over this function in source/interpret.c:
>>
>> /* Allocate a new string buffer of length chars */
>> char *AllocString(int length)
>> {
>>     char *mem;
>>
>>     mem = XtMalloc(length + sizeof(char *) + 1);
>>     *((char **)mem) = AllocatedStrings;
>>     AllocatedStrings = mem;
>> #ifdef TRACK_GARBAGE_LEAKS
>>     ++numAllocatedStrings;
>> #endif
>>     return mem + sizeof(char *) + 1;
>> }
>>
>> I have a number of questions about and/or issues with this functions.
>>
>> - XtMalloc is never tested to be successful. It does not return NULL
>>   on errors, but calls XtErrorMsg(), which simply exits NEdit. We
>>   have more than 500 calls to XtMalloc; should we cover this by an
>>   error handler?
>> - It mallocs (length + sizeof(char *) + 1), which looks like a typo.
>>   Should this really be (length * sizeof(char *) + 1)?
>No. You want 'length' bytes for the string + 1 '\0' and one pointer to
>char, to chain all allocated strings together.

Actually I would want ((length + 1) * sizeof(char)) + sizeof(char*).


>'length * sizeof(char *)' would give you 'length' pointer to char.

Sure, I didn't take the falg into account.


>> - Also, it returns (mem + sizeof(char *) + 1), which is (char* +
>>   size_t + int). What is this supposed to bring about?
>We need to skip the pointer to char for the chaining. The + 1 is unknown to me.
>It may be the reference count which is skipped too.

So I guess is something is put there later. Should it be
sizeof(size_t) instead of 1?


>> - Lastly, it promises in the comment:
>>
>>     Length does not include the terminating null ** character, so to
>>     allocate space for a string of strlen == n, you must ** use
>>     AllocString(n+1).
>>
>>   So what is the + 1 about?
>With the reference count in mind, than this comment is right.

Above you say "string + 1 '\0'".


Thorsten                                             Bexar Bexar: Winter 99
-- 
Getting a thrill out of some stupid quote is a sign of idiocy.
    - turmeric

Attachment: pgphNr54WOvZe.pgp
Description: PGP signature

-- 
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop

Reply via email to