Take the 2-minute tour ×

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 ;
         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?

share|improve this question

migrated from programmers.stackexchange.com Nov 9 '12 at 16:06


marked as duplicate by R. Martinho Fernandes, Alok Save, Henrik, Luchian Grigore, Loki Astari Nov 9 '12 at 16:21


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 9 '12 at 19:36
You are getting lucky. –  MrFox Nov 9 '12 at 19:42
add comment

4 Answers

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.

share|improve this answer
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 –  nashmaniac Nov 9 '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 Nov 9 '12 at 16:22
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment
