Dynamically Allocate Memory without Malloc

15,382

Solution 1

You can implement your own version of 'malloc' using system calls for process memory management... Try brk, sbrk and mmap system calls to get memory from kernel...

This has a easy to understand implementation which you can implement and improve on

http://www.inf.udec.cl/~leo/Malloc_tutorial.pdf

Solution 2

No, new is not supported in C, and setting a pointer to null does not free the associated memory. In fact that's a good way to leak memory.

It depends on the details of what you are trying to do, but typically you'd make some initial call to malloc() to get a largish block of memory, and then write your custom functions which you'd use to manage allocations from that large block within your program.

If you don't want to use malloc() at all, you'll have to use one of the memory allocation calls for your operating system. For example under Windows you might call HeapAlloc() or GlobalAlloc(). on UNIX systems you'd call brk() or sbrk().

Share:
15,382

Related videos on Youtube

Sam Welch
Author by

Sam Welch

Updated on September 15, 2022

Comments

  • Sam Welch
    Sam Welch over 1 year

    I've been given a task to dynamically manage memory to beat the speed of malloc. Some requirements:

    1) Have a pointer to a struct

    2) Use "Chunks" of memory

    3) The memory will be allocated with a call like

    init(memory * mem, int chunk_size, int num_chunks)
    

    4) The memory pointer will be declared globally.

    5) Not using system calls

    So, I've thought about having my struct simply just:

    typdef struct {
      char *byte;
    } memory;
    

    And then that would leave my init function to do something like:

    mem = new memory[chunk_size * num_chunks];
    

    I don't know if you can do that in C -- normally I would use malloc! And then to free would I be able to just be able to set the pointer to null?

    Thanks for the help!

    • John Bode
      John Bode over 10 years
      Set up a honking big array of unsigned char with static storage extent (i.e., declared at file scope or with the keyword static) as your "heap", then build some kind of data structure that indexes into it.
  • aks
    aks over 10 years
    These operations are expensive though and if you are implementing your own memory allocation scheme, you would need to take care of issues like fragmentation of allocated memory
  • Sam Welch
    Sam Welch over 10 years
    The problem with malloc's speed is the system calls. They have too much overhead.
  • dsolimano
    dsolimano over 10 years
    You have to get the memory from the OS somehow, which requires a system call. I think what Charles is suggesting is that you make only a single system call ever, so the overhead is irrelevant. Then you have your functions work within the block allocated by that single system call.
  • Charles E. Grant
    Charles E. Grant over 10 years
    There is no way to get around making at least one system call to get the initial block of memory. There is no performance impact of a single call to malloc(), you just want to avoid making repeated calls. If your memory requirements are limited you might be able to use alloca() which allocates memory on the stack, but that's non-standard, and doesn't give much room (typically a few megabytes or less).
  • ssp4all
    ssp4all about 3 years
    the provided link here is DEAD!
  • Spaceship222
    Spaceship222 almost 3 years
    @ssp4all updated link