Archived

This topic is now archived and is closed to further replies.

using new in a class when can i use delete?

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok if I use new in a () thats not a constructor and the pointer is public, can I use delete in the deconstructor to deallocate the memory or do I need to use delete in the () that I used new? Thanks

Share this post


Link to post
Share on other sites
We don't understand what you mean by (); we assume you mean function.

Yes, you can delete the pointer anywhere it is visible.

EDIT: however, if your destructor will automatically delete the pointer, you should make sure the pointer is initialized to NULL (sorry, I mean 0) in the constructor. That way, if the function that calls new is never called, the delete won't do anything.



Don't listen to me. I've had too much coffee.

[edited by - sneftel on August 29, 2002 10:45:05 PM]

Share this post


Link to post
Share on other sites
If the pointer is not a member (i.e. it's created in a member method), you must free all memory you allocate within the function in which it was created. However, if it IS a member, you may allocate it memory in any member method and free it in the destructor, provided you don't lose the address of any dynamic memory.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on August 29, 2002 10:52:31 PM]

Share this post


Link to post
Share on other sites
Ok, I got a new problem. How do I assign the address of one pointer to another? I see a problem with this is if you use new or malloc(). If you have a pointer defined and decalred with new and try to assign a new address to it you just lost the way to delete the new memory right? So my question is in my class

class FOO
{
public:
unsigned char *p;
FOO();
~FOO();
};

//FOO.CPP
FOO::FOO()
{
p = new unsigned char(''0'');
}

FOO::~FOO()
{
delete p;
}


that sould be all fine and dandy

now if I have a file I load in some data to load a tga file. I have endianess issues ok. so I do a byte swap and with the temp pointer I have assigned the new byte order to how do I reasign this new address to the pointer p? Or is new out of the question? See I don''t understand how you can initalize a pointer without using new or malloc() unless you create a variable and assign the address of the variable to the pointer so the pointer is pointing somewhere to avoid crashes? thanks for the help.

Share this post


Link to post
Share on other sites
what are you byte swapping? not an object?

if you store your bytes in an object (TGAObj) and create it using new you will have a pointer to a TGAObj. What goes on inside the object doesn''t matter (swapping bytes etc).

if you aren''t using objects but are making lots of new ints (like in your example) then the pointer will still point to the int even if you change the value of the int, including swapping the bytes.

if you make a whole array of ints then you can swap the bytes and still have the same array address.

so there doesn''t seem to be much of a problem

Peace

Share this post


Link to post
Share on other sites