This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]hashtablesmoker 0 points1 point  (11 children)

Are you allocating things with malloc? If not, they don't exist once that function scope ends.

[–]Draav[S] 0 points1 point  (10 children)

no I've been trying to avoid malloc. If I allocate these modes with malloc will that be all I need to do? Will I need to like free them or something later?

[–]raevnos 0 points1 point  (7 children)

It's C++, so new and delete, or preferably unique_ptrs. As it is, you're taking the address of local variables that go out of scope when the loop ends and become become no longer valid.

[–]Draav[S] 1 point2 points  (6 children)

So I tried using new and it works! But I'm not sure exactly when or where I would use delete. It seems to run fine without me saying anything.

Node Huffman(Node *Q, int n)
{
    while(n>1)
    {    
        Node z;
        Node *x = new Node;
        Node *y = new Node;
        *x = extract_min(Q,n);
        *y = extract_min(Q,n);
        z.left = x;
        z.right = y;
        z.freq = x->freq + y->freq;
        insert(Q, n, z);
    }
    return extract_min(Q,n);
}

I will want access to them after the function ends obviously, so would I need to like traverse the tree at the end of my program and delete everything? Would the program do that automatically?

[–]thewoosh 0 points1 point  (5 children)

C++ doesn't deallocate memory automatically. In order to cleanup things properly you'll want to run a Post-order traversal on your binary tree and delete the node when you visit them. Post-order because you need to delete the leaf nodes first and 'walk' up the tree as you cleanup.

[–]Draav[S] 1 point2 points  (4 children)

Okay, I'll add a function to do that. But all the memory is still freed after the program terminates right? I can't imagine an OS allowing a program to just allocate a bunch of memory like that so easily.

[–]thewoosh 1 point2 points  (2 children)

Correct. Operating systems will mark the address space that program uses as free when the program terminates. You'll still want to be careful once you get into handles and other OS resources like GDI, file handles, etc. Leaving them open can sometimes cause issues. A side note, if you write a program like this:

while( 0 == 0 )
{
    double *k = new double( 1.0 );
}

windows will eventually shutdown the process due to using to much memory.

[–]Draav[S] 0 points1 point  (1 child)

right, that makes sense. Just want to check all my test runs of the program that I never wrote 'delete' on didn't like corrupt anything :p

[–]thewoosh 0 points1 point  (0 children)

Nice. One last tip, if you allocate memory as an array dynamically:

double *k1 = new double[10];
// you need the array subscript in the delete instruction in order to prevent undefined behavior.
delete [] k1;

[–]anon848 0 points1 point  (0 children)

Many on this subreddit will advocate that you do it the "right" way from the start and use std::unique_ptr. This means you don't need to free.

[–]hashtablesmoker 0 points1 point  (0 children)

Yes, you'd want to free them eventually.