For every "new", there should be "delete"

Every time you use new keyword, there should be also delete keyword. In short, the ratio 1new/1delete. Could anyone please help me imagine why? Yes, following this advice is good but may I know why? Just curious :)
Because whenever you use the keyword new to allocate memory, if you don't correspondingly delete it, the memory will never be freed.

Other programs won't be able to access it until your memory is cleared (by a system reboot).

This phenomenon is more commonly called a memory leak.
What would be the consequences if the rule was not followed?
Any memory that was allocated (but not deallocated) this way will be essentially "locked". Nothing else will be able to use it -- it's dead space until your memory is cleared.
Eventually, if the program continues to run and allocate memory without deleting it you will run out of memory and the program will crash.
Because whenever you use the keyword new to allocate memory, if you don't correspondingly delete it, the memory will never be freed.

It will be freed by the operating system when the application ends. This isn't specified by the standard, of course, but I'm not aware of any operating system where it isn't true.

> Every time you use new keyword, there should be also delete keyword.

The general rule is easier to remember:
If we acquire a resource, we must release it after we are done with using it.

The resource involved could be anything: memory from the free store, a file that is opened, a lock on a database, a socket bound to an address ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// acquire a resource
std::FILE* file = std::fopen( "myfile", "r" ) ;

// use the resource
// int c = std::fgetc(file) ;
// etc...

// release the resource
std::fclose(file) ;


// acquire a resource
int* a = new int[123] {0} ;

// use the resource
// a[0] = 9 ;
// etc...

// release the resource
delete[] a ;



When resources are acquired dynamically, doing this manually is error-prone; we may attempt to use a resource after it has been released, we may try to release a resource that had already been released, we may parasitically continue to hold on to the resource for long periods after we are done with using it, and (what is most commonly pointed out) we may not release it at all. Automating the process of managing resources makes life a lot simpler. So:

1
2
3
4
5
6
7
8
9
10
11
12
void foo()
{
     // prefer this; resource management is automated
     int a[123] {0} ;
     // the resource is tied to the scope, and is automatically released  when the scope ends
     // either via a normal return or because of a thrown exception

     // to this: resource management is our headache
     int* b = new int[123] {0} ;

    // ...
}


A few programming languages (C++, Ada and the sill-born D) support RAII - the mechanism of associating a resource with the duration in which it can be used, and then automatically releasing the resource as soon as its use is over. So in C++, we would much rather write:

1
2
3
4
5
6
7
8
std::ifstream my_file( "myfile" ) ;
std::vector<int> seq( 123 ) ;

// if we must
std::unique_ptr< int[] > array( new int[123]{0} ) ;

// if we really must
std::shared_ptr<std::FILE> c_file( std::fopen( "file2", "r" ), std::fclose ) ;
Topic archived. No new replies allowed.