재형김
재형김

Reputation: 31

What happens to the momory of a char pointer when no pointer is being pointed at it?

And by that I mean something like this.

int8_t *pt1 = malloc(sizeof(int8_t) * 10);
int8_t *pt2 = malloc(sizeof(int8_t) * 10);

pt1 = pt2;

Here, pt1 has a memory assigned. But now it is being pointed at pt2. Then what happens to the memory that was assigned to pt1? And is there a way to free it even if you did not save its pointer value?

Upvotes: 2

Views: 295

Answers (6)

Ferit
Ferit

Reputation: 9717

It's called a Lost Heap-Dynamic Variable and It Causes a Memory-Leak

Lost Heap-Dynamic Variable

A lost heap-dynamic variable is an allocated heap-dynamic variable that is no longer accessible to the user program. Such variables are often called garbage, because they are not useful for their original purpose, and they also cannot be reallocated for some new use in the program. Lost heap-dynamic variables are most often created by the following sequence of operations:

  1. Pointer pt1 is set to point to a newly created heap-dynamic variable.
  2. pt1 is later set to point to another newly created heap-dynamic variable.

The first heap-dynamic variable is now inaccessible, or lost. This is sometimes called memory leakage. Memory leakage is a problem, regardless of whether the language uses implicit or explicit deallocation.

Source: Concepts of Programming Languages 10th - Robert W. Sebesta

Upvotes: 0

DineshB
DineshB

Reputation: 37

The memory which is allocated by an application, in especially Linux like system, belongs to the application as long as application is running. So whatever memory your process has allocated will be taken from application's address space and for better memory utilization, application is expected to free the block if it no longer wants to use it. So, if an application keeps allocating memory without freeing, at one point of time, system may go out of address space and process will be killed for lack of memory. But, if application is terminated without freeing any memory block, that memory block is owned by parent process and cleanup routine is run to free up that memory block.

Upvotes: 0

Muntasir
Muntasir

Reputation: 808

Your question has 2 parts:

  1. what happens to the memory that was assigned to pt1?

    The memory pt1 was pointing becomes inaccessible - although the memory is still allocated. On the other hand, both pt1 and pt2 point to the same memory location. This causes the program to occupy more memory than it needs and uses (i.e- the memory location pt1 was initially pointing - that location can't be used anymore, but still occupied by the program) - which is named as memory leak.

  2. is there a way to free it even if you did not save its pointer value?

    Before doing pt1 = pt2;, you should call the free() function like free(pt1);- which will de-allocate the memory location pointed by pt1.

Upvotes: 3

Abhishek Priyankar
Abhishek Priyankar

Reputation: 63

the memory is still occupied by the application unless until you free the memory allocation using the free() function or memory leak occurs. for above question you need to write like

  int8_t *pt1 = malloc(sizeof(int8_t) * 10);
  int8_t *pt2 = malloc(sizeof(int8_t) * 10);

  pt1 = pt2;
  free(pt2);

which will free the memory of pt2 .

In java this is taken care by Garbage Collector

Upvotes: 0

EricZhou
EricZhou

Reputation: 1

The OS will free the memory if you don't free it. BTW: shared_ptr will free the memory if no one use it. http://www.boost.org/doc/libs/1_64_0/libs/smart_ptr/shared_ptr.htm

Upvotes: -1

EnabrenTane
EnabrenTane

Reputation: 7466

The memory still belongs to your application, and you no longer have the address stored to free it. This is the root of all memory leaks.

Upvotes: 4

Related Questions