deleting instance in array

D

Dan

hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}



Dan
 
H

Howard

Dan said:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}


What does not work? Does it crash?

How do you create each member of the array? If you use something like

array_3 = new someobjectyouhavenotshown;

then you're deleting it properly. This assumes that the array holds
pointers. Does it?

Or, are you expecting the ith item to go away, and all items beyond that to
shift up one space?

You'll need to be more specific about what the array contains, how it is
populated (i.e., how are its items created), and what you mean when you say
it doesn't allow you to do a delete.

Perhaps what you want to use is a std::vector, not an array. But without
more info, it's impossible to tell.

-Howard
 
J

John Harrison

Dan said:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

The easiest way to do this is to learn about vectors, which can be resized.
Get yourself a modern C++ book that teaches you about vectors.

john
 
V

Victor Bazarov

Dan said:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };

and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}
 
D

Dan

Victor Bazarov said:
Dan said:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?



Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.





for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}
 
J

John Harrison

Dan said:
Victor Bazarov said:
Dan said:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?



Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.

I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).

2) Are you prepared to use a vector? It could literally be one line of code
if you are. In a perfect world there would be no reason at all not to use a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time
while dynamic arrays are what experts should use sometimes (most of the time
experts should use vectors as well).

john
 
G

Gianni Mariani

Dan said:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3 == array_3[j + i] )
{delete array_3;
}

}
}


You can call the destructor on an array element, but you'd be advised to
construct it again -- see below.


#include <new>

template <typename Tz>
Tz * Create( void * i_location )
{
return new ( i_location ) Tz();
}


template <typename T>
void Destroy( T & i_val )
{
i_val.~T();

void * i_addr = static_cast< void * >( & i_val );

Create<T>( i_addr );
}



struct A
{

A()
{
}

~A()
{
}

int z;
};


A a[20];
int i[20];

int main()
{

Destroy( a[3] );

Destroy( i[3] );
}
 
D

Dan

I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).

I originally had two on sorted arrays, I combined into a third array, then I
sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?

2) Are you prepared to use a vector? It could literally be one line of code
if you are. In a perfect world there would be no reason at all not to use a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time
while dynamic arrays are what experts should use sometimes (most of the time
experts should use vectors as well).
It is something to look into and I will , but my class does not ask for
this at the moment
 
J

John Harrison

Dan said:
I originally had two on sorted arrays, I combined into a third array, then I
sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?

Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array != array[i - 1])
array[new_size++] = array;
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a << ' ';
cout << '\n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john
 
D

Dan

Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array != array[i - 1])
array[new_size++] = array;
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a << ' ';
cout << '\n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john


WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.
 
J

John Harrison

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array != array[i - 1])
array[new_size++] = array;
return new_size;
}


WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.


Thanks, but actually its bugged. It will fail if the old_size is zero, in
that case new_size will equal 1 which is obviously incorrect. Replace with
this slight modification

int remove_dups(int* array, int old_size)
{
if (old_size == 0)
return 0;
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array != array[i - 1])
array[new_size++] = array;
return new_size;
}

john
 
J

Julie

John said:
....

It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.
 
J

John Harrison

Julie said:
John said:
...

It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john
 
V

Victor Bazarov

Julie said:
John said:
...


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.

By calling 'realloc' you're not resizing an array, you're getting
another array of a potentially different size with some elements that
have the same values as the source array.

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

V
 
J

Julie

John said:
Julie said:
John said:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
...

It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john

Ok, we are getting into a pretty gray area here by making that distinction.
Yes, I do agree that there is a distinction conceptually, but implementation,
it is the same, right?
 
J

Julie

Victor said:
John said:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

...


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.

By calling 'realloc' you're not resizing an array, you're getting
another array of a potentially different size with some elements that
have the same values as the source array.

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

V

I don't have a copy either, but I've never heard that it *must* be a different
address, especially if the initial block is being shrunk. Regardless, I could
write my own sub allocator that would resize (shrink) in place that would then
meet all the conditions of being able to have an allocated array, delete
elements and resize (shrink) in place.

Regardless, I think that this discussion is boiling down to the concept of what
an array is -- JH doesn't consider malloc(sizeof(int) * size) to allocate an
array, per se, but rather a block of memory. So, I guess if you get down to
the specific exact case of an array, such as int array[] = { 1, 2, 3 }, then
yes, you can't _delete_ an element, however I was referring to the more looser
term for array which includes allocated blocks treated as arrays...
 
J

John Harrison

It is impossible to change the size of an array in C++, so you are
trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at
the
end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john

Ok, we are getting into a pretty gray area here by making that distinction.
Yes, I do agree that there is a distinction conceptually, but implementation,
it is the same, right?

I was thinking along practical lines. Using realloc on an array is not going
to get you very far

int a[5] ;
realloc(a, 4*sizeof(int)); // crash here

And I didn't want to start explaining about dynamic memory allocation until
I was sure that was what the OP needed (turned out he didn't).

john
 
J

John Harrison

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

That's not true. The precise words from the C99 standard are "The realloc
function returns a pointer to the new object (which may have the same value
as a pointer to the old object) ..."

john
 
J

Julie

John said:
It is impossible to change the size of an array in C++, so you are
trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the
end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john

Ok, we are getting into a pretty gray area here by making that distinction.
Yes, I do agree that there is a distinction conceptually, but implementation,
it is the same, right?

I was thinking along practical lines. Using realloc on an array is not going
to get you very far

int a[5] ;
realloc(a, 4*sizeof(int)); // crash here

And I didn't want to start explaining about dynamic memory allocation until
I was sure that was what the OP needed (turned out he didn't).

john

Agreed and understood.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top