#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    2
    Rep Power
    0

    Can you assign a pass-by-reference var to a local var?


    Greetings,

    This is an over-simplification for clairity:

    void test(struct stat *sbp);

    int main(void)
    {
    struct stat sb;
    test(&sb);
    return(0);
    }

    void test(struct stat *sbp)
    {
    struct stat sb2;
    *sbp = sb2;
    return;
    }

    What happens to sb in main() when test() returns? I thought local variables were defined on the stack, so if sb in main is set to point to sb2 in test(), when test() returns, doesn't sb2 go away, and thus sb, become invalid?

    Thanks,
    Matthew
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    2
    Rep Power
    0
    To answer my own post, I found this:

    ---
    Structure Assignments

    ANSI C compilers allow the information in one structure to be assigned to another structure, as in:

    binfo=addr_info;
    ---

    I confirmed this. Basically the assignment of one struct to another actually copies the data of one struct into the other. I didn't know that. I wonder why that functionality is extended to structs but not arrays? As we all know, you have to use something like memcpy() or strncpy() with arrays...

    Oh well, you learn something every day!

    Matthew
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    This statement:
    *sbp = sb2;
    doesn't do what you think it does. It DOES NOT point sb to sb2. What it actually translates to is, set the value of what sbp points to (i.e. sb in main) to the value of sb2. So, all it does is copy the values of sb2 into sb. To make things clearer, consider the following:
    Code:
    #include <stdio.h>
    
    struct foo {
      int a, b;
    };
    
    void test(struct foo *sbp);
    
    int main(void)
    {
      struct foo bar;
      bar.a = 1; bar.b = 3;
    
      printf("Before Test: we have a = %d and b = %d\n", bar.a, bar.b);
      test(&bar);
      printf("After Test: we have a = %d and b = %d\n", bar.a, bar.b);
    
      return 0;
    }
    
    void test(struct foo *sbp)
    {
      struct foo sb2;
      sb2.a = 5; sb2.b = 6;
      *sbp = sb2;
      return;
    }
    Run it and you'll see that after the call to test(), the value of bar in main() will have different values.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Originally posted by Scorpions4ever
    This statement:
    *sbp = sb2;
    doesn't do what you think it does. It DOES NOT point sb to sb2. What it actually translates to is, set the value of what sbp points to (i.e. sb in main) to the value of sb2. So, all it does is copy the values of sb2 into sb.
    Yes, I assumed that is what was happening. I've never taken that approach in C, so I was surprised when matthew180's code compiled (after I added a dummy struct declaration).

    I think that matthew180 was getting garbage, but he should realize that is because he had never initialized the local variable, sb2.
  8. #5
  9. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    >>doesn't sb2 go away, and thus sb, become invalid?

    It should do, but the memory will still be hold the same values until the OS reuses it.

    AFAIK this is an error termed a 'dangling pointer'.

    As local (non static) variables only have scope until the function hits a 'return', returning a pointer to a (non static) local variable is wrong. In small apps and with small structs (especially in the IDE's protected memory environment) the memory will still hold the values. Not so in large apps outside the IDE.

    http://pdsf.nersc.gov/documentation/..._retd.htm#HDR2
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    >> AFAIK this is an error termed a 'dangling pointer'
    If you look at the code carefully, this isn't a dangling pointer :). He's passing a variable by ref and then assigning a value to the variable that he passed by ref. He is NOT passing back a pointer to a local variable, so the problem of a dangling pointer doesn't occur at all.
  12. #7
  13. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    13
    Originally posted by Scorpions4ever
    >> AFAIK this is an error termed a 'dangling pointer'
    If you look at the code carefully, this isn't a dangling pointer :). He's passing a variable by ref and then assigning a value to the variable that he passed by ref. He is NOT passing back a pointer to a local variable, so the problem of a dangling pointer doesn't occur at all.
    You're correct when you say there is no dangling pointer in this example.

    A dangling pointer is one that points at an object that no longer exists (eg it has been deleted, freed, passed out of scope, etc). It doesn't only result from returning local variables by address.

    Consider whether the following example introduces a dangling pointer, which variable holds it, and what the results will be.

    #include <stdio.h>

    struct foo
    {
    int a,b;
    }

    void test(struct foo *x, int test)
    {
    static struct foo *q = NULL;
    if (!test)
    q = x;
    else if (test && q != NULL)
    fprintf(stdout, "%d %d\n", q->a, q->b);
    }

    int main(int argc, char **argv)
    {
    if (argc >= 0)
    {
    struct foo a;
    test(a,0);
    }
    test((struct foo *)NULL, 1);
    return 0;
    }
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Nice one --- I'd say the dangling ptr is assigned to the static variable q in function test. BTW I think you should call test like this:
    test(&a, 0);
    That way, it won't give you a warning.
  16. #9
  17. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Sorry, I assumed when Mathew said

    *sbp = sb2; //copy cntents of local struct

    he realy ment

    sbp = &sb2;//point to local struct

    creating a dangling pointer.

    AFAIK Grumpy's example is not a dangling pointer but I see the point.

    Static variables have scope for the entire program so will not create dangling pointers.

    As does a variable declared in main (it has scope until the main function hits a return which will end the program, therefore it has scope for the entire program).

    This assumes you call the function correctly (with a pointer to the struct) and not try to pass an instance of the struct.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  18. #10
  19. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    13
    Originally posted by Scorpions4ever
    Nice one --- I'd say the dangling ptr is assigned to the static variable q in function test. BTW I think you should call test like this:
    test(&a, 0);
    That way, it won't give you a warning.
    Correct; I typed too fast obviously :-)

IMN logo majestic logo threadwatch logo seochat tools logo