Jump to content
  • Advertisement
Sign in to follow this  
altra4u

Simple memory allocation question

This topic is 3527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. Is there something else for memory allocation in C++? Thanks jan

Share this post


Link to post
Share on other sites
Advertisement
For one thing, could the instances go on the stack?

It might also be possible not to create that many objects? Or short-lived objects within a tight loop?

There is also a possibility of custom allocators (memory pools and things which I haven't used so far).

However, is creating the objects really a problem. You can't just base design decisions on hearsay and start to optimize furiously upfront.

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
For one thing, could the instances go on the stack?

It might also be possible not to create that many objects? Or short-lived objects within a tight loop?

There is also a possibility of custom allocators (memory pools and things which I haven't used so far).

However, is creating the objects really a problem. You can't just base design decisions on hearsay and start to optimize furiously upfront.


Well I was thinking of the stack option as well but then I was wondering if creating the total allowed instances right from start is allowed or not.

Also I was wondering if I should create an stl vector or list.Do you know how they work under the hood?Do they take a fixed time to create?

Could you tell me more about this custom allocators thing or give me some link where I can check it out?

*Actually this is a test question and I want to give the best answer,not just from what I currently know.

Share this post


Link to post
Share on other sites
There are two key questions here:

1) Do you know how many times you are going to need to allocate this struct?

2) What is the scope of the object? What should it's lifetime be?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
There are two key questions here:

1) Do you know how many times you are going to need to allocate this struct?

2) What is the scope of the object? What should it's lifetime be?


1)This is dynamic.I have to remove the object when the character is killed by the player.
2)The scope of the object would be a data member i guess. It should be an array of the struct or a pointer pointing to an array.

Share this post


Link to post
Share on other sites
*Use a pool allocator.
*Use a std::vector. First std::vector::reserve() the max number of instances, then use vector::remove_if and vector::erase to get rid of instances. (a vector won't normally shrink in memory usage, and reserve allows you to only have the initial allocation)
+std::vector and std::list use a std::allocator to allocate memory, the default one uses new/delete for each requested allocation (each allocation isn't necessarily 1 item, but usually many items to make room for additional push_back() calls later).
std::vector keeps a block of memory where the first vector::size() items are alive and active and all items from there to vector::capacity() are allocated but inactive and awaiting construction so they can be used. (constructed using placement new() by default)

Share this post


Link to post
Share on other sites
Quote:
Original post by altra4u
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.


What kind of application is this for? If it's simply for a game, then I'd say you're seriously micro-optimizing here. You shouldn't be worrying about alternate memory allocation techniques until you've profiled your application and discovered that memory allocation is your bottleneck.

Share this post


Link to post
Share on other sites
Quote:
Original post by altra4u
and I need to do (memory allocation) in fixed time.


Prove it. (I am highly doubtful that any sane company would give a job with such a requirement, to a person who needs to post in For Beginners for help.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by altra4u
and I need to do (memory allocation) in fixed time.


Prove it. (I am highly doubtful that any sane company would give a job with such a requirement, to a person who needs to post in For Beginners for help.)


Ha ha dude why are you getting so edgy???Its not like I put it in just the beginner's forum and I don't know anything.I had a couple of answers of my own but I just wanted other people's opinion and I was posting it around where I expected some expert people's replies.
And yes it is from a job test not my personal will to scour the trenches of C++ memory allocation.(No need to prove it anyone)
Thx anyways for ur concern

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!