About Stack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanketbarot
    New Member
    • Sep 2006
    • 30

    About Stack

    #include <iostream.h>
    #include <string.h>

    char *fun(void);

    void main()
    {

    char *p = fun();

    cou<<p;

    }

    char *fun()
    {

    char buf[32];
    strcpy(buf,"hel lo");
    return buf;
    }

    In this program how the address of the buffer will be pass to the pointer?
    I am confused caz buf is a local variable, and when fun() come back to the main at that time if I am not wrong stack will be cleared.
    Can I know the stack working for main and for the function fun()?
    or from where can i get the information about the stack?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Code:
    char *fun()
    {
      char buf[32];
      strcpy(buf,"hello");
      return buf;
    }
    This is an error and very very poor style. You should not return a pointer to a local variable because as soon as you return that pointer is no longer valid as the memory for the variable is returned to the stack. This invokes undefined behaviour.

    If you continue to do this in the end you will cause bizzare and hard to trace errors in your program.

    You should either make the variable returned static, but then there will only ever be 1 copy of the variable so each call will overwrite the previous data.

    Code:
    char *fun()
    {
      static char buf[32];
      strcpy(buf,"hello");
      return buf;
    }
    or allocate the memory for the pointer but then the calling code must free the memory or cause a memory leak


    Code:
    char *fun()
    {
      char * buf;
      buf = malloc(strlen("hello")+1);
      if (buf != NULL)
      {
        strcpy(buf,"hello");
      }
      return buf;
    }
    Or return a pointer to a string constant but then you can not alter the data returned


    Code:
    const char *fun()
    {
      return "hello";
    }

    Comment

    • sanketbarot
      New Member
      • Sep 2006
      • 30

      #3
      ya though buf is a local variable. my output is correct.
      that is why i am not getting the point, how my output is correct.
      I am using Turboc++ compiler

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Once you exit the function fun(), the memory occupied by buf no longer belongs to your program. It is possible, yet not probable, that some other program requests memory and it gets the memory that buf occupied.

        The memory now belongs to that program, and it can change the contents of buf to whatever it wants and it has every right to do so. Then, when you access the memory that now belongs to the other program, you may print something other than "hello".

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Indeed, just because your code no longer owns the memory doesn't mean that it has been overwritten, it just means that it shouldn't be accessing it.

          On my system I do not get "hello" output at all because ostream& ostream::operat or<<(const char * s) puts data on the stack.

          However try this

          Code:
          #include <iostream.h>
          #include <string.h>
          
          char *fun(void);
          
          void main()
          {
          
              char *p = fun();
          
              NotFun();
          
              cout<<p;
          }
          
          char *fun()
          {
          
              char buf[32];
              strcpy(buf,"hello");
          
              return buf;
          }
          
          void NotFun()
          {
          
              char buf[32] = "I'm having a bad day, go away";
          }

          Comment

          Working...