Why is my linked list working now? Vs before?

so i had this issue with this c++ linked list error was
The error is "malloc: *** error for object [address on my: pointer being freed was not allocated"
the code was https://gist.github.com/pemby/bbc90762157afb5e32bd
then I changed thiis
to this
and it seems to be working.
can someone help me understand why this is the case
Item::Item(char *name, double weight) {
     name = nullptr;
    weight = 0;
It will work as Item::setName(nullptr); because local variables are hiding member variables.
In your second code snippet you are initializating name through member initialization list and is nothidden by locals.

You could also do this->name = nullptr; in your first snippet.
Thank you. I was thinking it is because the way initialization list handles scope. So I was partly right?
Last edited on
Yes. You can do something like
Foo::Foo(int index) : index(index) 
There member variable index will be set to the value of local value (argument) index.
What to initialize in init lists will be always treated as member variable. To what might be another member variable, argument, global variable... it uses normal scoping rules.
Last edited on
Topic archived. No new replies allowed.