stack & passing variables

Discussion in 'C Programming' started by Mark, Oct 17, 2011.

  1. Mark

    Mark Guest

    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
    Mark, Oct 17, 2011
    #1
    1. Advertising

  2. Mark

    James Kuyper Guest

    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
    James Kuyper, Oct 17, 2011
    #2
    1. Advertising

  3. "Mark" <> 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.
    Ben Bacarisse, Oct 17, 2011
    #3
  4. Mark

    Nobody Guest

    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.
    Nobody, Oct 17, 2011
    #4
  5. Nobody <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 17, 2011
    #5
  6. On Oct 17, 3:22 am, "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,


    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
    Nick Keighley, Oct 18, 2011
    #6
  7. 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.
    88888 Dihedral, Oct 21, 2011
    #7
  8. 88888 Dihedral <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 21, 2011
    #8
  9. In article <>,
    Keith Thompson <> wrote:
    >88888 Dihedral <> 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.
    Kenny McCormack, Oct 21, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,195
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,082
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,285
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    224
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    259
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page