Dynamically Allocate Memory without Malloc
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()
.
Related videos on Youtube
Sam Welch
Updated on September 15, 2022Comments
-
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 over 10 yearsSet up a honking big array of
unsigned char
with static storage extent (i.e., declared at file scope or with the keywordstatic
) as your "heap", then build some kind of data structure that indexes into it.
-
-
aks over 10 yearsThese 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 over 10 yearsThe problem with malloc's speed is the system calls. They have too much overhead.
-
dsolimano over 10 yearsYou 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 over 10 yearsThere 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 about 3 yearsthe provided link here is DEAD!
-
Spaceship222 almost 3 years@ssp4all updated link