returning address of a local variable

S

Spitfire

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???
 
J

jacob navia

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???

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.
 
N

Nick Keighley

Spitfire said:
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;
}


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.
 
R

Richard Heathfield

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.
 
S

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.

hmmm... that pretty much explains everything...
 
K

Keith Thompson

Nick Keighley said:
void* get_local_address ()
{
int i;
return &i;
}



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

And completely useless.
 
S

Spitfire

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.
 
S

Spitfire

Hi,

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?
 
R

Richard Heathfield

Spitfire said:
Hi,


that's something I overlooked :)

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.
 
S

Spitfire

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
 
N

Nick Keighley

Spitfire said:
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];
}
 
K

Kenny McCormack

Richard Heathfield said:
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".
 
P

Peter Shaggy Haywood

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!
Can't understand your point here!?


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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top