Implement malloc()

This is a discussion on Implement malloc() within the C Programming forums, part of the General Programming Boards category; Hi, Can anyone suggest any idea or some C code to implement malloc function. The point is how will you ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Implement malloc()

    Hi,
    Can anyone suggest any idea or some C code to implement malloc function. The point is how will you find the amount of memory present in the heap and allocate it.
    Please try to explain the code as I am sure it would be complicated.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    Dynamic 2D array of longs:
    Code:
    // Malloc returns a pointer to the beginning of the array you just initialised
    // (long*) is a typecast telling the compiler that malloc is returning a pointer to an array of longs
    long *pointer = (long*)malloc(length * width * sizeof(long));
    
    if (pointer == NULL) {
        // Do something error-like (malloc returns null if failed to allocate memory)
    }
    
    //From now on we can use it like a 1-dimensional array, use this formula to write a 2D position:
    pointer[(y * width) + x] = value;
    
    // When done, you MUST do this:
    free(pointer);
    - Tigs

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    Hi tigs,
    Thanks for responding. However I think you didnt get my question. I want to know how is the malloc function actually written in the system library. I mean what would be the contents/definition of the function. How would the function figure out how much heap memory is remaining and how would it allocate it.
    Thanks.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by rahuls
    Hi tigs,
    Thanks for responding. However I think you didnt get my question. I want to know how is the malloc function actually written in the system library. I mean what would be the contents/definition of the function. How would the function figure out how much heap memory is remaining and how would it allocate it.
    Thanks.
    We are not allowed to know that...
    It's up to each compiler.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The point is how will you find the amount of memory present in the heap and allocate it.
    You would use a system level function or other implementation/system dependent method to acquire core memory from the free store. malloc is one of the functions that will probably be wildly different for every system.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    Here's how GNU did it.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM
  5. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM

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