If you want to allocate n bytes with malloc, does it allocate exactly n bytes, or the smallest greater or equal number of bytes that is a multiple of four? Or does it depent on which system you are running?
If you want to allocate n bytes with malloc, does it allocate exactly n bytes, or the smallest greater or equal number of bytes that is a multiple of four? Or does it depent on which system you are running?
It depends entirely on your system.
All that you know for sure is that it is at least the amount you asked for.
Debug versions for instance can allocate more dead space than necessary to detect 'off-by-1' overruns without necessarily crashing the whole program by doing something bad to a vital part of the memory pool data structures.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I want to add that you shouldn't use malloc in a C++ application. The 'malloc' way of allocating memory isn't compatible with C++ class features. Use 'new' and 'delete', it's pretty much the standard for memory allocation,
Videogame Memories!
A site dedicated to keeping videogame memories alive!
http://www.videogamememories.com/
Share your experiences with us now!
"We will game forever!"
Okey, that seems to make sense.
I'm using DevCpp and WinXP, so I guess there is some way to find out how much a malloc function in my program (not in debug mode) allocates. Is there some way to find out for other systems and other compilers, maybe in precompiling or in runtime?
Well, if you get a exception error when you run the program you know you have oversteped the boundarys of that particular type of memory. ie: the stack is full. Like Sentral said, avoid Malloc() unless you plan to learn straight C program memory managment. New a delete are safer in my view, you can set a deative pointer to NULL. When I learnt C at school, we were taught to not set a pointer to NULL, but I always do now, and have done for the past 3 years,
Practise safe computing folkes - initalize your pointers and NULL deleted ones!!
Double Helix STL
There is no way. While the MS CRT does provide a function that reports the size of a dynamic block, it reports the number of bytes you requested, not those actually allocated.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Ok, thanks. The thing was that I wanted to make my own vector class. So that when the memory I requested gets full, I will have to request for another larger block of memory. I figured it maybe would be smart to request for a multiple of 4 or 8 bytes. However I can do that anyway. You never know.
>> The thing was that I wanted to make my own vector class.
Hopefully just as a learning exercise. It is highly unlikely that creating your own is a good idea for any other reason.
>> I figured it maybe would be smart to request for a multiple of 4 or 8 bytes.
It is. In fact, if you do some research on the platform you are targeting, you might find specific information on what values are good to use. There are also general guidelines that are good as well. For example, if you were writing a pool allocator you might want to allocate chunks that fit on a page in memory.
In general, since many objects stored in a container are multiples of 4 bytes on 32 bit machines, you probably don't have to worry about that too much. Also, note that many vector implementations double the allocated space each time more space is needed, which provides a nice power of two size.
>> Hopefully just as a learning exercise. It is highly unlikely that creating your own is a good idea for any other reason.
Yes, only as an exercise.