Question

I have a question to ask, which occurred when reading the concept of static variables. If I create an allocated block of memory in a function, using malloc, and then the function returns to main, without having used free() on the allocated memory, will that memory block be susceptible to changes in the course of the program, or not? I mean, after I leave the function is it possible that the memory block can be overwritten by another process, while I wanted to use it and/or edit it in my way, or is it "locked" from something like that, until I free it? Is it possible for the block to be considered as free of data before I free it?

Was it helpful?

Solution

Once you malloced a certain num ber of bytes, it's going to be alive throughout your program's lifetime unless you explicitly free it.

It doesn't matter in which function you did the malloc, the memory is going to be alive for you to use anywhere in your program provided you have a valid pointer to the malloced memory.

OTHER TIPS

The C Standard specifies a storage duration called allocated (in C99 6.2.4 Storage duration of objects.) The lifetime of allocated storage is from allocation (with malloc, calloc, realloc) until deallocated (with free or realloc). So, yes, returning from a function does not invalidate allocated storage (unlike automatic storage like local variables). You can expect it to be still allocated. And you can read and write it as long as you have a valid pointer to such storage.

When memory is allocated with malloc, it stays allocated to your program as long as your program is running and as long as you do not free this memory block. So this memory block cannot be modified or overwritten by another process.

Generally, the malloced memory block cannot be overwritten by another process because the two processes reside in two different virtual address spaces, unless you share the memory block with another process in some way like this.

OK, so I think you're asking what happens in a situation like this...

#include <stdlib.h>

void myfunc( void )
{
    static void* p = malloc(BLOCK_SIZE);
    // perhaps the rest of this function uses the pointer to the allocated mem...
}

int main( int argc, char** argv )
{
    myfunc();
    // the rest of the program goes here...
}

... and asking if "the rest of the program" code could write into the memory block allocated by myfunc().

The heap would still have the memory allocated, as it doesn't know that only the code in myfunc() holds a pointer to the memory block. But it won't 'lock' it either (i.e. protect it from being written to -- there is no such concept in the C language itself.)

Due to the heap still regarding the memory block as already allocated no code that used a subsequent malloc() would get a pointer to a block that is within the one you already allocated. And no code outside of myfunc() would know the the value of the pointer p. Thus the only way any later code could end up writing to the block is 'accidentally' by somehow gaining a pointer to a location that happened to be within the memory in your block (probably due to some sort of code bug), and writing to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top