The MSVC help files say that a call to free() with an invalid pointer,
that is, one not previously associated with a call to malloc,calloc etc, may not function correctly.
However, what if a block of memory is assigned to a pointer with malloc, then the address of the memory block is hashed, and the address of the memory block is later returned to the original pointer...will free() function correctly under these conditions ?
What I need to do is this:
dynamically allocate an unknown number of memory blocks of unknown sizes, which can be retrived and accessed in an unknown order. At any time, a particular block of memory might become redundant and therefore, preferably, should be deleted (freed).
The hashed address system apears to works fine, but I cannot tell if the redundant pages are really being de-allocated by free().
Can any one help, or suggest a better method?
When you dynamically allocate memory to a pointer, you should not readjust that pointer, but rather assign a new pointer to the allocated pointer and than traverse the allocated memory:
The reason for this is due to the fact that if the allocated pointer s_name is moved, than you might have trouble freeing the memory block.
char *s_name = (char*)malloc(sizeof(char)*10);
char *p_current = s;
//do stuff with p_current
This is about as far as I know. When passing the allocated array or structure or whatever type into a function, I think that it works like an ordinary char array, but the pointer in the caller still points to the beginning of the allocated memory so it is safe to mess with the parameter passed into the called function. Not 100% sure about this though.
> However, what if a block of memory is assigned to a pointer with malloc, then the address of the memory block is hashed, and the address of the memory block is later returned to the original pointer...will free() function correctly under these conditions ?
A pointer is a number. This number represents a memory address. So long as you pass the address that you received from malloc to free, your block will be freed. It doesn't matter if you hashed it, added 73 to it or whatever in the middle - it's a number, not a magic stone.
The debug libraries of your compiler should trap invalid calls to free(), in any event.
Yeah basically don't write code like this
while(curr != NULL)
curr = curr->next;
thanks for the advice.
but Esss, the problem is, how does the free() function know how much memory to return to the heap?
I am presuming the size of the block is recorded somewhere when memory is allocated, or am i missing something?
When you allocate with malloc(), extra bytes are used for bookkeeping. free() will then use this information to release the correct amount of memory.
The size of the block is specified when you use malloc, it is SIZE which could represent any int.
char *mptr gets an address but it also knows how much space is reserved as in a block of addresses. This is likely a lower level characteristic of a pointer, that is, to know its size. When you use free you only need specify the address recieved by char*mptr. You can not move char *mptr to the middle of the block and use free, it has to be at the address returned by malloc.
#DEFINE SIZE 10;
char *mptr = (char*) malloc (sizeof(char) * SIZE);
int size = 10;
char *mptr = (char*) malloc(sizeof(char) * size);
char *mptr = (char*)malloc(sizeof(char) * 10);
You could however do this:
//assign a new pointer to address returned by malloc
char *freep = mptr;
//move original pointer
//free allocated block
thanks, thats excellent news for my program :)
The thing i was unsure about was whether the book-keeping
information was stored relative to the memory block, or relative to
the pointer used with malloc, if it had bee the latter case then there would be a problem freeing memory using its address only.