Hi,
On Jan 6, 2008 1:40 AM, Thorsten Haude <[EMAIL PROTECTED]> wrote:
> Hi,
>
> 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.
'length * sizeof(char *)' would give you 'length' pointer to char.
> - 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.
> - 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.
Bert
>
>
> Thorsten Tegan and Sara: I know I know I know
--
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop