Thread: free() technicalities

  1. #1
    Unregistered
    Guest

    Question free() technicalities

    Hi,

    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?

    rob

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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:

    Code:
    char *s_name = (char*)malloc(sizeof(char)*10);
    char *p_current = s;
    //do stuff with p_current
    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.

    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.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Registered User Esss's Avatar
    Join Date
    Aug 2001
    Posts
    133
    > 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.
    Ess
    Like a rat in a maze who says,
    "Watch me choose my own direction"
    Are you under the illusion
    The path is winding your way?
    - Rush

  4. #4
    Unregistered
    Guest
    Yeah basically don't write code like this

    Code:
    while(curr != NULL)
    {
          free(curr);
          curr = curr->next;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    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?

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When you allocate with malloc(), extra bytes are used for bookkeeping. free() will then use this information to release the correct amount of memory.

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    The size of the block is specified when you use malloc, it is SIZE which could represent any int.
    Code:
    #DEFINE SIZE 10;
    char *mptr = (char*) malloc (sizeof(char) * SIZE);
    //integer variable
    int size = 10;
    char *mptr = (char*) malloc(sizeof(char) * size);
    //literal constant
    char *mptr = (char*)malloc(sizeof(char) * 10);
    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.

    You could however do this:
    Code:
    //assign a new pointer to address returned by malloc
    char *freep = mptr;
    //move original pointer
    mptr +=1;
    //free allocated block
    free(freep);
    Last edited by Witch_King; 09-04-2001 at 02:14 AM.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    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.

    thanks again...


    rob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM