[OCLUG-devel] malloc() woes

James Colannino james at colannino.org
Sun May 29 14:09:11 PDT 2005


Christopher Smith wrote:

> James Colannino wrote:
>
>> char * more_memory(char *buffer);
>>
>> You pass the function the old pointer and it returns a new pointer.
>>  
>>
> What does it do with the old memory, and how does it decide how much
> new memory it needs to allocate?


First it attempts to allocate new memory and store the pointer in a
temporary variable, buffer2.  If buffer2 isn't NULL, then the function
returns it.  If it is NULL, then the function returns the old pointer.

I modified the original program to declare buffer as a char * and use
malloc to determine its space, but basically, I start off with an
arbitrary size of 80 characters, and if it isn't enough to store the
entire line, more_memory() runs and doubles the space, afterwhich, the
program tries to read the same string into the buffer again.

James

-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment



More information about the OCLUG-devel mailing list