Am I deleting correctly question

A

alacrite

I have a class that is interacting with some code that takes an array
in the form of pointer to the first object and size of array as
arguments: quick example void foo(*obj o, int size )

I then have a class whose internal structure can be converted to a
primative array

Note: this is an imcomplete class just an example

class X
{
public:
Obj* object; //this is the public interface for the
function that I need to interact with
//example: foo(X.object, X.size)

int objSize; //number of objects in object array
private:
Obj* convertInternalToArray(Internal i)
};

so at some point X says:
this.object = convertInternalToArray(this.internalData)

Now convertInternalToArray creates a tempObj with the keyword new and
does the conversion and returns tempObj.

That means that when convertInternalToArray is called X.object is
pointing to memory on the heap.

Question: since tempObj only has scope in the convertInternalToArray
function I cant get access to it to delete it. If I delete this.object
in the destructor am I cleaning up the memory that was created for
tempObj and then returned to this.object.

Can someone explain to me what is happening with the memory in a
situation like this?

thanks
 
R

Ron Natalie

I have a class that is interacting with some code that takes an array
in the form of pointer to the first object and size of array as
arguments: quick example void foo(*obj o, int size )

The above is syntactically invalid.
public:
Obj* object; //this is the public interface for the
function that I need to interact with
//example: foo(X.object, X.size)

int objSize; //number of objects in object array\\\
private:
Obj* convertInternalToArray(Internal i)
};

Hopefully you have public and private interchanged above, otherwise
it makes no sense at all.
so at some point X says:
this.object = convertInternalToArray(this.internalData)

this is a pointer.
Now convertInternalToArray creates a tempObj with the keyword new and
does the conversion and returns tempObj.
That means that when convertInternalToArray is called X.object is
pointing to memory on the heap.
If that is where you set it too when you did the new.
Why don't you just declare internally:
vector<Obj> internal_object;
and then you don't have to worry so much about screwing up
your allocation (and copying semantics).
Question: since tempObj only has scope in the convertInternalToArray
function I cant get access to it to delete it.

"tempObj" only holds the dynamic allocation that new returns. It
doesn't need to be deleted.
Can someone explain to me what is happening with the memory in a
situation like this?
I can't explain to you anything unless you give us a little more
complete program because your explanations are incomprehensible
and the what little code you have posted can't possibly be right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top