Simple memory allocation question

Started by
13 comments, last by dave 15 years, 3 months ago
Generally speaking, if you know how much you want to allocate, allocate it up front. If you think it will fit on the stack, make it an array and if you don't think it will then allocate it dynamically, or better still use a container to do it for you.
Advertisement
Quote:Is there something else for memory allocation in C++?


No, C++ only has new.

For anything else, you need a real-time OS which will guarantee fixed time allocation or fail otherwise.

Quote:Original post by altra4u
So I have a struct and I need to allocate memory to a new instance when I create it.
Trouble is I cannot use malloc or new since they are said to use variable amounts of time for execution and I need to do it in fixed time.


The problem is following:
int main() {  Foo foo; // fixed time, right?};
Unless your OS guarantees that code that constructs foo will not be pre-empted, the "fixed" time may take 5 cycles on one run, and 20ms on another.

So long story short, if you're running multi-threaded OS, there is no fixed time. That is only possible in real-time OS, and without any kind of energy managed CPU (such as laptops usually have).
I think what he was being told is surely memory allocation in fixed time.

But this cannot work, until you have always guarenteed to have the needed memory size available in one piece (memory framentation).

secondly, your OS must always know in advance where this location is (fixed time search).


both is impossible for any OS, especially since memory is divided into pages of memory. if you search for it, and its not in the cache/current page, you need too
load the proper page (searching again). so even when trying to reading data just in common, you can get a cache miss and a load of the proper memory page containing the data you want to acces in your current block of code.

so fixed time? i say "never"...
any comments welcome!


if you want to get fast allocation, use memory pools/preallocate. also "fixed time" *COULD* mean you are NOT allowed to use construction like this:

if(a)
(
allocVariantA())
)
else
(
if(b)
(
if(c)
allocVariantC()
else if (d)
allocVariantD()
)
)

because if condition A is met, you could allocate the subvariant A of your struct faster as the other, since they include additional expressions to be evaluated.

Quote:Original post by Dave
Generally speaking, if you know how much you want to allocate, allocate it up front. If you think it will fit on the stack, make it an array and if you don't think it will then allocate it dynamically, or better still use a container to do it for you.

Creating an array and putting it on the stack-Thats what I think the answer is as they want the allocation to happen in fixed time.I can't think of anything else.I don't know about the pool allocator thing but stl entities use new/malloc under the hood which again will result in varying time for allocation.
You can't say that using new and delete uses varying time without being sure of what the scenario is. If you are running a piece of software running on a hard real time operating system, using dynamic allocation might be unsuitably slow, but still percieved as instant to the onlooker. Note also that you may not be able to allocate with new and delete on hard real time operating systems because they may not be able to page or guarantee memory availability at run time.

I think terminology has been confusing in this thread.

Also, as i think i mentioned in one of my earlier posts, if you reserve the size of the container, then only a single allocation is made unless you fill it past the reserve size. So actually, this is a pretty 'constant' allocation time.

This topic is closed to new replies.

Advertisement