3

Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?

So heres a simple c++ function. What it does it take an array of characters as its argument and a integer n and then creates a new character array with only n elements of the array.

char * cutString(char * ch , int n){

  char * p = new char[n];
  int i ;
  for(i = 0 ; i < n ; i++)
         p[i] = ch[i];
  while(i <= n ){
    p[i++] = '\0';
  }
  return p ;

}

This works just fine but if I change char * p = new char[n]; to char p[n]; I see funny characters. What happens? What difference does the former make? Also, p is a temporary variable; how does the function return it successfully?

Community
  • 1
  • 1
Nash Vail
  • 848
  • 2
  • 11
  • 27
  • 1
    http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – chris Nov 09 '12 at 16:07
  • If `char* c` is a null terminated string, there should be no reason to add a second null terminator. The `for` loop terminates when `i == n` so the while loop will always start. The while loop adds a null terminator at `p[i]` (really `p[n]`) before incrementing `i`. p[n] is one over the allocated size of the array. Either you need to say `p = new char[n + 1]` at the top, or replace the while loop with `if (p[n - 1] != '\0') p[n - 1] = '\0';` or start out saying `if (ch[n] != '\0') return 0`. I don't know what's appropriate for your usage model. – GlenPeterson Nov 09 '12 at 19:36

4 Answers4

9

char *p = new char[n] dynamically allocates memory on the heap. Such memory's lifetime is not bound to any function and it exists until it's deallocated (using delete[]). Therefore, returning a pointer to it is perfectly valid.

char p[n], on the other hand, allocates memory on the stack, whose lifetime is bound to the function defining it. Once that function returns, any references to the memory become invalid.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • so if I use delete [] p inside the function will the program still work ? I mean I deleted it so it has nothing to return so it shouldn't work – Nash Vail Nov 09 '12 at 16:14
  • @user1218380 Yes, if you use `delete[] p` inside the function and then `return p`, you're returning a pointer to invalid (unallocated) memory. Chances are your data is still there and if accessing the returned pointer doesn't cause an access violation, it will get the data, but that is pure coincidence. Accessing deallocated memory is illegal in principle. – Angew is no longer proud of SO Nov 09 '12 at 16:22
7

When you change the code to:

char p[n];

...and then return p, you are returning a pointer to a locally-scoped variable. After the function returns, p no longer exists. The long and the short of it is you are evoking Undefined Behavior, and your program is ill-formed when you do this.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

because new char[n] will allocate memory on the heap (don't forget to delete[] it later)

whereas char p[n]; is a static array which resides on the stack which gets reused automatically after the function returns

if you return a reference to it from a function you are returning a pointer to unues memory and referencing unused memory is stock-standard undefined behavior

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
1

It's the difference between dynamic allocation and automatic allocation of memory. char * p = new char[n]; is dynamic allocation and it means the memory remains valid until you delete it. On the other hand, char p[n]; is strictly speaking not legal C++, but with the compiler you are using it is a form of automatic allocation, which means the memory is no longer valid when you exit the function. That's why you see funny characters.

john
  • 7,897
  • 29
  • 27