I would like to know whether the dynamic memory pointer is still available even after freeing the allocated memory. If I want to allocate and deallocate so many times within a loop , I receive memory corruption. Pls help me how I should do.If I call "free" more than one time, it reports error because allocated memory is already freed even though there is still available. I don't understand this concept . Pls help me.The program is as follows.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int i = 0;
unsigned char* temp = NULL;
unsigned char st[] = "Hello";
for ( i = 0;i < 10;i++)
{
temp = (unsigned char*) malloc( strlen(st) );
memcpy( temp, st, strlen(st));
if (temp)
free(temp);
if (temp)
printf("still available");
else
printf("already destroyed");
if (temp)
free(temp);
}
}

Dani AI

Generated

Short answer: once you call free the memory block is returned to the system and the pointer becomes a dangling pointer. The pointer variable itself is not changed by free, so testing if (ptr) after free does not tell you whether the memory is valid. That explains the corruption and the error when free is called a second time. As suggested, set the pointer to NULL after freeing; as noted, you must assign the pointer to valid owned memory before dereferencing it.

Two concrete mistakes in the posted loop make things worse: the code allocates exactly the string length but does not account for the terminating NUL byte, and it calls free twice on the same non-NULL value. The double-free is undefined behavior and can corrupt the heap. Also always check malloc's return before use and avoid casting malloc in plain C.

A safe pattern (conceptually) is:

  • allocate the correct size (include room for '\0' if copying strings),
  • check the returned pointer,
  • use the memory,
  • call free once,
  • immediately set the pointer to NULL.

Example idiom:

p = malloc(size);
if (!p) { /* handle error */ }
...
free(p);
p = NULL;

To find these bugs quickly, run the program under a memory checker such as Valgrind or compile with AddressSanitizer; they will point to double frees, use-after-free, and out-of-bounds copies. For the defined behavior of free and details, see the C library documentation (for example, free on cppreference) and the Valgrind home page (Valgrind).

Recommended Answers

All 2 Replies

when you "free" memory, you are deleting the object which "temp" points to, you are not modifying temp itself. What you are left with is a dangling pointer which points to some undefined part of memory, which could contain absolutely anything. you should assign the value of NULL to "temp" after freeing the memory.

>I would like to know whether the dynamic memory pointer is still
>available even after freeing the allocated memory.
No, it's not. When you free memory, you must reassign the pointer to memory that you own before you can dereference it again.

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.