0

Ok, so I have two pointers, one initialising new array:

int * array = new int [5];
int * ptr = &array[0]; // or "= array", is this the same?

Now let's assign some value

array[0] = 12;

Now check if they point the same adress and value

cout << "Adress under array: "<< array << " Value under array: "<< array[0] <<
" Adress under ptr: "<< ptr << " Value under ptr: " << *ptr;

Output shows the same adresses, and 12 two times. Now let's delete array:

delete [] array;

Now there is my question, this:

cout << array;

Output different address than before. Additionaly

cout << *ptr;

does not output 12, so the value in the old array[0] changed.

Then, where the pointer points after deleting array []array? The other question, is array pointer to array, or just pointer for int?

I know that int *ptr[x] will be a pointer to array of x integers, but i do not use square brackets on the left side here int * array = new int [5] so is array the pointer to the first element of the array?

Slajni
  • 105
  • 1
  • 10

3 Answers3

3

When you execute delete [] array;, the address held in array becomes an invalid pointer value, so you can't even inspect it reliably in order to print it.

When the end of the duration of a region of storage is reached, the values of all pointers representing the address of any part of that region of storage become invalid pointer values (3.9.2). Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Ok, but I can still use the `array` pointer right? So where the pointer points after i use `delete`? Is it random free place in memory or what? – Slajni Dec 29 '16 at 19:46
  • Slajni: I thought the quoted statement "the values of all pointers representing the address of any part of that region of storage become invalid pointer values" was pretty clear. You may not be able to print it. You may not be able to compare it to other pointers. You should treat whatever value is left in `array` (and `ptr`) as pure poison, the only safe thing to do is store a new valid or null value over the top. – Ben Voigt Dec 29 '16 at 19:48
  • By "Ok, but I can still use the array pointer right" i wanted to ask if i can still use it for point to other value. – Slajni Dec 29 '16 at 19:51
  • Anyway do you know answer for "So where the pointer points after i use `delete`? Is it random free place in memory or what? – Slajni Dec 29 '16 at 19:52
  • 1
    Slanji: It's not true that all pointers point to something. After you use `delete [] array;` what's inside `array` and `ptr` ceases to be meaningful. Even a self-comparison `delete [] ptr; if (ptr == ptr) /* Happy! */;` may crash your program. The pointer variable still exists, but it can't be used for anything until you store a different address inside. – Ben Voigt Dec 29 '16 at 19:56
  • Right, i get it. I have one last quiestion. `int (*ptr)[5]` may be pointer to array of integers with size 5 right? I have: `int * ptr;` - pointer to integer `int b = 5;` - integer `ptr = &b`; - pointer to integers points to integer Now: `ptr = new int [5]` - pointer to integer points to... what? Array or the first element of the array? – Slajni Dec 29 '16 at 20:03
  • @slajni: The address of the array and the address of the first element are the same, but have different types. `int * ptr` is not a pointer to an array, but to an `int` (which is the element type of an `int[]`) – Ben Voigt Dec 29 '16 at 20:06
  • Then type of adress of the array is (*)[]? – Slajni Dec 29 '16 at 20:10
  • The type of the address of the array is `int(*)[5]`. But `new[]` doesn't return this type, it returns a pointer to the first element (and the type is just `int*`) – Ben Voigt Dec 29 '16 at 20:11
  • Right, all clear. Btw, one technical question. When i tried to edit my last comment i got error message "Users without post commenting privilege can't comment auto-generated comment". Well, i guess my comment wasn't auto generated, so why couldn't I edit it? – Slajni Dec 29 '16 at 20:14
1

Accessing the array after you delete it is Undefined Behaviour - meaning; anything could happen, you are not guaranteed any consistent result - your program has a bug if you do that and its execution is no longer well defined. Even just reading the pointer value is Implementation Defined and you can't be sure what you get across implementations.

So in short; just don't do that.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

There is a difference between int* ptr[5] and int* ptr = new int[5];

The first one creates an array of pointers with a fixed size 5. And the other one creates a pointer to an array of integers with a fixed size 5

int* ptr = &array[0]; is the same as saying int* ptr = array;

See. By doing int* array = new int[5] you

  1. create an array with 5 integer values
  2. create a pointer that points to the first ([0]) address of the array ... meaning that if you point to &array[0] is the same as array

After deletion the pointers point to an address which has memory allocated which will result in undefined behaviour since you don't know what gets allocated on this address afterwards

anon
  • 210
  • 3
  • 11
  • Ye, i used bad syntax. By `int* ptr[5]` i meant to say `int (*ptr)[5]` which is pointer for array with fixed size of 5 right? – Slajni Dec 29 '16 at 19:48