Dynamic array delete

Hello guys.
I can`t delete dynamic array using
 
  delete []array;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main ()
{
int *array = new int[10];//array declaration with 10 elements

for (int i=0;i<10;i++) //filling the array 
{
array[i] = i+1;
}


for (int i=0;i<10;i++) //array output
{
cout<<array[i]<<" ";
}

cout<<"\n";

delete []array; //deleting a memory

for (int i=0;i<10;i++) //test array output
{
cout<<array[i]<<" ";
}

cout<<"\n";

return 0;
}


Program output (both array the same, only deleted first element):

./main
1 2 3 4 5 6 7 8 9 10
0 2 3 4 5 6 7 8 9 10

You are invoking undefined behavior on line 26. When you delete the memory doesn't necessarily alter the content of that memory, it just tells the operating system that that memory is available for reuse.

So, it`s no necessairily, that memory after delete operation will empty all data, that she stored before deleting? Ok, but how delete an pointer array, for reusing this pointer name? Thanks a lot.
Last edited on
What do you mean by "pointer array"?
Which pointer (name) do you want to reuse?
I want:
1
2
3
4
5
int *array = new int[10];//dynamic array declaration

delete []array; //memofry refusing

int *array = new int[15]; //array re-declaration 


But compiler give an error, that array already previously declared.

Help, please, how to reuse pointer name, for example if another place of program?
Aso it need, i think, when need to change an array size.
Last edited on
closed account (LA48b7Xj)
You are redefining array, I wouldn't use that word anyway since it used for std::array.

1
2
3
4
5
6
7
int *p = new int[10];
//do something
delete[] p;

p = new int[500];
//do something
delete[] p;
Thanks a lot, but after delete[] p;, pointer p store info about address of array first element. Maybe i some not understand, how to use pointer p, for example, for store address for another array, for example float *p = new float[15];. Or it only possible to do in different scopes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function1()
{
int *p = new int[10];
//do something
delete[] p;
}

void function2()
{
float *p = new float[15];
//do something
delete[] p;
}


Last edited on
When you say new a block of memory is dynamically allocated and you do get an address that you have to store in a pointer in order to reach that memory.

When you use delete the block at the address is deallocated and you may no longer use it. The deletion does not change the pointer variable. The pointer still stores an address, but that is an invalid address, because it does not point to properly allocated memory.

If you now allocate a block again, it will be in some address -- a new address that you have to store in a pointer.

1
2
3
4
5
6
7
int *p = new int[10];
//do something
delete[] p;

p = new int[500];
//do something
delete[] p;

is similar to:
1
2
3
4
5
int x = 42;
//do something

x = 7;
//do something 

Both the pointer (p) and integer (x) are variables that are set to new value at some point.
Thanks a lot, this is i clear understand with Your help!!!
So about this i have the last question about pointer name.
Reuse the similar pointer name p for another arrays poissible only in different scopes?
1
2
3
4
5
6
7
8
9
10
11
12
13
void function1()
{
int *p = new int[10];
//do something
delete[] p;
}

void function2()
{
float *p = new float[15];
//do something
delete[] p;
}

Forget the arrays and pointers in that one. You question is more general and is about names.

Those are not similar. A p is identical to p, if they are in the same scope.

One cannot declare the same name more than once in the same scope.

However, one can declare the same name in an inner scope, in which case it masks identical name of an outer scope. Masking is confusing. Avoid doing it.


Local names in the scope of one function are entirely different from local names in the scope of another function. Therefore, the p in function1 is not at all similar to the p in function2.


Once again, there is nothing that prevents you from using pointer p to point first to one array and later to point to a different array within same function.

This is entirely legal:
1
2
3
4
5
6
7
8
9
int x;
int * p = nullptr;
p = new int[7];
delete [] p;
p = &x;
p = new int;
delete p;
p = new int[9];
delete [] p;


But why use raw pointer, new and delete ?
You could use smart pointer, like std::unique_ptr (there is std::make_unique for it), or container like std::vector.
Thanks a lot everybody!
Topic archived. No new replies allowed.