Author |
Message |
<- Chameleon -> #1 / 9
|
 malloc - free question
When I reserve memory with malloc, standard C has anywhere the size of reserved memory or it is compiler specific? Because I believe it is impossible to reserve memory only with a pointer...
|
Wed, 28 Sep 2005 04:58:36 GMT |
|
 |
Mark A. Odel #2 / 9
|
 malloc - free question
Quote: > When I reserve memory with malloc, standard C has anywhere the size of > reserved memory or it is compiler specific? > Because I believe it is impossible to reserve memory only with a > pointer...
Compiler specific. You cannot reserve memory with only a pointer so the implementation must come up with some scheme to make malloc/free work.
|
Wed, 28 Sep 2005 05:01:17 GMT |
|
 |
E. Gibbo #3 / 9
|
 malloc - free question
Quote:
>> When I reserve memory with malloc, standard C has anywhere the size of >> reserved memory or it is compiler specific? >> Because I believe it is impossible to reserve memory only with a >> pointer... >Compiler specific. You cannot reserve memory with only a pointer so the >implementation must come up with some scheme to make malloc/free work.
Every implementation I know of, stores the size somewhere, often in the same block of memory that the allocation is taken from. Sometimes the bytes at the addresses just before the returned pointer (often, four bytes) will be found to contain the size of the allocation, or a pointer to its end, or other similar data that allows the allocator to tell how big the block is. But this is not strictly necessary, it is an implementation detail. An implementation could, conceivably, manage allocation just using a single "allocated" bit per block. E.g., all blocks might be the same size in reality, such as 1024 bytes (and attempts to allocate more would fail). --Ben --
|
Wed, 28 Sep 2005 07:28:18 GMT |
|
 |
Dav #4 / 9
|
 malloc - free question
Quote: > When I reserve memory with malloc, standard C has anywhere the size of > reserved memory or it is compiler specific? > Because I believe it is impossible to reserve memory only with a pointer...
What are the best orderly ways of keeping track of this yourself?
|
Thu, 29 Sep 2005 00:27:45 GMT |
|
 |
Friedrich Dominicu #5 / 9
|
 malloc - free question
Quote:
> > When I reserve memory with malloc, standard C has anywhere the size of > > reserved memory or it is compiler specific? > > Because I believe it is impossible to reserve memory only with a pointer... > What are the best orderly ways of keeping track of this yourself?
Write wrapper around the allocations. There are plenty of examples, for a variety of reasons, check e.g dmalloc or or. Regards Friedrich
|
Thu, 29 Sep 2005 01:03:01 GMT |
|
 |
Michael Wojci #6 / 9
|
 malloc - free question
>> You cannot reserve memory with only a pointer... > But this is not strictly necessary, it is an implementation detail. > An implementation could, conceivably, manage allocation just using a > single "allocated" bit per block. There's nothing I can see in the standard to stop the implementation from including the size of the area in its pointer types. Or, for that matter, the time when the memory was allocated, the source file and line of the malloc/calloc/realloc call, and whatever else it likes. (And yes, there are implementations that blah blah etc.) An implementation with address space to burn could also assign various memory block sizes to ranges of address space, and deduce the size from the pointer value. That would handle the length issue and still provide a "pure" pointer representation, ie one where a pointer value was a pure unsigned binary representation of an offset into machine address space. At some level the implementation would have to keep some state so that it would know how to satisfy the next allocation request, so in that sweeping sense Mark's statement holds true. -- Michael Wojcik
|
Thu, 29 Sep 2005 00:04:00 GMT |
|
 |
Dan P #7 / 9
|
 malloc - free question
Quote:
>> When I reserve memory with malloc, standard C has anywhere the size of >> reserved memory or it is compiler specific? >> Because I believe it is impossible to reserve memory only with a pointer... >What are the best orderly ways of keeping track of this yourself?
Associate a size_t object to each pointer initialised via malloc and friends. Store the size of the allocated block in it. However, I have yet to see a case where this is needed for each and every dynamically allocated pointer (unless you're developping your own dynamical allocation system). Dan -- Dan Pop DESY Zeuthen, RZ group
|
Fri, 30 Sep 2005 19:36:04 GMT |
|
 |
Brand Hu #8 / 9
|
 malloc - free question
There is no way to get the size of a memory block from the allocator that is standard across all platforms/compilers/allocators. The most common ways are either using HeapSize (on Windows) or _msize (on multiple flavours of Unix.) -- Brand Turbo charge your C/C++ apps - without changing code! http://www.roguewave.com/ATSevan01
Quote: > When I reserve memory with malloc, standard C has anywhere the size of > reserved memory or it is compiler specific? > Because I believe it is impossible to reserve memory only with a pointer...
|
Sat, 01 Oct 2005 01:23:13 GMT |
|
 |
E. Gibbo #9 / 9
|
 malloc - free question
Quote:
> >> You cannot reserve memory with only a pointer... > > An implementation could, conceivably, manage allocation just using a > > single "allocated" bit per block. >At some level the implementation would have to keep some state so that >it would know how to satisfy the next allocation request, so in that >sweeping sense Mark's statement holds true.
Well, no it doesn't hold true. The implementation does have to keep some state, I agree, but his statement does not preclude that: specifically, you *can* "reserve memory only with a pointer" (if the implementation keeps track of all pointers it returns, perhaps in a linked list, then it has enough information to free them all if all blocks are the same size (*)). Indeed, reserving fixed-size blocks "only" with a pointer would be wasteful: less state than that is needed; as I mentioned, one bit per block will do it. So you can reserve memory with only a pointer, and in fact you can reserve it with only a single bit, but at least with the types of allocation we have been discussing, I don't see how to do it with no state at all. --Ben (*) Or at least, if the value of the pointer is sufficient to know how big its associated block is: as you pointed out, one might wish to have different block sizes available (but still only a fixed collection of sizes to choose from), located in different regions so you could tell by the pointer values... kinda like classes of IP network addresses. This enhancement occurred to me, too, but it seemed unnecessary to introduce the complexity to the discussion. --
|
Sat, 01 Oct 2005 10:59:09 GMT |
|
|
|