Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   stack & passing variables (http://www.velocityreviews.com/forums/t754954-stack-and-passing-variables.html)

Mark 10-17-2011 02:22 AM

stack & passing variables
 
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



James Kuyper 10-17-2011 03:51 AM

Re: stack & passing variables
 
On 10/16/2011 10:22 PM, Mark wrote:
> 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.
--
James Kuyper

Ben Bacarisse 10-17-2011 10:43 AM

Re: stack & passing variables
 
"Mark" <mark_cruzNOTFORSPAM@hotmail.com> writes:

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

Nobody 10-17-2011 12:24 PM

Re: stack & passing variables
 
On Sun, 16 Oct 2011 22:22:04 -0400, Mark wrote:

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


Keith Thompson 10-17-2011 07:15 PM

Re: stack & passing variables
 
Nobody <nobody@nowhere.com> writes:
> On Sun, 16 Oct 2011 22:22:04 -0400, Mark wrote:
>
>> 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.


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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Nick Keighley 10-18-2011 09:14 AM

Re: stack & passing variables
 
On Oct 17, 3:22*am, "Mark" <mark_cruzNOTFORS...@hotmail.com> wrote:
> 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
> 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



88888 Dihedral 10-21-2011 07:59 AM

Re: stack & passing variables
 
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
------------------------------------------------------------------

OK, one can get one's own stacks or even a memory management subsystem 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.

Keith Thompson 10-21-2011 08:49 AM

Re: stack & passing variables
 
88888 Dihedral <dihedral88888@googlemail.com> writes:
> Hello,
>
> Consider the following simple code.
>

[30 lines deleted]
>
> Thanks in advance.
>
> Mark
> ------------------------------------------------------------------
>
> OK, one can get one's own stacks or even a memory management subsystem 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Kenny McCormack 10-21-2011 12:25 PM

Re: stack & passing variables
 
In article <lnbotaohjd.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
>88888 Dihedral <dihedral88888@googlemail.com> writes:
>> Hello,
>>
>> Consider the following simple code.
>>

>[30 lines deleted]
>>
>> Thanks in advance.
>>
>> Mark
>> ------------------------------------------------------------------
>>
>> OK, one can get one's own stacks or even a memory management subsystem

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



All times are GMT. The time now is 10:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.