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

Help on 'delete[] data' in an example

45 views
Skip to first unread message

fl

unread,
Jun 8, 2015, 6:08:29 PM6/8/15
to
Hi,

When I read a post on-line about self-assignment, I do not understand one
line:

delete[] data;


My question here is not directly related to self-assignment, but about the
function of *data and n in the class array. What use are *data and n?

Whether they are meant to an array length n, beginning address is by a
pointer data?

Please help me on the function:

array &array::operator =(const array &rhs)



Thanks,


..................................
class array {
...
int *data;
size_t n;
};


In order to "fix" it one can either add an explicit self-assignment check

array &array::operator =(const array &rhs)
{
if (&rhs != this)
{
delete[] data;

n = rhs.n;
data = new int[n];
std::copy_n(rhs.data, n, data);
}

return *this;
}

Paavo Helde

unread,
Jun 9, 2015, 1:01:00 AM6/9/15
to
fl <rxj...@gmail.com> wrote in
news:ce8bcba5-5d76-4285...@googlegroups.com:

> Hi,
>
> When I read a post on-line about self-assignment, I do not understand
> one line:
>
> delete[] data;

Everything allocated by new[] ("data = new int[n];" in your example) must
be deleted by delete[].

Beware that in real life the array-style new[] and delete[] have about zero
usage cases. These come up almost only in silly learning examples like
this.

>
> My question here is not directly related to self-assignment, but about
> the function of *data and n in the class array. What use are *data and
> n?
>
> Whether they are meant to an array length n, beginning address is by a
> pointer data?

It seems so. A poor-man reimplementation of std::vector, I guess.

hth
Paavo

woodb...@gmail.com

unread,
Jun 9, 2015, 11:06:47 AM6/9/15
to
On Tuesday, June 9, 2015 at 12:01:00 AM UTC-5, Paavo Helde wrote:
> fl <rxj...@gmail.com> wrote in
> news:ce8bcba5-5d76-4285...@googlegroups.com:
>
> > Hi,
> >
> > When I read a post on-line about self-assignment, I do not understand
> > one line:
> >
> > delete[] data;
>
> Everything allocated by new[] ("data = new int[n];" in your example) must
> be deleted by delete[].
>
> Beware that in real life the array-style new[] and delete[] have about zero
> usage cases. These come up almost only in silly learning examples like
> this.
>

I use them in my code -

http://webEbenezer.net/build_integration.html


Brian
Ebenezer Enterprises - "Free at last; free at last. Thank
G-d Almighty we are free at last." Martin Luther King Jr.

http://webEbenezer.net

Rosario19

unread,
Jun 13, 2015, 12:22:17 AM6/13/15
to
On Mon, 8 Jun 2015 15:08:12 -0700 (PDT), fl <rxj...@gmail.com> wrote:

>Hi,
>
>When I read a post on-line about self-assignment, I do not understand one
>line:
>
> delete[] data;
>
>
>My question here is not directly related to self-assignment, but about the
>function of *data and n in the class array. What use are *data and n?
>
>Whether they are meant to an array length n, beginning address is by a
>pointer data?
>
>Please help me on the function:
>
>array &array::operator =(const array &rhs)
>
>
>
>Thanks,
>
>
>..................................
>class array {
> ...
> int *data;
> size_t n;
>};

I would say above that
"data" is space for a pointer to int, and n is the size of the obj a
can point

i would say it is + useful something as

class array {
...
unsigned *d;
unsigned dlen;
unsigned dsiz;
};

"d" is space for a pointer to unsigned int, "dlen" is the number of
unsigned elements, obj pointed from"d" contain,
dsiz is the number of unsigned "d" can access [the memory "d" can use]
for minimize malloc() / free() use
0 new messages