-2

I have a pointer array of Word (objects) and I have to assign another object of type Word to this objects array.

Using this two lines of code I put the new object w inside my objects array (word).

 Word w = Word(new_word, len);
 this->word[index - 1] = w;

Then I print my objects array and everything comes out right

for (int k = 0; k < this->len; k++) {
cout << this->word[k].getChars() << endl;
} // End of function 1

After "End of function" we return to the main class that call to another function.

This function print the objects array again but now the function does not print the w object that I insert in the previous function.

Second function

for (int k = 0; k < this->len; k++) {
    cout << this->word[k].getChars() << endl;
} // End of function 2

Can anyone explain to me why this is happening and how it can be arranged.

0xcode
  • 9
  • 4
    Show a [mcve]. My guess would be that `Word` constructor stores a pointer to local variable, and ends up with a dangling pointer once the function returns. But rather than guessing, you should show the actual definition of `Word`. – Igor Tandetnik Aug 12 '19 at 16:54
  • Please post a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). This is just not enough code to see what's wrong... – jan.sende Aug 12 '19 at 16:55
  • 2
    Could be a [Rule of Three violation](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). I join the others in requesting a [mcve]. – user4581301 Aug 12 '19 at 17:04

1 Answers1

1

Though it's difficult to be certain (since we don't have the rest of the function to look at), it seems that you may have a dangling pointer issue.

When you declare Word w = Word(new_word, len); in your function, you're declaring it as a local variable, placing it on the stack. Adding this to the array doesn't cause any issues when you're still in the function, but once you return from where you came, the function's memory - including Word w - is destroyed. When you try to access that memory location again by printing it from the array, you're looking for a variable that no longer exists, and thus get undefined behavior.

Luckily, you're using c++, and heap memory management is fairly well supported! I would consider implementing word as an array of pointers. If you then try something like this...

Word *w = new Word(new_word, len);    //use "new" to create an object on the heap - persistent after you leave the function!
this->word[index - 1] = w;            //make sure this->word is now an array of Word*; it seems to currently be an array of Word

...you may find the problem solved. Just don't forget to free it when you're done!

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • Pedantic note: You're not likely to ever see an implementation that doesn't use them, but C++ doesn't require stacks and heaps. If someone can figure out how to implement Automatic and Dynamic memory with fairy dust and Care Bear fluff, that's valid C++. – user4581301 Aug 12 '19 at 19:26
  • @user4581301 And thanks to that curiosity-piquing comment, I've learned what [Automatic and Dynamic](https://stackoverflow.com/a/9182244/7431860) memory is. Developers who read posts with `-Wpedantic` on are more helpful than most people realize! – Nick Reed Aug 12 '19 at 19:32