0

I have a function similar to this:

void foo(obj ary[], int arysize) {
    for (int i = 0; i < arysize; i++)
         ary[i] = obj(i, "abc");
}

And I call it like this:

obj array[5];
foo(array, 5);

It's supposed to populate the array with my objects. However, when it returns, the objects are garbage. It works with value types like int and stuff, so I think it's something to do with the object I create being local in scope so it gets destroyed when the function returns. I would like to be able to do this without using dynamically allocated objects with new. How can I do this?

1
  • Does your obj class has memory allocation in its constructor? Commented Aug 27, 2010 at 2:46

1 Answer 1

2

That should work fine. I'd look into the assignment operator for your class.

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

1 Comment

Wow how stupid of me. The assignment operator was changing the attributes of the object but not copying the data over. Thanks.

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.