#Destructor deletes reference items after function scope ends

1076 messages · Page 2 of 2 (latest)

swift abyss
#

but my problem isn't in pointers

#

it's how they pass into functions

#

it's basically their behaviours outside of the main code

#

it usually pisses me off

#

but yeah that's what makes C++ so hard

#

and generally any C++ dev can go from X to Y imo

idle isle
#

passing a pointer is not actually special

#

passing anything by value just copies it

#

so passing a pointer to a function copies the pointer

#

and copying a pointer doesn't affect the thing it's pointing to

#

which means the copied pointer simply points to the same thing as the original pointer

swift abyss
#

but then

#

I have a question

idle isle
#

so in effect, that's kind of like passing the pointed-to thing by reference

swift abyss
#

if we do a permutation function

#

where we pass pointers as params

#

why does effectively change x and y

#

since it would create copies of the pointers

idle isle
#

can you show code for what you mean?

swift abyss
#
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
idle isle
#

right, so as you can see you dereference a and b

#

you never change a and b themselves

#

a and b are the pointers, which are copies of the pointers you passed in

#

for example, if you called it as ```c++
int i = 1;
int j = 42;
swap(&i, &j);

#

then &i would create a temporary pointer that pointed to i

#

same for &j

#

and then those pointers get copied into swap, where they are called a and b

#

and then when you dereference a in swap, you get a reference to i, and same for b and j

#

so *a = *b actually assigns the value of j to i

idle isle
swift abyss
#

clear

#

but I think it really needs too much focus

#

one small mistake and it's all going in the dirt

#
==3948096==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7f095b6b94c8 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x5626c3b735a5 in utils::WeightedBinaryTree<unsigned long, char>::complex(utils::WeightedBinaryTree<unsigned long, char>*, utils::WeightedBinaryTree<unsigned long, char>*) /src/utils.hpp:54
    #2 0x5626c3b6dbab in encoding::lossless::Huffman<char>::encode(std::vector<char, std::allocator<char> > const&, bool) /export/home/users/ingenieurs/info2/e12413655/DM_C++/src/encoding_lossless.hpp:79
    #3 0x5626c3b6796c in main /src/test_2.cpp:10
    #4 0x7f095b446249 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: 32 byte(s) leaked in 1 allocation(s).
#

final issue

idle isle
#

at the end of the program, you need to delete the root node

#

and that should then delete its children in the destructor, which in turn delete their children, and so on

swift abyss
#
return {*root, encoded_result};
#

I'm returning it's value ehre

#

here*

#

so I can't delete can I?

idle isle
#

nope, but at some point you're done with dealing with the tree

#

like, when you return from main you don't need it any more

#

so delete it once you're done using it

swift abyss
#

that's the thing

#

I'm passing the value of the pointer

#

not the pointer itself

#

so I have to delete it before I leave the function

#

since I wouldn't have it's address anymore outside

swift abyss
#

and then delete it

idle isle
#

delete doesn't actually affect the pointer itself

#

it just destroys the object that the pointer points to, and then gives back the memory

swift abyss
#

yes but I don't have the access to the pointer

#

in my main code

idle isle
#

you're creating it at some point

#

like, there's a variable called Tree* root = ...; or something

#

and this variable goes out of scope at some point

#

that's where you should call delete

#

alternatively, use a std::unique_ptr

swift abyss
#

yes yes but I have to call it before the end of the function

swift abyss
idle isle
#

if you return it from a function, that means the calling function uses it

#

and at some point, that function is going to stop using it

swift abyss
#

exactly

idle isle
#

that's when you should delete it

#

when there is no one left who's using it

swift abyss
#

I can't delete it if I'm returning it

#

because after the return statement

idle isle
#

no but you can delete the returned pointer in the calling function

swift abyss
#

I can't be in the fucntion anymore

idle isle
#

you return the pointer somewhere, to some other function

#

now that function has the responsibility for deleting the pointer

#

we also say that it owns the tree that the pointer points to

#

and it can either pass on that ownership to another function, such as by returning the pointer, or it has to delete it when it's no longer needed

#

std::unique_ptr automates this, so that you can't forget the delete