Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

delete pointer type user defined object

40 views
Skip to first unread message

kushal bhattacharya

unread,
Jan 14, 2017, 5:33:29 AM1/14/17
to
hi,
I have defined a class and then i create a pointer type instance of it at every iteration.After that i push that instance in a vector for later accessing the instance at a particular index.I am now confused about how to delete this pointer type instance after it is totally disposed.I came using 'delete' during destruction of the object.When i use this approach it causes some sort of segmentation fault.Is there any fool proof approach about delete this pointer instance after it is totally used from the vector without any seg faults.
Thank you,
Kushal

JiiPee

unread,
Jan 14, 2017, 6:30:23 AM1/14/17
to
A bit difficult to understand what you mean. Would help if you give a
very short example about the problem.

kushal bhattacharya

unread,
Jan 14, 2017, 7:56:08 AM1/14/17
to
for example
i have a class called Join
//now i create an instance of Join
for(int i=0;i<some_size;i++){
Join *j=new Join(with some parameter of constructor);
//after that i push it to vector of this class type(pointer type);
vector.push_back(j);
}
now if this Join instance is accessed by this vector via a particular index i want full access at those pushed instances.After all of the instances are use up (from this vector) i want to delete the pointer instances in order avoid any memory leak.I tried the conventional approach but there occurs some segmentation fault at some point.So is there any full proof method of disposing these pointer instances from the vector

Jens Thoms Toerring

unread,
Jan 14, 2017, 9:32:44 AM1/14/17
to
kushal bhattacharya <bhattachar...@gmail.com> wrote:
> On Saturday, January 14, 2017 at 4:03:29 PM UTC+5:30, kushal bhattacharya wrote:

> > I have defined a class and then i create a pointer type instance of it at
> > every iteration.After that i push that instance in a vector for later
> > accessing the instance at a particular index.I am now confused about how
> > to delete this pointer type instance after it is totally disposed.I came
> > using 'delete' during destruction of the object.When i use this approach
> > it causes some sort of segmentation fault.Is there any fool proof approach
> > about delete this pointer instance after it is totally used from the
> > vector without any seg faults.

> for example
> i have a class called Join
> //now i create an instance of Join
> for(int i=0;i<some_size;i++){
> Join *j=new Join(with some parameter of constructor);
> //after that i push it to vector of this class type(pointer type);
> vector.push_back(j);
> }

> now if this Join instance is accessed by this vector via a particular index
> i want full access at those pushed instances.After all of the instances are
> use up (from this vector) i want to delete the pointer instances in order
> avoid any memory leak.I tried the conventional approach but there occurs
> some segmentation fault at some point.So is there any full proof method of
> disposing these pointer instances from the vector

Calling 'delete' on each pointer you received from 'new' is the
way to go. If this results in a segmentation fault then there's
a bug in the code you haven't shown. And diagnosing invisible
bugs is a bit hard to do;-) Your best approach would probably
be to par down your program to the smallest possible one that
still shows the problem and post it here. While doing so you
might already figure out what's going wrong yourself.

Regareds, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

kushal bhattacharya

unread,
Jan 15, 2017, 5:39:12 AM1/15/17
to
thanx a lot :-) i will try this approach and post if there is any problem further

kushal bhattacharya

unread,
Jan 15, 2017, 5:40:39 AM1/15/17
to
but when i am storing it in vector for later use will this approach work still?

Jens Thoms Toerring

unread,
Jan 15, 2017, 3:08:22 PM1/15/17
to
kushal bhattacharya <bhattachar...@gmail.com> wrote:
> but when i am storing it in vector for later use will this approach work
> still?

It's not clear which "approach" you mean here. If you store
pointers to memory you allocated with 'new' in the array then
you have to do a 'delete on each of them once you're done with
them to avoid memory leaks. That's completely independent of
were you store them. If you instead use an array of objects
(instead of pointers to objects) then you didn't call 'new'
and thus you also don't call 'delete' - they will vanisch
together with the array. Nor do you have to deallocate me-
mory when you put objects created using std::make_unique()
into the array - you didn't call 'new' yourself, so you
neither call 'delete'.
Regards, Jens

JiiPee

unread,
Jan 15, 2017, 6:36:00 PM1/15/17
to
On 14/01/2017 12:56, kushal bhattacharya wrote:
> for example
> i have a class called Join
> //now i create an instance of Join
> for(int i=0;i<some_size;i++){
> Join *j=new Join(with some parameter of constructor);
> //after that i push it to vector of this class type(pointer type);
> vector.push_back(j);
> }


In a professional code you would want to use unique_ptr instead of raw
pointers. Then the pointer will automatically delete the object and you
dont need to worry about it.

But if for some reason you want to use raw pointers, then just loop the
array and use "delete" to delete each object:

like deleting the first object:

delete vector[0];

If I was you I would do something like:

std::vector<std::unique_ptr<Join>> myVector;

and then (if possible) use emplace_back to add the elements.

JiiPee

unread,
Jan 15, 2017, 7:02:48 PM1/15/17
to
On 15/01/2017 23:53, Stefan Ram wrote:
> JiiPee <n...@notvalid.com> writes:
>> But if for some reason you want to use raw pointers, then just loop the
>> array and use "delete" to delete each object:
> If one wants raw pointers, one still can often get by
> without new and delete. One just emplaces the new T objects
> to the end of a vector< T >, and then one can take the
> address of the new T entry (and possibly then append that
> to the end of another vector of raw pointers).
>
> Optimization: When individual entries should be deleted
> before the whole array is deleted, their destructor can
> be called and then their address can be added to a free
> list. Now, when a new T needs to be added, we will first
> check the free list and then possibly use a placement new.
>
> For many cases, not all, this will suffice.
>

But this is more work and in many cases there is no need to optimize
like this. I like the idea of unique pointer as it makes it easy... dont

jonkalb

unread,
Jan 15, 2017, 7:03:09 PM1/15/17
to
A smart pointer such as std::unique_ptr is definitely a better the way to go. There is an even better solution. Don't call new at all and just create a std::vector<Join>.

Jon

woodb...@gmail.com

unread,
Jan 16, 2017, 1:01:16 PM1/16/17
to
I think that's often good advice, but it seems to say
there's little point for std::unique_ptr. If the class
in question, in this case Join, is large enough, using
a std::unique_ptr would be a good idea.

I'm not sure how large a class has to be for it to pay
off. I think it depends on the hardware and OS. I use
a unique_ptr with a class that's size is over 170 bytes.
That may be too small to justify the use, but there's
also the possibility that the size of the class will
grow in the future.


Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

JiiPee

unread,
Jan 16, 2017, 2:12:58 PM1/16/17
to
On 16/01/2017 00:03, jonkalb wrote:
> A smart pointer such as std::unique_ptr is definitely a better the way to go. There is an even better solution. Don't call new at all and just create a std::vector<Join>.


When doing polymorphism I think pointers are needed.. so if Join is
pointer to the parent class and wants to make polylmorphic calls to its
child classes. doing

std::vector<Join>

would not allow that. So it depends....
0 new messages