#Destructor deletes reference items after function scope ends
1076 messages · Page 2 of 2 (latest)
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
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
so in effect, that's kind of like passing the pointed-to thing by reference
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
can you show code for what you mean?
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
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
(btw don't worry about the fact that creating a temporary pointer and then copying it maybe sounds expensive, it's not)
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
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
return {*root, encoded_result};
I'm returning it's value ehre
here*
so I can't delete can I?
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
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
I would have to put it inside a variable
and then delete it
delete doesn't actually affect the pointer itself
it just destroys the object that the pointer points to, and then gives back the memory
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
yes yes but I have to call it before the end of the function
I can't free it before I return it
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
exactly
no but you can delete the returned pointer in the calling function
I can't be in the fucntion anymore
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