Subj : Re: malloc() To : borland.public.cpp.borlandcpp From : Jan Vernimmen Date : Thu Jun 24 2004 12:45 pm Hi Wayne, txtBuf is initialized as follows: txtBuf = (char*)malloc( (size_t)LEN_MALLOCMINIM ); // LEN_MALLOCMINIM = 12 txtBuf[0] = '\0'; Jan. Wayne A. King wrote: > To restate the issue: > > Your conditional statement > >>if ( strlen( szInput ) > strlen( txtBuf )) > > presupposes that txtBuf actually *contains* a nul-terminated string. > > My question was/is: > > What is the buffer's state on the *first* iteration of this loop? > How was it initialized? You *cannot* use strlen() to determine > the size of the *buffer*. It can only determine the length of any > string *in* the buffer. Memory allocated with malloc is uninitialized. > Its contents will be random - whatever happens to be in those > memory locations at the time. Therefore, you *cannot* use > strlen on a buffer immediately after malloc without first setting > at least one of the bytes within the buffer to nul (binary zero in > this case). > .