Whats going onto the stack here?
- From: "Callum Winter" <Spam not required>
- Date: Mon, 5 Mar 2007 17:57:24 -0000
Hi all,
Ok, hopefully theres a quick answer to this one. Ive just changed some code
to use a linked list of objects instead of a heap array, however I get a
stack overflow when 2151 of these objects are recursivly deleted. Im not
actually sure how the stack is filling up so quick when all the objects in
the list, I assume, are allocated on the heap with 'new'. Im also guessing
each must create its own stack as i can quite happily create 2 or more lists
of 2150 objects but not one of them can be over that amount.
Anyway if anyone can shed some light on whats actually being put onto the
stack in this case as arrays arnt really a useful option for me, especially
considering this list will always be changing, and shifting big blocks of
memory around as im resizing memory is way too slow.
Heres the structure layout of a node in the link list.
struct NODE
{
110 bytes of other data;
NODE *pPrev;
NODE *pNext;
NODE()
{
pPrev = pNext = NULL;
}
~NODE()
{
pPrev = NULL;
if (pNext)
delete pNext;
pNext = NULL;
}
};
OK, so i now have a root pointer for the list in a structure elsewhere
NODE *pNodeRoot = NULL;
and add to the list using this add function which is a member function of
the structure the root pointer is in
void Add(NODE *pNode)
{
pNode->pNext = pNodeRoot;
if (pNodeRoot)
pNodeRoot->pPrev = pNode;
pNodeRoot = pNode;
}
Ok thats that; now i can happily create 2151 objects like this elsewhere
NODE *pNode = NULL;
for (WORD i = 0; i < 2151; ++i) {
pNode = new NODE();
Add(pNode);
}
Now this is fine no problems when the list is created but as soon as i do
if (pNodeRoot)
delete(pNodeRoot);
pNodeRoot = NULL;
I get stack overflow after its deleted 2150 of the objects. Sigh!.
Im obviously missing some important point about link list sizes based on
what its putting on the stack, but what?
Should I be creating multiple smaller lists rather than one big one?
Is there a fast way to evaluate a best size for a linked list based on whats
in it, or should I allocate a fixed heap array of free NODE pointers and use
that to list all my heap objects, allowing me to avoid recursive delete,
probably cutting down on stack use.
Just looking for the best way to avoid this issue.
Thanks in advance
Callum.
.
- Follow-Ups:
- Re: Whats going onto the stack here?
- From: Igor Tandetnik
- Re: Whats going onto the stack here?
- Prev by Date: Re: Critical section vs. other sync mechanisms
- Next by Date: Re: Whats going onto the stack here?
- Previous by thread: open a large file in win64
- Next by thread: Re: Whats going onto the stack here?
- Index(es):
Relevant Pages
|