Is necessary deallocate memory of pointer-to-pointer?

Example:

my_type_t **example;

example = (my_type_t **) calloc(10, sizeof(my_type_t *));

for (i = 0; i < 10; i++)
{
   example[i] = (my_type_t *) malloc(sizeof(my_type_t));
}

// ...

free(example); // it enough? 

// Or is necessary do this...
for (i = 0; i < 10; i++)
{
   free(example[i]);
}

One more question...
What's the vantage of set NULL to a pointer freed?

Thank you.

Recommended Answers

All 3 Replies

The general rule is, for every time you malloc you have to call free..

Why set a pointer to NULL? Because if you call free on a NULL set pointer nothing happens, also its easy to check if a pointer is NULL by examining its value..

int *ptr = (int*)NULL;

if (!ptr)/*check if pointer is NULL*/
{
/*do something*/
}

Pointers, like everything else in programming should be left in a known/safe state.

One more question about memory.

In my program I use several time the malloc/calloc and free functions. Taking a look in the monitor system I note that, when I allocate memory the memory-counter raise (obvious), but when I use the free function the memory-counter don't change. If I allocate memory again, the freed memory is reused. Ok... However, if I allocate / free things with different sizes, the memory can keep with bits unusable. Memory leak. Right? The question: There are any method to previne memory leak? Or, there are any function that "give back" the memory to the system?

Thank You, again.

I assume your talking about a system monitor that displays your processes and its resources. To know how and what its displaying you'll have to get a look at its source code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.