Dynamic Array, delete

Hi All

I make a Dynamic Array in C++ , and I want to delete it, But without using delete command, so what will happen if we make the arry pointer point to the NULL?
i wrote code like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int *arry = new int[10];
	for(int i = 0 ; i < 10 ; i++)
		cin>>arry[i];
	cout<<endl<<endl<<endl;
	for(int i = 0 ; i < 10 ; i ++)
		cout<<arry[i];
	cin.get();
	cin.get();
	arry = NULL;
	for(int i = 0 ; i < 10 ; i ++)
		cout<<arry[i];
	cin.get();
	cin.get();
	return 0;
}

and it gonna did not show values in third "for".

if we do that, is the memory that dedicated to the arry with size 10 , be free?
because i think in the memory, only the blocks will still and will no dedicate to any other programs that have a pointer to it, and if we make arry point to NULL, we will make other blocks free.

any one have an idea about it?

Have Nice Life


EDITS:all of edits are about make my misspelling right and the question did not changed
Last edited on
If you create something with 'new', you must release it using 'delete'. When you set it to NULL, you are only changing the variable 'arry' to NULL. All of the data is still in memory, you just can't access it anymore because you deleted the pointer to its location.
Last edited on
closed account (L6b7X9L8)
Making the pointer point to nothing doesn't release the memory. As Yay295 said to release them memory from being allocated you have to use delete[].

I believe your operating system will automatically reallocate the memory when the program is closed. But when programming hardware you won't have that support, so that RAM will be unusable until the system is turned off, I believe.
Also, when allocating with new[], then use delete [] when finished.
1
2
3
4
5
    int *arry = new int[10]; // allocate memory
    
    // do stuff here

    delete [] arry;          // release memory 
Thanks all,
i think it was reallocate the blocks because of my OS,as the zorg said, I did not think about that side of program.
But anyone know what delete[] do to release memory?
closed account (j3Rz8vqX)
Memory leak.

Memory doesn't automatically free itself in C++; especially primitive data types.

Some objects such as smart pointers will, but not the native pointer.
i mean how it work?
how delete[] release memory?

When you allocate some memory it becomes your applications memory, memory which only your application can use and modify. If you were to simply close that application without freeing it then it would still be marked as "in use" and Windows (or any other OS or App) couldn't touch it - keep doing that on older systems which had very little memory you would run into problems.

The delete[] operator frees the memory back to Windows (or another OS) telling it that its no longer being used by your application, and it can then be used again by the OS or an Application.
closed account (j3Rz8vqX)
But [does] anyone know what delete[] do[es] to release memory?
Don't know if it'll do you any good, but this is the assembly behind c++ new/delete:
1
2
3
4
5
call    ??2@YAPAXI@Z                ; Calling operator new functions
;Stuff...
call    ??0X@@QAE@H@Z               ; The call object constructor
;Stuff...
call    ??1X@@QAE@XZ                ; The destructor is called
http://www.programering.com/a/MDM1ETMwATc.html

A simple example code that illustrates data being read after its array has been set to null. The user will input a valid address and the program will print the value at the address:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

void devils_advocate()
{
    srand(time(0));
    int *arry = new int[5];
    int *safety = arry;                 //Safety net to release memory
//---------------------Warning-----------------
    cout<<"Warning: Memory will leak if program crashes or forced exit!"<<endl<<endl;
    cout<<"If so, a simple reboot will restore any memory loss this way."<<endl<<endl;
//---------------------Pretest-----------------
    cout<<"Inserting 5 random integers into the array: "<<endl<<endl;
    for(int i = 0 ; i < 5 ; i++)        //Insert data into the array
    {
        arry[i]=rand()%90+10;//Example data
        cout<<"Address: "<<&arry[i]<<" with a data of: "<<arry[i]<<endl;//Print data
    }
    cout<<endl<<endl;
    cout<<"Array has been set to NULL however data will still persist..."<<endl<<endl<<endl;
    arry = NULL;                        //Set address of arry to NULL

//---------------------Postest-----------------
    int*head;
    string address;
    do
    {
        cout<<endl;
        cout<<"Please enter: (A 'valid address' listed above) or ('Enter') to exit: ";
        getline(cin,address);
        if(address!="")
        {
            head=reinterpret_cast<int*>(strtol(address.c_str(),0,0));
            cout<<"The data at address: "<<head<<" is "<<*head<<endl;
        }
        else
            break;
    }while(true);
    delete []safety;                //Release memory
    cout<<"Memory freed"<<endl;
}
int main()
{
    string choice;
    cout<<"Choice: TEST(1) or EXIT(return key): ";
    getline(cin,choice);
    if(choice=="1")
        devils_advocate();
    else
        cout<<"Exit without testing"<<endl;
    return 0;
}

After arry = NULL; the data(s) would still persist at their respective addresses.

Even if we were to remove: int *safety = arry; and delete []safety; ,having no pointers associating with the memory, the data would still persist when we later access those addresses.

However, if we were to call delete []safety; before line 29, we would get undefined behaviors. The data will become garbage while the addresses would still be accessible, however there are possibilities that others may reserving that specific memory.

Warnings:
In the above code, if you are to enter an invalid address, a segmentation fault will occur and you will be leaking memory. Same applies to ending the program early; do not force exit with ctrl-c.

If you do leak memory, you can regain your memory on your next reboot.
Topic archived. No new replies allowed.