Archived

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

This topic is 5679 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
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
quote:
Original post by Anonymous Poster
Your
for( index=0; index<0xFF; ++index )    

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



_johan's version is perfectly fine (and I would actually prefer his method (the reason is below)). In this context ++index and index++ will have exactly the same effect (since the value of the expression isn't directly used), but on a stupid non-optimizing compiler ++index will be more efficient (though I think that most compilers would produce the same code for them). I believe it's important to get used to using prefix-increment whenever possible, because it will have a positive effect on performance when the 'index' is something more complex than a simple integer (such as an iterator), and if you're used to it, well it's just easier to remember. Only use postfix-increment when you actually need that functionality.

[EDIT: speling]

[edited by - Dactylos on June 30, 2002 12:13:21 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What are you using to store the handles to your trees?

If you only have one tree, then where are you storing the handle to that tree outside of that function?

The way I am reading your function is that you create a new node everytime you run that function and that new node is Lost. The only thing that your program would create is memory leaks.

I would store either a pointer to or a HUFFNODE itself outside of the function where you insert nodes.

Are does this function insert NODE TREEs?

Share this post


Link to post
Share on other sites
Like I said, I didn''t paste everything. The nodes are stored in a class member variable. It inserts nodes - they might have children, they might not.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Dactylos
_johan''s version is perfectly fine (and I would actually prefer his method (the reason is below)).


The difference is seen on the MSVC compiler. If I were near my computer, I would give you a code example.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What code are you using to traverse your Nodes and what method are you using to count how many leaves there are?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you inadvertantly try to add a node below a node that hasn''t been allocated yet, or try to access any members of a node that hasn''t been allocated yet, you would get an access violation.

Share this post


Link to post
Share on other sites
I don''t believe the tree nor the list has anything to do with this... the problem occurs at allocation of a struct - new returns 0 because of some kind of error in the memory... and that''s what I can''t figure out

Share this post


Link to post
Share on other sites
PHUFFNODE pNewNode = new HUFFNODE;

If you replace this with this, is the error the same ?

HUFFNODE* pNewNode = new HUFFNODE;

(I would imagine so, but then......)


I''m assuming PHUFFNODE is typedef''d to HUFFNODE* (in which case this post is useless).

,Jay

Share this post


Link to post
Share on other sites
What do you do with pNewNode after that line ? Is it not a local var with scope only to the end of the function. Do you return it or assign it to something.

,Jay

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What Loop?

Actually just try the ''++'' at the end of your variable.

Share this post


Link to post
Share on other sites
Lol, that won''t make any difference. In case you didn''t know, ++ before the variable means to increase the value before the expression is evaluated, ++ after the variable means to increase the value after the expression has been evaluated.

Share this post


Link to post
Share on other sites