   Why this works?
  Why this works, when it shouldn't (atleast i think so):char* do_something(){  char t[101];  .. // do something with string t  return &t}Now i am creating object t in scope of this function, also i am returning at the end of the function. Shouldn't object t be destroyed after the exit from the function (just after return)?   
  In a release mode, the memory used by t won't be overwritten until something else is put on the stack - so that will probably work in the short term.In a debug build the compiler will probably insert code that overwrites t's memory to help you catch problems like this.   ;  The behaviour is undefined.  t is deallocated when the function exits, but since it is stack allocated this deallocation usually just involves a modification of the stack pointer.  The memory t occupied isn't immediately overwritten, it's just marked as available again.  Until the point that the stack memory that t existed in is reused many operating system/compiler combinations will not object to you accessing that memory as if t still exists.  Usually.  However, this is not something you should rely on.  It is also perfectly acceptable for your compiler to zero-out the memory when the function returns, or request the operating system mark the memory area invalid, in which case access after the function has returned will just crash.  To misquote the Nike slogan, "Just don't do it".Enigma   ;  I thought about the same, but wasn't sure!Thanks!r++ both of you! :)   ;  In order for it to work however you will need to make t static, or dynamically allocated so it doesn't go out of scope with the stack frame. But the best solution is the pass a pointer to an array to be filled rather than returning a pointer.   ;  Any compiler worth it's salt should emit a warning. One reason to always compile with full warnings.   ;  When you return &t, you're actually supposed to return t. t is already a pointer.  Think of t[1] as *(t+1).But here's a way that will work.char* do_something(){   char *t = (char *)malloc(101);   // do something   return t;}char t[101] is basically the same as char *t = (char *)malloc(101), except malloc allocates it separately, which keeps it alive after the function ends.  I use this to return strings.  But I use free(t); when I'm done with the string to keep my memory clean.   ;Quote:Original post by bitterbananaWhen you return &t, you're actually supposed to return t. t is already a pointer.  Think of t[1] as *(t+1).But here's a way that will work.*** Source Snippet Removed ***char t[101] is basically the same as char *t = (char *)malloc(101), except malloc allocates it in the heap, which keeps it alive after the function ends.  I use this to return strings.  But I use free(t); when I'm done with the string to keep my memory clean.I really don't like bitter bananas....  But that's not the point ;).Anyways, if I am correct, won't malloc(101) just allocated void* memory on the heap with a size of 101 bytes?  Unless a char is a byte, I don't think that will work out.  But then again, maybe I am wrong ;P?@ OP:You are returning the address of an on-stack variable.  Your conclusion was correct.  All (_ALL_) variables on the stack are deallocated for you in each(_EACH_) scope.  Not only functions, but scopes (lucky we, image not having the stack ;)).  Returning the address of a stack object (in a scope, not global) will be unpredictable.  Depending on what happens with the memory, either you access t, nothing or something (<   ;Quote:Original post by Anonymous PosterQuote:Original post by bitterbananaWhen you return &t, you're actually supposed to return t. t is already a pointer.  Think of t[1] as *(t+1).But here's a way that will work.*** Source Snippet Removed ***char t[101] is basically the same as char *t = (char *)malloc(101), except malloc allocates it in the heap, which keeps it alive after the function ends.  I use this to return strings.  But I use free(t); when I'm done with the string to keep my memory clean.I really don't like bitter bananas....  But that's not the point ;).Anyways, if I am correct, won't malloc(101) just allocated void* memory on the heap with a size of 101 bytes?  Unless a char is a byte, I don't think that will work out.  But then again, maybe I am wrong ;P?@ OP:You are returning the address of an on-stack variable.  Your conclusion was correct.  All (_ALL_) variables on the stack are deallocated for you in each(_EACH_) scope.  Not only functions, but scopes (lucky we, image not having the stack ;)).  Returning the address of a stack object (in a scope, not global) will be unpredictable.  Depending on what happens with the memory, either you access t, nothing or something ({--This is mostly the common one.).Just remember - never return addresses of local variables on the stack.Good luck!C++Sorry about that - my arrow to the left made the computer think it was html, and since AP's can't post html my post got cut off.Good luck!P.S.  Can a mod delete my previous post because it was cut off?C++;  It's not a local variable if you manually allocate a block of memory using malloc.  It'll only free the block if you use free(variable).  The only thing that's pushed on the stack is the pointer to that memory block.  The pointer is pushed off the stack when the function is done, but the memory block is not deallocated.   