Very wierd problem

Discussion in 'C Programming' started by Johs32, Mar 14, 2006.

  1. Johs32

    Johs32 Guest

    I have the follwing code:

    #include <stdio.h>
    #include <stdlib.h>

    struct data {
    int *ip;
    };


    struct data first;
    struct data *current = &first;


    int init()
    {
    int a = 222;
    current->ip = &a;
    printf("init :%d\n", *current->ip);
    return 0;
    }
    void zap(int a, int b)
    {
    printf("current->prio: %d\n",*current->ip);

    }

    int main()
    {
    init();
    zap(3,5);
    return 0;

    }

    When I do the printf in init() I get 222 as expected. But when I do it in
    zap(int a, int b) I get 5!! If I remove the arguments from zap like this:

    zap()

    and call it like this from main:

    zap()

    I get 222 printed as expected from the zap() function.

    What kind of black magic is this?

    JOhs
     
    Johs32, Mar 14, 2006
    #1
    1. Advertising

  2. Johs32

    Eric Sosman Guest

    Johs32 wrote On 03/14/06 15:02,:
    > I have the follwing code:
    > [snipped; see up-thread]
    >
    > What kind of black magic is this?


    The consequence of defective spells. If you haven't
    been told already, there's a Frequently Asked Questions
    (FAQ) site for comp.lang.c at

    http://www.c-faq.com/

    .... which you should visit, because the problem you're
    experiencing is covered in Question 7.5a. (If you've
    already been told to read the FAQ and you haven't done
    so, shame on you!)

    By the way, your problem is "weird" or possibly
    "wired," but not "wierd."

    --
     
    Eric Sosman, Mar 14, 2006
    #2
    1. Advertising

  3. Johs32

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Johs32 wrote:
    > I have the follwing code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct data {
    > int *ip;
    > };
    >
    >
    > struct data first;
    > struct data *current = &first;
    >
    >
    > int init()
    > {
    > int a = 222;
    > current->ip = &a;
    > printf("init :%d\n", *current->ip);
    > return 0;
    > }
    > void zap(int a, int b)
    > {
    > printf("current->prio: %d\n",*current->ip);
    >
    > }
    >
    > int main()
    > {
    > init();
    > zap(3,5);
    > return 0;
    >
    > }
    >
    > When I do the printf in init() I get 222 as expected. But when I do it in
    > zap(int a, int b) I get 5!!


    - From reviewing your code, I can see why.
    [snip]

    > What kind of black magic is this?


    There is no "black magic" involved. You just invoked "undefined
    behaviour" by doing something incorrect in your code, that's all.

    Tell me, at the printf() statement in init(), what does *current->ip
    point to? What does it point to at the printf() statement in zap()? Is
    there anything special about the object that *current->ip points to at
    the point you get this odd behaviour?

    Here's a hint: Unless you specify that the variable is "static",
    variables defined within a function are only guaranteed to be available
    /within/ the function that they were defined in, and they only last
    until the end of the function. After that, any reference to them invokes
    "undefined behaviour".

    HTH
    - --

    Lew Pitcher, IT Specialist, Corporate Technology Solutions,
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD4DBQFEFyZgagVFX4UWr64RArITAJ9wGtlihwpr7Bif8sOBF00/RvcfsQCYwWnd
    b0lqTaSCbJvuiozW1KJnkw==
    =5WY1
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Mar 14, 2006
    #3
  4. On Tuesday 14 March 2006 20:02, Johs32 opined (in
    <dv77hh$hc6$-c.dk>):

    > I have the follwing code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct data {
    > int *ip;
    > };
    >
    >
    > struct data first;
    > struct data *current = &first;
    >
    >
    > int init()
    > {
    > int a = 222;
    > current->ip = &a;
    > printf("init :%d\n", *current->ip);
    > return 0;
    > }
    > void zap(int a, int b)
    > {
    > printf("current->prio: %d\n",*current->ip);
    >
    > }
    >
    > int main()
    > {
    > init();
    > zap(3,5);
    > return 0;
    >
    > }
    >
    > When I do the printf in init() I get 222 as expected. But when I do it
    > in zap(int a, int b) I get 5!! If I remove the arguments from zap like
    > this:
    >
    > zap()
    >
    > and call it like this from main:
    >
    > zap()
    >
    > I get 222 printed as expected from the zap() function.
    >
    > What kind of black magic is this?


    No black magic, just poor programming (or poor understanding thereof).

    In `init` you assign an address of a local variable to your pointer.
    This local variable no longer exists once you exit `init`. Therefore,
    dereferencing the pointer in `zap` reads memory that does not even
    necessarily exist (as far as C Standard is concerned).

    The difference you observe with or without parameters to `zap` is due to
    the fact that your implementation seems to be using the stack, and the
    same stack area for both functions. So, if you have no parameters it
    does not bother changing the memory once occupied by `a` in `init`, but
    with parameters to `zap`, one of them gets the honour of occupying the
    same spot.

    I suggest you go back and study local variables in C.

    --
    BR, Vladimir

    Did YOU find a DIGITAL WATCH in YOUR box of VELVEETA?
     
    Vladimir S. Oka, Mar 14, 2006
    #4
  5. Johs32

    Johs32 Guest

    Lew Pitcher wrote:

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Johs32 wrote:
    >> I have the follwing code:
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> struct data {
    >> int *ip;
    >> };
    >>
    >>
    >> struct data first;
    >> struct data *current = &first;
    >>
    >>
    >> int init()
    >> {
    >> int a = 222;
    >> current->ip = &a;
    >> printf("init :%d\n", *current->ip);
    >> return 0;
    >> }
    >> void zap(int a, int b)
    >> {
    >> printf("current->prio: %d\n",*current->ip);
    >>
    >> }
    >>
    >> int main()
    >> {
    >> init();
    >> zap(3,5);
    >> return 0;
    >>
    >> }
    >>
    >> When I do the printf in init() I get 222 as expected. But when I do it in
    >> zap(int a, int b) I get 5!!

    >
    > - From reviewing your code, I can see why.
    > [snip]
    >
    >> What kind of black magic is this?

    >
    > There is no "black magic" involved. You just invoked "undefined
    > behaviour" by doing something incorrect in your code, that's all.
    >
    > Tell me, at the printf() statement in init(), what does *current->ip
    > point to? What does it point to at the printf() statement in zap()? Is
    > there anything special about the object that *current->ip points to at
    > the point you get this odd behaviour?


    I see your point. I have now moved "a" outside of the function:


    int a = 222;
    struct data first;
    struct data *current = &first;

    and changed init() to:

    int init()
    {
    current->ip = &a;
    printf("init :%d\n", *current->ip);
    return 0;
    }

    And I know get the correct print form zap.
     
    Johs32, Mar 14, 2006
    #5
  6. "Johs32" <> wrote in message
    news:dv77hh$hc6$-c.dk...
    >I have the follwing code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct data {
    > int *ip;
    > };
    >
    >
    > struct data first;
    > struct data *current = &first;
    >
    >
    > int init()
    > {
    > int a = 222;
    > current->ip = &a;
    > printf("init :%d\n", *current->ip);
    > return 0;
    > }
    > void zap(int a, int b)
    > {
    > printf("current->prio: %d\n",*current->ip);
    >
    > }
    >
    > int main()
    > {
    > init();
    > zap(3,5);
    > return 0;
    >
    > }
    >
    > When I do the printf in init() I get 222 as expected. But when I do it in
    > zap(int a, int b) I get 5!! If I remove the arguments from zap like this:
    >
    > zap()
    >
    > and call it like this from main:
    >
    > zap()
    >
    > I get 222 printed as expected from the zap() function.
    >
    > What kind of black magic is this?
    >
    > JOhs


    After returning from init(), current->ip points to an address that is now
    out-of-scope.
    (it was set to the address of variable 'a', which was local to init() now no
    longer exists).
    Anything can happen when you try to dereference it.
     
    Fred Kleinschmidt, Mar 14, 2006
    #6
    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. Flores Eken
    Replies:
    1
    Views:
    348
    Flores Eken
    Dec 26, 2003
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    500
    Raymond Hettinger
    Jul 27, 2003
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,682
    Merrill & Michele
    Oct 19, 2004
  4. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    446
    jacob navia
    Oct 17, 2004
  5. David Lucus

    Very wierd Problem. Please help.

    David Lucus, Jul 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    151
Loading...

Share This Page