By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,545 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,545 IT Pros & Developers. It's quick & easy.

returning address of a local variable

P: n/a
Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn't that a
unrecommended procedure? Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it's not safer to do such an operation, how do functions like
strtok for example work???

--
Spitfire

Nov 23 '06 #1
Share this Question
Share on Google+
14 Replies

P: n/a
Spitfire a écrit :
Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn't that a
unrecommended procedure? Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it's not safer to do such an operation, how do functions like
strtok for example work???

--
Spitfire
You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.

strtok use a static variable, not a local variable with automatic
storage class.
Nov 23 '06 #2

P: n/a
Spitfire wrote:
I've this weird question about pointers. I would like to know how to
return the address of a local variable,
void* get_local_address ()
{
int i;
return &i;
}

safely!!
it's perfectly (well almost) safe as long as you don't use it.
Isn't that a unrecommended procedure?
I just don't understand why you want to do this

Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
so don't dereference it?

If it's not safer to do such an operation, how do functions like
strtok for example work???
now I'm confused. Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.

--
Nick Keighley

Nov 23 '06 #3

P: n/a
Spitfire said:
Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!!
int *foo(void)
{
static int i;
return &i;
}
Isn't that a unrecommended procedure?
It can be. That depends on what you're doing and how you do it.
Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
Not if you're careful (see above).
If it's not safer to do such an operation, how do functions like
strtok for example work???
Carefully. For one thing, they don't return the addresses of local
variables.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #4

P: n/a
On Nov 23, 3:06 am, jacob navia <j...@jacob.remcomp.frwrote:
>You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.

strtok use a static variable, not a local variable with automatic
storage class.
hmmm... that pretty much explains everything...

Nov 23 '06 #5

P: n/a
"Nick Keighley" <ni******************@hotmail.comwrites:
Spitfire wrote:
> I've this weird question about pointers. I would like to know how to
return the address of a local variable,

void* get_local_address ()
{
int i;
return &i;
}

>safely!!

it's perfectly (well almost) safe as long as you don't use it.
And completely useless.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '06 #6

P: n/a
On Nov 23, 3:11 am, "Nick Keighley" <nick_keighley_nos...@hotmail.com>
wrote:
safely!!it's perfectly (well almost) safe as long as you don't use it.
Can't understand your point here!?
Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.
I can understand that it modifies the string that I pass to it.
Nevertheless, if you observe the function "returns" the token after the
current call. Understood what I'm referring to? I somehow got that I'm
overlooking something trivial... however, couldn't catch what that was!

The reply from Jacob highlights what I'd overlooked.

Nov 23 '06 #7

P: n/a
Hi,

On Nov 23, 3:56 am, Richard Heathfield <r...@see.sig.invalidwrote:
return the address of a local variable, safely!!
int *foo(void)
{
static int i;
return &i;

}
that's something I overlooked :)
>Carefully. For one thing, they don't return the addresses of local
variables.
the point is just to extend the lifetime, to prevent invalid references
right?

Nov 23 '06 #8

P: n/a

On Nov 23, 4:05 am, Keith Thompson <k...@mib.orgwrote:
it's perfectly (well almost) safe as long as you don't use it.
And completely useless.
the reason being?

--
Spitfire

Nov 23 '06 #9

P: n/a
Spitfire said:
Hi,

On Nov 23, 3:56 am, Richard Heathfield <r...@see.sig.invalidwrote:
>return the address of a local variable, safely!!
int *foo(void)
{
static int i;
return &i;

}
that's something I overlooked :)
>>Carefully. For one thing, they don't return the addresses of local
variables.
the point is just to extend the lifetime, to prevent invalid references
right?
Yes, this is about lifetime, not scope. Basically, if the object *exists*,
it's okay to know where it lives. But if it doesn't exist, knowing where it
lives is more information than you can use.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #10

P: n/a
Spitfire said:
>
On Nov 23, 4:05 am, Keith Thompson <k...@mib.orgwrote:
it's perfectly (well almost) safe as long as you don't use it.
And completely useless.

the reason being?
If the object doesn't exist, where's the percentage in knowing where it used
to live?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #11

P: n/a

On Nov 23, 4:20 am, Richard Heathfield <r...@see.sig.invalidwrote:
Yes, this is about lifetime, not scope. Basically, if the object *exists*,
it's okay to know where it lives. But if it doesn't exist, knowing where it
lives is more information than you can use.
thanks for the info

--
Spitfire

Nov 23 '06 #12

P: n/a
Spitfire wrote:
On Nov 23, 3:06 am, jacob navia <j...@jacob.remcomp.frwrote:
You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.

strtok use a static variable, not a local variable with automatic
storage class.

hmmm... that pretty much explains everything...
no, not really. strtok() doesn't return a pointer to the static
variable
but a pointer to the argument string. The use of a static variable
is irrelevent.

char *tail (char* s)
{
return &s[1];
}

--
Nick Keighley

Nov 23 '06 #13

P: n/a
In article <x9******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
....
>Yes, this is about lifetime, not scope. Basically, if the object *exists*,
it's okay to know where it lives. But if it doesn't exist, knowing where it
lives is more information than you can use.
ITYM, "lived".

Nov 23 '06 #14

P: n/a
Groovy hepcat Spitfire was jivin' on 23 Nov 2006 02:07:22 -0800 in
comp.lang.c.
Re: returning address of a local variable's a cool scene! Dig it!
>On Nov 23, 3:11 am, "Nick Keighley" <nick_keighley_nos...@hotmail.com>
wrote:
>safely!!it's perfectly (well almost) safe as long as you don't use it.

Can't understand your point here!?
>Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.

I can understand that it modifies the string that I pass to it.
Nevertheless, if you observe the function "returns" the token after the
current call.
It returns a pointer to the first/next token in the string. However,
the string, and therefore the token, is in the buffer whose address is
passed to strtok().
Understood what I'm referring to?
Not really. Maybe you're refering to the address (of the next token)
stored in a static variable used by strtok()? In that case it's
strtok()'s internal pointer itself that's static, not the thing it
points at.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 26 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.