0

If I use malloc() to allocate memory and do not use free() to de-allocate the the memory. why cannot that memory be accessed by other program by just over writing the previous content

void main()
{
    int *a;            //here a is holding a junk value

    MALLOC(a,1,int);
    *a=100;            //a is holding 100

    MALLOC(a,1,int);
    *a=200;            //a is holding 200 

    /* so memory location where the value 100 is stored is inaccessible
       cannot be reused */
    //why can't we just over write that memory space when used next time
}
Inian
  • 71,145
  • 9
  • 121
  • 139
  • 1
    Never used `malloc` that way (standard syntax is `*malloc(size_t size)`, I think. That way you can't tell the program to allocate memory at the exact same location. – Art May 13 '19 at 09:08
  • If you don't use `free()` you have lost memory allocated by the first malloc and that is inaccessible to you. You're basically leaking memory. Also possible [duplicate](https://stackoverflow.com/questions/19435433/what-happens-if-i-use-malloc-twice-on-the-same-pointer-c) – Pratik Sampat May 13 '19 at 09:09
  • What does `MALLOC` do? – eerorika May 13 '19 at 10:25

2 Answers2

2

You are using a very strange macro, the allocations in standard C would read:

int * const a = malloc(sizeof *a);
if (a != NULL)
{
  *a = 100;
}

then the second allocation is done without calling free(), and overwriting the contents of a; this is very bad since it leaks the memory. That's why I declared it as * const above, to signal that the pointer variable in typical use should not be overwritten since we want to keep its value.

When your program ends, on typical modern systems, all resources used by that process will be reclaimed by the operating system and the memory will be used for something else.

The need to call free() is mostly about allocations done while your program runs, if the allocations happen repeatedly the process will just grow and grow otherwise.

unwind
  • 378,987
  • 63
  • 458
  • 590
-1

Malloc returns a pointer to an allocated block of memory.

int *a;
a= (int *) malloc(sizeof *a);

malloc, in this case, returns a pointer to a block of the size of 'a' bytes, and 'a' is set equal to that.

Now again if you do

a= (int *) malloc(sizeof *a);

You're not "using malloc on the same pointer". You're calling malloc() (which allocates space and returns a pointer to that space) and assigning its returned value to the same pointer object.

If you actually want to use "malloc with the same pointer", you might be interested in the realloc function:

int *a;
a = malloc(sizeof *a);
a = realloc(a, 10 * sizeof(a));
Nitin Jadhav
  • 177
  • 1
  • 3
  • 14
  • I think the OP's question was what happens if you don't free before allocating again on the same pointer. – Pratik Sampat May 13 '19 at 09:22
  • @Pratik Sampat That point is clearly explained by "unwind". Is there need to rewrite it? Here I tried to correct his code. – Nitin Jadhav May 13 '19 at 09:25
  • Yeah, it could be a cleaner answer if you also explain the effects of the two `malloc`s he's made on the same pointer without a `free` so that the OP can understand why it wrong rather than just this wrong, here's the right way to do things. – Pratik Sampat May 13 '19 at 09:31
  • 1
    @PratikSampat As per your suggestion tired to modify the answer. Thanks. – Nitin Jadhav May 13 '19 at 10:22