Reputation: 6057
I want to initialize memory for storage that will need to be capped at 2gb. Basically, I need to allocate 2gb memory then add items to the memory, and when adding an item, make sure it will not overflow the allocated memory. I was looking at calloc()
, because the items should be stored in an array, but it only allows me to allocate the memory based of number of items and size of items. Because I do not have a specific number of items, or a specific size for items, this will not do. Is there someway I can allocate a 2gb array and have the number of items and the size of items dynamic>
Upvotes: 0
Views: 37
Reputation: 150
If you don't know the end size ahead of time, you could use a std::list or std::vector. The storage for these data types will increase as more items are added.
Since you said you also don't know the size of each item, you could use some sort of structure with a pointer and size variable to store each individual item.
Upvotes: 1