Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 16th, 2005, 03:35 PM
Moonrie Aurum
Guest
 
Posts: n/a
Default Can I delete an unnamed memory with a certain address?

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;
};




  #2  
Old August 16th, 2005, 03:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
  #3  
Old August 17th, 2005, 05:05 AM
Moonrie Aurum
Guest
 
Posts: n/a
Default Re: Can I delete an unnamed memory with a certain address?

thanks for your help.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles