new

Started by
30 comments, last by _johan 21 years, 10 months ago
I can''t figure this one out... I have a function that inserts a node into a list - the first line in the function is: PHUFFNODE pNewNode = new HUFFNODE; where HUFFNODE is a structure and PHUFFNODE is a pointer to such a structure. new returns 0 for some odd reason, and GetLastError returns 998: "Invalid access to memory location.". What can I do to fix this?
Advertisement
This peace of code seams to be clear of errors!!!!

U gad to show us more stuffz man
Well ok... the function InsertNode is called like this:

DWORD dwFreqs[ 0xFF+1 ] = { 0 };
for( index=0; index<0xFF; ++index )
InsertNode( ( BYTE )index, dwFreqs[ index ], NULL, NULL );

and here''s the first part of InsertNode:

void CCompress::InsertNode( BYTE btByte, DWORD dwFreq, PHUFFNODE pLeft, PHUFFNODE pRight )
{
PHUFFNODE pNewNode = new HUFFNODE;

Both functions are part of a class called CCompress. I checked the this pointer and it seems to be fine.
Several Things cross my mind:

HUFFNODE* pNewNode;pNewNode = new HUFFNODE; 

Which is the same as

HUFFNODE NewNode;
and use the ampersand to get the pointer to it.
ie:
return &NewNode;

But I don''t think this is what you are trying to do. When I think of Nodes, I think of STL (Which I hate but recommend) and my own personal favorite:

struct Data{int height;int width;}struct HUFFNODE{int num_nodes;Data node_data;HUFFNODE* next_node;}HUFFNODE Tree[256];tree[0].num_nodes = 50;HUFFNODE *tempPointer;tempPointer = tree[0].nextNode;//Create tree brach with 50 nodesfor(int i = 0; i < tree[0].num_nodes; i++){tempPointer.next_node = new HUFFNODE;tempPointer = tempPointer.next_node;} 


Then you would have to write code to Traverse the tree using the pointer idea.

STL is much cleaner and safer. I believe it has a node container or a container that would work just fine for your purposes.

Don''t forget to delete what ever you create!
Ok - I get what you''re saying, but I don''t really understand why your code would help? The Huffnode thing is a linked list of trees btw, and nodes are inserted and removed frequently ( in case that helps )
quote:Original post by _johan
Well ok... the function InsertNode is called like this:

DWORD dwFreqs[ 0xFF+1 ] = { 0 };for( index=0; index<0xFF; ++index )  InsertNode( ( BYTE )index, dwFreqs[ index ], NULL, NULL ); 

and here''s the first part of InsertNode:

void CCompress::InsertNode( int index, DWORD dwFreq, PHUFFNODE pLeft, PHUFFNODE pRight ){ PHUFFNODE pNewNode = new HUFFNODE; 

Both functions are part of a class called CCompress. I checked the this pointer and it seems to be fine.



Well, you code still seems incomplete. Does you program have some serious memory leakes?

I would seriously look into STL containers. They automate all of what you are trying to do.
This sounds particularly dangerous:

"Which is the same as

HUFFNODE NewNode;
and use the ampersand to get the pointer to it.
ie:
return &NewNode"

First of all,
because it isn''t the same.
HUFFNODE * pn = new HUFFNODE;
dynamically allocates memory on the heap.
This memory gets freed by the next hn pointer matching
call to delete.
And HUFFNODE n; allocates a node on the stack
which gets invalid after leaving the scope of
the declaration.
So if you return the adress of n,
you actulally return a pointer to invalid
memory on the stack.

Just think about it, and it will make sense
( At least I hope so .
I didn''t paste everything since there''s a lot of code thats insignificant to my problem. AFAIK, I don''t have any memory leaks. And for some reason I dont like using STL, I like coding everything myself ( yeah, yeah, reinventing the wheel, but you learn a lot from it ).
quote:Original post by _johan
Ok - I get what you''re saying, but I don''t really understand why your code would help? The Huffnode thing is a linked list of trees btw, and nodes are inserted and removed frequently ( in case that helps )


Please answer these questions:

What are you using to store the handles to your trees? It looks almost as if you are trying to use the dwFreqs to do it. If you are, then maybe you should try this:

(HUFFNODE*) dwFreqs = new HUFFNODE;

What type is the ''index'' variable you are using? Why are you casting it to a byte? What purpose does it have in your insert node function?

Your for( index=0; index&lt;0xFF; ++index ) </pre> <br>should look like this:<br><pre>for( index = 0; index &lt; 0xFF; index++ ) </pre> <br><br>You seem to have a left and a right. Are you creating a linked list of binary leaf trees?<br>
Yes, a binary tree... the for loop has no significance - my problem is that new is returning NULL. dwFreq and dwFreqs[0..0xFF] just values ranging from 0 to 0xFF, which are put in the trees. index is double word, and it is also a variable used in the trees. Every node in the treelist has a unique byte ( 0-255 ) and a non-unique frequency. The frequencies also range from 0-255.

This topic is closed to new replies.

Advertisement