stack & passing variables

M

Mark

Hello,

Consider the following simple code.

#include <stdio.h>
static void p2(int *pp)
{
printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);
}

static int p1(void)
{
int p=10;
printf("p1: p=%d @ %p\n", p, (void *)&p);
p2(&p);

return 0;
}

int main(void)
{
p1();

return 0;
}

Is it guaranteed that object 'p' defined in function p1 will have the same
value and will be at the same address location, when it is used from
function p2 ? I know that the C standard doesn't operate such terms as
stack or heap, but as far as I understand most of the time automatic
variables (automatic -- local?) will be placed on the stack, and my question
is - is it safe to pass parameters this way? (I do not intend to *store*
anything at the locations of these parameters, only pass information and use
it once).

Thanks in advance.

Mark
 
J

James Kuyper

Hello,

Consider the following simple code.

#include <stdio.h>
static void p2(int *pp)
{
printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);
}

static int p1(void)
{
int p=10;
printf("p1: p=%d @ %p\n", p, (void *)&p);
p2(&p);

return 0;
}

int main(void)
{
p1();

return 0;
}

Is it guaranteed that object 'p' defined in function p1 will have the same
value and will be at the same address location, when it is used from
function p2 ? I know that the C standard doesn't operate such terms as
stack or heap,

You're right - the standard doesn't talk about these issues in terms of
the stack or the heap. That's an implementation detail. The key point
is, that no matter what method the the implementation uses to set aside
storage for 'p', that storage much remain accessible throughout the
lifetime of 'p'. The lifetime of the 'p' starts when the body of p1 is
entered. It does not end until after the the return statement at the end
of p1. The pointer value that is the result of the &p expression will
remain valid throughout that lifetime. Since the call to p2 starts and
finishes during the call to p1(), the object p is still alive throughout
that call, so the pointer remains valid, and can be used in the fashion
indicated.
 
B

Ben Bacarisse

Mark said:
Consider the following simple code.

#include <stdio.h>
static void p2(int *pp)
{
printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);
}

static int p1(void)
{
int p=10;
printf("p1: p=%d @ %p\n", p, (void *)&p);
p2(&p);

return 0;
}

int main(void)
{
p1();

return 0;
}

Is it guaranteed that object 'p' defined in function p1 will have the same
value and will be at the same address location, when it is used from
function p2 ?

Just to add a bit more to what has been said... Section 6.2.4 paragraph
2 says (in part):

"An object exists, has a constant address, and retains its last-stored
value throughout its lifetime."

and you've already had an answer about the lifetime of 'p' which does
indeed span the whole execution of 'p2'.

<snip>
 
N

Nobody

static void p2(int *pp)
{
printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);
}

static int p1(void)
{
int p=10;
printf("p1: p=%d @ %p\n", p, (void *)&p);
p2(&p);
Is it guaranteed that object 'p' defined in function p1 will have the
same value and will be at the same address location, when it is used
from function p2 ?

pp will compare equal to &p, and the two will reference the same memory
location. However, the two aren't guaranteed to have the same
representation and aren't guaranteed to produce the same results when
printed with "%p"; there may be multiple ways to represent a given
pointer, and the implementation is free to convert between them.
I know that the C standard doesn't operate such
terms as stack or heap, but as far as I understand most of the time
automatic variables (automatic -- local?) will be placed on the stack,
and my question is - is it safe to pass parameters this way? (I do not
intend to *store* anything at the locations of these parameters, only
pass information and use it once).

Yes, it is safe. And common. Passing a pointer to a variable is the only
way to have the function modify the variable (C doesn't have Pascal's
"var" parameters). Also, passing a pointer is likely to be more efficient
if the object is large.
 
K

Keith Thompson

Nobody said:
pp will compare equal to &p, and the two will reference the same memory
location. However, the two aren't guaranteed to have the same
representation and aren't guaranteed to produce the same results when
printed with "%p"; there may be multiple ways to represent a given
pointer, and the implementation is free to convert between them.

True, but unlikely to be an issue in practice. An implementation would
probably have to deliberately go out of its way to use different
representations for the pointer in these two contexts.
 
N

Nick Keighley

Hello,

Consider the following simple code.

#include <stdio.h>
static void p2(int *pp)
{
    printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);

}

static int p1(void)
{
    int p=10;
    printf("p1: p=%d @ %p\n", p, (void *)&p);
    p2(&p);

    return 0;

}

int main(void)
{
    p1();

    return 0;

}

Is it guaranteed that object 'p' defined in function p1 will have the same
value and will be at the same address location, when it is used from
function p2 ?  I know that the C standard doesn't operate such terms as
stack or heap,

but it does have such terms as "automatic" and "dynamic" storage
(assuming I spelt them right!)


but as far as I understand most of the time automatic
 
8

88888 Dihedral

Hello,

Consider the following simple code.

#include <stdio.h>
static void p2(int *pp)
{
printf("%s: pp=%d @ %p\n", "p2", *pp, (void *)pp);
}

static int p1(void)
{
int p=10;
printf("p1: p=%d @ %p\n", p, (void *)&p);
p2(&p);

return 0;
}

int main(void)
{
p1();

return 0;
}

Is it guaranteed that object 'p' defined in function p1 will have the same
value and will be at the same address location, when it is used from
function p2 ? I know that the C standard doesn't operate such terms as
stack or heap, but as far as I understand most of the time automatic
variables (automatic -- local?) will be placed on the stack, and my question
is - is it safe to pass parameters this way? (I do not intend to *store*
anything at the locations of these parameters, only pass information and use
it once).

Thanks in advance.

Mark
 
K

Keith Thompson

88888 Dihedral said:
Hello,

Consider the following simple code.
[30 lines deleted]

Thanks in advance.

Mark

Most of your followup was an *unmarked* quotation of the entire parent
article. Quoted text is normally marked with a leading "> " on each
line; even the lousy Google Groups interface should do this for you.

And your answer doesn't seem to have anything to do with the question.
 
K

Kenny McCormack

88888 Dihedral said:
Hello,

Consider the following simple code.
[30 lines deleted]

Thanks in advance.

Mark
in one's program in C to gain speed, because one knows how the program
will consume memory than the OS subsystem that does not know.

Most of your followup was an *unmarked* quotation of the entire parent
article. Quoted text is normally marked with a leading "> " on each
line; even the lousy Google Groups interface should do this for you.

And your answer doesn't seem to have anything to do with the question.

Just like yours.

(And mine, too)

--
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top