Why malloc() when target code is executed?

Andrew Cagney ac131313@redhat.com
Mon Aug 25 22:08:00 GMT 2003


> On Mon, Aug 25, 2003 at 11:25:31AM -0700, Kevin Buettner wrote:
> 
>> On Aug 22, 10:48pm, Josef Wolf wrote:
>> 
> 
>> > I just noticed that ``print printf("Hello\n")'' call malloc() on the
>> > target to allocate the memory for the string. AFAICS, this memory
>> > never gets freed. Is there any reason not to allocate this memory
>> > on the stack? This would avoid this memory leak. In addition, this
>> > would make it possible to use this feature on embedded systems which
>> > often have either restricted memory or even dont have malloc() at all.
> 
>> 
>> For printf(), allocating the string on the stack is (usually) okay. 
>> This is because printf() doesn't return a pointer to the string nor
>> does it write the string pointer to some data structure in the
>> inferior process.  Functions which did either of these could/would end
>> up with a dangling pointer if the string were to be allocated on the
>> stack.
> 
> 
> Ahhh, I see there is good reason for current behavior. Had not thought
> about this one. Thanks for clarifying this.

The doco should probably mention this.  What to file a bug report (or a 
patch?).

Andrew




More information about the Gdb mailing list