Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

_johan

new

This topic is 5930 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

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
This peace of code seams to be clear of errors!!!!

U gad to show us more stuffz man

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 nodes
for(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!

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 .

Share this post


Link to post
Share on other sites
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 ).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 [code]for( index=0; index<0xFF; ++index )

should look like this:
for( index = 0; index < 0xFF; index++ ) 


You seem to have a left and a right. Are you creating a linked list of binary leaf trees?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • 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!