Typecasting and void pointers...

This is a discussion on Typecasting and void pointers... within the C Programming forums, part of the General Programming Boards category; I am currently working on an assignment that manages user's requests for memory. I are basically developing the procedures to ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    Typecasting and void pointers...

    I am currently working on an assignment that manages user's requests for memory. I are basically developing the procedures to replace malloc() and free(). I am supposed to maintain a free_list that holds a record of available memory blocks(obtained via sbrk()), and when a user requests a memory block, the free_list is checked for an available block, and if it isn't available, then a new block is obtained, added to the free_list, then checked again.

    This is where I am having a problem. When adding a new block to the free_list, which I am implementing as a circular linked list, I am passing the Free() function a void pointer to the newly acquired memory block.

    My question is this: how do I know how large is the memory block being pointed to?

    This is necessary to know so when my Malloc() function searches the list, this new block's size can be seen.

    Sorry for the long question, I just seem to be stuck on this point. If you want any code examples or further explanation, please let me know.

    Thanks in advance.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    IMO, the best thing you can do is create a struct that holds both the size and the void * and actually store that. This is how I implemented it for a short example in recent days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Arithmetic and Typecasting
    By pobri19 in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 12:06 AM
  2. working with void stars???
    By scarlet00014 in forum C Programming
    Replies: 13
    Last Post: 10-25-2008, 11:41 AM
  3. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 01:26 AM
  4. typecasting void *pointers
    By Eavan Hyde in forum C Programming
    Replies: 16
    Last Post: 07-03-2006, 09:33 AM
  5. Uses for Void Pointers
    By Ashes999 in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2003, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21