By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

About Stack

P: 30
#include <iostream.h>
#include <string.h>

char *fun(void);

void main()
{

char *p = fun();

cou<<p;

}

char *fun()
{

char buf[32];
strcpy(buf,"hello");
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?
Oct 1 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. char *fun()
  2. {
  3.   char buf[32];
  4.   strcpy(buf,"hello");
  5.   return buf;
  6. }
  7.  
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.

Expand|Select|Wrap|Line Numbers
  1. char *fun()
  2. {
  3.   static char buf[32];
  4.   strcpy(buf,"hello");
  5.   return buf;
  6. }
  7.  
or allocate the memory for the pointer but then the calling code must free the memory or cause a memory leak


Expand|Select|Wrap|Line Numbers
  1. char *fun()
  2. {
  3.   char * buf;
  4.   buf = malloc(strlen("hello")+1);
  5.   if (buf != NULL)
  6.   {
  7.     strcpy(buf,"hello");
  8.   }
  9.   return buf;
  10. }
  11.  
Or return a pointer to a string constant but then you can not alter the data returned


Expand|Select|Wrap|Line Numbers
  1. const char *fun()
  2. {
  3.   return "hello";
  4. }
  5.  
Oct 1 '06 #2

P: 30
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
Oct 1 '06 #3

100+
P: 293
D_C
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".
Oct 2 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
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::operator<<(const char * s) puts data on the stack.

However try this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. char *fun(void);
  5.  
  6. void main()
  7. {
  8.  
  9.     char *p = fun();
  10.  
  11.     NotFun();
  12.  
  13.     cout<<p;
  14. }
  15.  
  16. char *fun()
  17. {
  18.  
  19.     char buf[32];
  20.     strcpy(buf,"hello");
  21.  
  22.     return buf;
  23. }
  24.  
  25. void NotFun()
  26. {
  27.  
  28.     char buf[32] = "I'm having a bad day, go away";
  29. }
  30.  
Oct 2 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.