Re: Can I delete an unnamed memory with a certain address?
Moonrie Aurum wrote:[color=blue]
> 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];[/color]
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?
[color=blue]
> Can I do it like this:
> delete [] reinterpret_cast<LPBYTE>(pCur);[/color]
Yes, that should be fine assuming that 'LPBYTE' is the type that you get
when you use 'new BYTE[n]'.
[color=blue]
> 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);[/color]
What's "pCur" here? Did you forget to allocate it?
[color=blue]
> _curves.push_back(pCur);
> }
> private:
> POINTFX _pfx;
> // std::vector<Segment> _seg;
> std::vector<LPTTPOLYCURVE> _curves;
> };[/color]
V |