Scoping question in C

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Fri Jun 10 17:06:16 UTC 2005


On Fri, Jun 10, 2005 at 11:17:33AM -0400, Kevin Cozens wrote:
> William Park wrote:
> 
> >I did try it, and got what Henry and others said.  But, I just wanted
> >to make sure that data access is legal.  Because, sometimes, I could
> >access data pointed to by a dangling pointer for a while, before I
> >get segfault.
> 
> You will be in serious danger of getting a seg fault if 'char *x' was
> only declared inside func() and returned the pointer and you then
> tried to use this pointer outside of func().

But, if the static string lives for the duration of the program, then
pointer to it should always be valid, no?  Eg.

    char *func2()
    {
	char *y;

	y = "something inside";
	return (y);
    }

Here, 'y' (or, what the function returns) should be valid for entire
program.

-- 
William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
	   http://home.eol.ca/~parkw/thinflash.html
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list