memory leak(c++)

Honestly, it’s about my college home works. In an attempt to maniplate your computer memory with pointer, you have to prevent it happen(memory leak). It happen when a memory is being useless. Let’s make this simple: You have an allocated memory but nobody(I mean pointer(s)) access that address.

Look at this sample:

int *P;
//allocating an amount of memory space and save that address in pointer P
P = new int;
*P = 13;

P = new int;
*P = 25;

Look at this sample. At first you allocate some memory space and you give that memory a value. Until that time, It has no memory leak. But, after you allocate new memory space and save it in same pointer,  the previous address you have allocated isn’t accessed by any pointers. Nobody(again.. it’s pointer) Access that address, and it become a waste.

You can polish that memory leak with deallocate command after if you don’t need that address anymore.

int *P;
//allocate an amount of memory space and save that address in pointer P
P = new int;
*P = 13;

//deallocate address saved in its pointer
delete P;

P = new int;
*P = 25;

Happy to code!!

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s