Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all.
Newbie to C. I have some questions about some of the memory allocation functions in C.
1)
This is if you are adding memory to an allocated block:
Does the realloc(ptr, NUM + 1) function automatically append addtional memory to a memory block allocated with malloc(NUM) where ptr points to the first byte of the malloac(NUM) memory block without overwriting the contents in the original block? How does realloac() use the ptr and know where that block ends so as to append more memory to the end of block?This is if you are removing memory from an allocated block:
Does the realloc(ptr, NUM - 1) function truncate or free up (clear with zeros) the memory block allocated with malloc(NUM) where ptr points to the first byte of the malloac(NUM) memory block? How does realloac() use the ptr and know where that block ends so as to remove the required memory from the end of block?2)
How does the free() function know how much memory was dynamically allocated during execution to free up? The only parameter passed to it is a pointer to the block (which I assume means the first byte of the block?) so how does it know when to stop? That pointer can be reassigned throughout execution and a lot of code can be between a malloc() call and a free() call.What I mean is if I used the malloac(NUM) function where NUM was a fixed number and ptr pointed to the first byte of the block, then later on in the program I increased that memory block by using realloc(ptr, NUM + 1), for example, how does free() "keep track of" how much memory I orginally allocated with malloc(NUM) then increased using realloc(ptr, NUM + 1) so it can safely free it when I am done with the block?
There seems to be some automatic index counter (a stack pointer or something?) hidden from the program where it keeps track of a dynamically allocated block of memory and how it is increased and/or decreased. This index counter then is used to manipulate the ptr so that the correct amount of memory is added/removed from the memory block.
I hope that I was clear in my explanation.
Regards,
whoami

Hello whoami!
What you are asking is implementation specific, meaning that as long as the functions do what the API says it does, nobody questions how it does it.
You are quite right, however, in assuming that some memory management system (maybe OS, maybe not...) keeps record of information (the starting point, length, process id, etc.) associated with each allocation. In this respect, the memory management system only needs to know the address of the start of the memory block through which it can look up the rest of it and delete the record when the block is free'd, the program terminated, or any other reason to release it.
Remember that a pointer variable only holds an address to a piece of memory, however small. Whether that particular variable still holds it later is irrelevant, the address still exists and is being kept track of. If you reallocate, the system finds a available block of the new size (which might be a just more memory at the same address) and changes it's record.
That said, if you have access to a good debugger or the sources to the OS on whatever platform you're working with, the best thing to do would be to look at the sources or 'step-through' (sorry, I was raised on Borland/MSVS IDEs :)) the code at the time the calls are made (debugging). That is, if you wish to learn how the underpinnings of the systems work. That or grab the Minix sources and dig through them - beware, they're long and boring. :) It's a small *nix distro, though, so it shouldn't be too bad.
Finally, if you do figure out how each implementation (OS, etc.) handles memory allocation, don't write any code that depends on some specific quirk. It could very well change between implementations making your code quite non-portable (bad things happen here, much debugging, coffee).
Good luck!
Elliot_009

Exactly what the GNU libc does is documented in its source code:
http://sources.redhat.com/cgi-bin/c...
and explained in its manual:
http://www.gnu.org/software/libc/ma...
I don't know if it's different for other libc/OS implementations but as you can see, GNU libc's malloc and friends do their own management of memory for your program and just use the brk and (sometimes) mmap system calls to get chunks of memory from the OS as described here:
http://www.linuxjournal.com/article...
so you don't really need to delve into the kernel source to understand what's going on as far as your program's dynamic memory allocation is concerned: OS kernel memory management is a quite orthogonal concept - at least in *nix land.

Thanks Elliot_009 and Wolfbone for the info.
I have not had formal training in programming (I had one C++ class my freshman year in college ~5 years ago) so I picked up a teach yourself C in 21 days book a month ago because I wanted to try to in fact teach myself C.
I'll look through the documentation you all pointed me too and I'll try not to dwell on the details too much and just have faith that the functions work!
Regards,
whoami

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |