Skip to main content
gamedev.net
🔒 Locked

std::vector memory allocation

Started by vicviper 3 replies 1.6k views
Original Post
vicviper
vicviper
hi!, I've just got stucked in some memory problems, and they might be related to misuse of vector memory allocation. What I'm doing is this: class MYCLASS { std::vector <DWORD> MyVector; MYCLASS() { MyVector.resize(50000); } }; int main(. . .) { MYCLASS myc; myc.usage... } The problem is: as far as I understand, std::vector uses 'New' as an internal memory allocator, so, when the MYCLASS is allocated in the stack, it calls it constructor, which calls the constructor of the vector (which in theory, should call new to allocate the array in the heap) But what happens is that the program runs out of (stack) memory fast, so that's making me think that the full 50000 array is also allocated in the stack. In practice, I'm storing a structure instead of DWORDS, which might be enough to deplete the stack, but very far to eat all the heat memory. any help?
Agony
Agony
new allocates memory on the heap. Your class, the way your example is written, is allocated on the stack. And the heap shouldn't be running out of space anytime soon. It should be able to grow to 2 gigabytes, assuming you have the proper amount of RAM or have sufficiently large swap files. How are you running out of memory? Is new returning NULL or throwing an exception or something, later on in your program? If you have a stack overflow, then it's something entirely unrelated to std::vector. Probably incorrect or overly deep recursion, or an obsessively large array on the stack, like maybe a global array.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
silvermace
silvermace
show us the struct the vector is holding, and roughly how many of these are you allocating ?
vicviper
vicviper
the struct I'm setting in the vector has 28 DWORDs on it, and the allocation (vector.resize) fails with as few as 200.... but if I try a low number (10) the program works, but this is not very important, I have lots of other vectos allocated this way arround, it just happens that the one failing is this one

btw, on a w32 app, how can I see the usage of the heap and stack?

[Edited by - vicviper on February 21, 2005 6:41:03 PM]
silvermace
silvermace
a 28 DWORD struct at 50,000 allocations is only around 5.2MB of memory.

check to make sure that new isn't failing. it throws an std::bad_alloc, so just setup a try {} catch() {} block. What exactly is the error? is it the good ol' 0x00000005 ?

i am inclined to suggest that this has nothing to do with your allocation per se and you're more than likely writing into an invalid region. What are your structs constructors/destructors? Does the struct have any pointers or virtual functions?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.