0

I need to instansiate an object and add it to an array. This is what I currently have in a method:

Row r;
rows[count] = r;

The problem here is r is on the stack and being removed after the function exits. I quick fix is to make r static but that is bad right? What should I do? (Sorry, complete C++ noob).

Edit: Removing the deconstructor for Row fixes the problem.

7
  • do you have new/delete in your class? Commented Sep 14, 2010 at 23:57
  • The deconstructor for Row deletes the array and that is what is causing the problem. Trying to add something to an array that has been freed. Commented Sep 15, 2010 at 0:03
  • Could you please also describe the problem's symptom? It's possible that you're misdiagnosing its cause. (This is easy to do with C++!) Commented Sep 15, 2010 at 0:07
  • 6
    "Edit: Removing the deconstructor for Row fixes the problem." - no, it doesn't fix the problem, it just makes it differently broken. That destructor was there for a reason, right? To prevent memory leaks. You really, really need to read a C++ tutorial if you want to write C++. It's not a language you can get right by guessing. Commented Sep 15, 2010 at 0:09
  • @Steve Jessop, agreed. @Louis particularly needs to bone up on copy construction & assignment. Commented Sep 15, 2010 at 0:27

3 Answers 3

7

The line rows[count] = r copies the object r to the element at index count in the array. After that, it doesn't matter what happens to r, the array is unaffected.

[Edit: OK, it matters indirectly what happens to r - since the copy is using something that r can delete.]

This is surprising if you're used to (for example) Java, where an array element isn't actually an object, it's just a reference to one.

Sign up to request clarification or add additional context in comments.

7 Comments

What doesn't work? Does the class Row free anything in its destructor, and if so, does it have an operator= which copies whatever that is?
Yes Row frees an array it holds and there is no operator=.
The Row class is broken - when you copy an object you copy its pointer, but that doesn't stop the original deleting the thing that pointer points to. The difficult fix is to implement operator= and a copy constructor. The easy fix is to use a vector instead of an allocated array, so that there's nothing you need to do in the destructor of Row.
no problem. Btw, aaa is enough "aaa carp" is my "unicorn" moment.
@Louis If your class has a non-trivial destructor, it must also implement a copy constructor and an assignment operator in order to behave properly. This is called the "Rule of Three" (See: drdobbs.com/cpp/184401400 ) And it exists precisely to avoid problems like this.
|
3

Use std::vector instead of the array, provided construction of Row is not arduous:

std::vector<Row> rows;

Row r;
rows.push_back(r);

When the vector goes out of scope, the destructor ~Row() will be called for each entry.

You can access the most recent added entry using either

const Row& last = rows.back();

or

size_t count = rows.size();
const Row& last = rows[count - 1];

Comments

0

In this case, you're actually creating it on the stack, and then copying the object to the location rows[count]. Note that if you created rows as an array of Row objects, there already was a Row object at that location, created with the default constructor, that you copied over.

For various reasons, in C++ we try to use the standard library containers like std::vector and std::list. These will expand to handle the new elements you add.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.