Re: using local buffers.

From: Mark Devlin (markd@eskimo.com)
Date: 09/14/95


On Thu, 14 Sep 1995, Ron Poulton wrote:

> Btw, I know I can 'return "string"' in order to return a string from a 
> char* function, but if I have a local buffer, how would I return a string 
> in the same manner without returning the address of the local buffer?

The problem is that the address of a local variable goes out of scope as 
soon as the function/block is exited, so you need to return pointer to a 
variable that won't go out of scope when this happens.  The only way to 
do this is to copy the data to higher-level variable (usually a global 
variable), or copy it to a malloc()'d chunk of memory.  You'd then return 
the address of either of those (though it's not really necessary with a 
global variable, it doesn't hurt and it can make things easier to 'fix' 
if you later decide the global variable is a bad idea).

Hope this helps.
 
- Mark
markd@eskimo.com



This archive was generated by hypermail 2b30 : 12/07/00 PST