Stack Problem

This is a discussion on Stack Problem within the C Programming forums, part of the General Programming Boards category; Hello guys.This is my first post. Problem: The first printf outputs 35 which makes sense but the second printf outputs ...

  1. #1
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22

    Stack Problem

    Hello guys.This is my first post.

    Problem: The first printf outputs 35 which makes sense but the second printf outputs a garbage value? I know this has something to do with stack but can any1 clarify how it works in this program?Thanks.

    Code:
    int *j;
    
    j=fun( );
    
    printf("\n %d ",*j);
    
    printf("\n %d ",*j);
    
    int *fun( )
    {
    
    int k=35;
    
    return (&k);
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Enable the warnings in your compiler, you should see "warning: function returns address of local variable".

    In fact you are returning the address of a local variable (k), which means that you can (or should) only use this variable in the scope (function) where it has been defined.

    You could use an int pointer allocated with malloc instead.
    HomePort : A C Web Service API for heterogeneous home automation systems

  3. #3
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    I am using Turbo C++ and i dont see any warning.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    I don't know Turbo C++, but I recall a lot of people saying that it is outdated on this forum.

    But anyway, your problem remains the same, the local variable is "destroyed" when the program exits the function. So you need to use dynamically allocated variable.
    HomePort : A C Web Service API for heterogeneous home automation systems

  5. #5
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    but if it gets destroyed than why does the first printf display 35 ,which means thats the value of 35 is still in stack but after the first printf the variable k gets destroyed. I am confused in the working of the stack cant seem to understand it clearly

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Waleed Mujeeb View Post
    but if it gets destroyed than why does the first printf display 35 ,which means thats the value of 35 is still in stack but after the first printf the variable k gets destroyed. I am confused in the working of the stack cant seem to understand it clearly
    It might not explicitly be destroyed, more like abandoned, which means that the value can be overwritten by something else, which you just witnessed yourself. The take home point here is that you should make no assumptions about this value, because you can't.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Take a look at the definition of a local variable on Wikipedia, I think it might help you.

    If you notice in my previous post I put the destroyed word between quotation marks, because the memory used for your local variable is not erased, it is just deallocated, which means that the memory may be reused by the program to store other variables.
    When you do your first printf, you get lucky because the memory has not been reused yet, so the 35 is still stored in it. But my best guess is that during the printf the memory is reused, which is why you get garbage on the second one.
    But this is a guess, because you never know what will happen when you access non allocated memory, the behavior is undefined, and it can often lead to a crash of your program.
    HomePort : A C Web Service API for heterogeneous home automation systems

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    On the same line, I know when a function exits, the local variables are deallocated the hence returning their addresses are meaningless. In case you want to save the address, use the declaration static int k = 35 instead of int k = 35. This saves the address of k even after exiting the function.

    But I do have a question about allocating the memory within the function. when u allocate a memory dynamically in a function, what happens to it when u exit the function? does it get deallocated or is it saved until free( ) is called somewhere else in the program? and if it is saved until free is called somewhere else, the argument to free, which is a pointer is local to the function where memory was allocated. Can this pointer be accessed from some other part of the program? In other words, what is the scope of a pointer which is dynamically initiated?

    Thanks,
    Livin

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by livin View Post
    In other words, what is the scope of a pointer which is dynamically initiated?

    Thanks,
    Livin
    The pointer will also be destroyed, but the address it contains will be valid until that region is freed. When you return from such a function you could return the value (i.e the address on the heap) but not the pointer it self.

    Code:
    char *foo() {
         char *p = malloc( 16 * sizeof(char) );
         if(!p) return NULL;
         
         return p;
    }

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Subsonics View Post
    ... When you return from such a function you could return the value (i.e the address on the heap) but not the pointer it self.
    Isn't returning the value viz. the heap location, the same thing as returning the pointer itself.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by itCbitC View Post
    Isn't returning the value viz. the heap location, the same thing as returning the pointer itself.
    Well p in this case, is a local variable. What I had in mind was the difference of returning the value that p contains and returning &p like Waleed Mujeeb did in the first post.

    In that case: 'return k' would be fine as 35 would be returned, 'return &k' would be useless since k can not be referenced after fun() returns.
    Last edited by Subsonics; 01-23-2012 at 12:35 PM.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Subsonics View Post
    Well p in this case, is a local variable. What I had in mind was the difference of returning the value that p contains and returning &p like Waleed Mujeeb did in the first post.
    Yep! the address of a local variable would be a dangling pointer; and thanks for clarifying.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by Subsonics View Post
    The pointer will also be destroyed, but the address it contains will be valid until that region is freed. When you return from such a function you could return the value (i.e the address on the heap) but not the pointer it self.
    In that case, if the pointer is destroyed, how will you free the memory that it points to? shouldn't u have a statement like free(p) somewhere before main() exits? and if p is no longer available what argument would free() take?

  14. #14
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    In that case, if the pointer is destroyed, how will you free the memory that it points to?
    In order to free the memory, you need a pointer to the address of that memory. So you always need to keep a valid pointer to a dynamically allocated memory, otherwise, this memory will be "definitely lost", which means that the program will not be able to reuse the memory, since it will still be considered as allocated, even though you cannot access it anymore.

    shouldn't u have a statement like free(p) somewhere before main() exits?
    Yes you should, but not necessarily "p", since as mentioned previously, the p variable will not be valid anymore once you leave the function. For example, using the "foo" function that Subsonics posted, you could do like :

    Code:
    char *p2 = foo();
    //do whatever you want with that pointer, and when not needed anymore :
    free(p2);
    HomePort : A C Web Service API for heterogeneous home automation systems

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by Tibo-88 View Post
    In order to free the memory, you need a pointer to the address of that memory. So you always need to keep a valid pointer to a dynamically allocated memory, otherwise, this memory will be "definitely lost", which means that the program will not be able to reuse the memory, since it will still be considered as allocated, even though you cannot access it anymore.


    Yes you should, but not necessarily "p", since as mentioned previously, the p variable will not be valid anymore once you leave the function. For example, using the "foo" function that Subsonics posted, you could do like :

    Code:
    char *p2 = foo();
    //do whatever you want with that pointer, and when not needed anymore :
    free(p2);

    Ahah! got it. thanks for the clarification.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem Stack in c++
    By magician89 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2010, 08:32 AM
  2. need help for stack problem again
    By bahareh in forum C++ Programming
    Replies: 20
    Last Post: 11-24-2006, 09:01 AM
  3. STL Stack problem
    By xmltorrent in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2006, 05:43 PM
  4. Stack problem
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2003, 03:30 PM
  5. Stack problem
    By riley03 in forum C++ Programming
    Replies: 6
    Last Post: 10-09-2002, 08:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21