Can I delete an unnamed memory with a certain address?

M

Moonrie Aurum

I have allocated a chunk of memory in the Contour::AddCurve() method as
below:

LPTTPOLYCURVE pCur=reinterpret_cast<LPTTPOLYCURVE>(new BYTE[n]);

Later in the destructor how can I free this new BYTE[n] memory block?
For some reason, I can not remain a LPBYTE pointor like: LPBYTE p= new
BYTE[n];
Can I do it like this:
delete [] reinterpret_cast<LPBYTE>(pCur);
Help me!
class Contour
{
public:
typedef std::vector<LPTTPOLYCURVE> ContourType;

Contour(){}
~Contour()
{
typedef std::vector<LPTTPOLYCURVE>::const_iterator itr;
for(itr i=_curves.begin(); i!=_curves.end(); i++)
{
//int n=sizeof(TTPOLYCURVE)+sizeof(POINTFX)*((*i)->cpfx -1);
LPBYTE p=reinterpret_cast<LPBYTE>(*i);
delete[] p;
}
}

void AddCurve(const TTPOLYCURVE& cur)
{
int n=sizeof(TTPOLYCURVE) + sizeof(POINTFX)*(cur.cpfx -1);

memcpy(pCur, &cur, n);
_curves.push_back(pCur);
}
private:
POINTFX _pfx;
// std::vector<Segment> _seg;
std::vector<LPTTPOLYCURVE> _curves;
};
 
V

Victor Bazarov

Moonrie said:
I have allocated a chunk of memory in the Contour::AddCurve() method as
below:

LPTTPOLYCURVE pCur=reinterpret_cast<LPTTPOLYCURVE>(new BYTE[n]);

Later in the destructor how can I free this new BYTE[n] memory block?
For some reason, I can not remain a LPBYTE pointor like: LPBYTE p= new
BYTE[n];

What does it mean "I can not remain a LPBYTE pointor"? Do you mean
"retain (keep) the LPBYTE pointer"? Why do you say "for some reason"?
Do you not know the reason?
Can I do it like this:
delete [] reinterpret_cast<LPBYTE>(pCur);

Yes, that should be fine assuming that 'LPBYTE' is the type that you get
when you use 'new BYTE[n]'.
Help me!
class Contour
{
public:
typedef std::vector<LPTTPOLYCURVE> ContourType;

Contour(){}
~Contour()
{
typedef std::vector<LPTTPOLYCURVE>::const_iterator itr;
for(itr i=_curves.begin(); i!=_curves.end(); i++)
{
//int n=sizeof(TTPOLYCURVE)+sizeof(POINTFX)*((*i)->cpfx -1);
LPBYTE p=reinterpret_cast<LPBYTE>(*i);
delete[] p;
}
}

void AddCurve(const TTPOLYCURVE& cur)
{
int n=sizeof(TTPOLYCURVE) + sizeof(POINTFX)*(cur.cpfx -1);

memcpy(pCur, &cur, n);

What's "pCur" here? Did you forget to allocate it?
_curves.push_back(pCur);
}
private:
POINTFX _pfx;
// std::vector<Segment> _seg;
std::vector<LPTTPOLYCURVE> _curves;
};

V
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top