¿Cómo eliminar un elemento de una matriz de estructura que tiene punteros?
Frecuentes
Visto 3,051 veces
3
This is my structure which has two integer pointers aV and aT.
struct ADJP
{
int *aV;
int eV;
int nV;
int *aT;
int nT;
};
ADJP *Umb = NULL;
The allocation process of aV and aT is like this..
for(int i=0; i<nb; i++)
{
Umb[i].aV = new int[N];
for(int j=0; j<n; j++)
Umb[i].aV[j] = pIn[i].aV[j];
}
I want to remove one specific element from Umb array. for example I want to remove Umb[5], then how can I remove. I have tried with various mathods but got error due to allocated pointers I think. I have tried with follow method but its not working with this kind of struct array. It is working with struct array having no pointers.
int DeleteStructElement(int Index, ADJP *b, int N, int at)
{
for(int i=Index; i<N-1; i++)
memmove(&b[i], &b[i+1], (N-at-1)*sizeof*b); // moving the terms of array
N--; // updating new size
return N;
}
Have any idea how to remove an element from my struct array?
3 Respuestas
2
You will want to delete the arrays in the deleted element to release their memory:
delete[] b[Index].aV;
delete[] b[Index].aT;
Then, you only have to do a single memmove to remove the element.
memmove(&b[Index], &b[Index+1], (N-Index-1) * sizeof(b[Index])
EDIT: as Mahmoud points out, this doesn't use the at
parámetro en DeleteStructElement
; I'm not sure what you intended that parameter to do.
Respondido 26 ago 12, 05:08
You did not make use of the parameter 'at' still I voted up because this is the right answer. Please direct him to remove the unused parameter. - mahmud fayez
thanks "nneonneo". You mean that I don't need to move all above elements? means there is no need to for loop ? just remove the Index elements and move the memory of Index+1 to index thats all ? this is what you mean ? - maxpayne
Well, actually, memmove is capable of moving more than one element at once, as it just moves whole blocks of memory. In this case, I've set the number of bytes (the third parameter) so that it moves all of the remaining elements. - neonneo
can you also tell me is it right to copy using memcpy. like this.. memcpy(temp, Umb, nb*sizeof(ADJP)); in this case, we r not allocating memory for temp.aV and temp.aT, so to copy Umb memory to temp is legeal ? - maxpayne
memcpy is wrong since, in the original code, source and target overlap. I do not see what you want to achieve with temp. - JohnB
2
int DeleteStructElement (int index, ADJP * b, int nb) {
delete [] (b[index].aV);
for (int i = index; i < nb - 1; ++i) {
b[i] = b[i+1];
}
return nb - 1;
}
Respondido 26 ago 12, 08:08
0
Assuming you're really using C++, a destructor in ADJP
would be much more straightforward than DeleteStructElement
.
But if you're doing some interesting "C" with new
/delete
(perhaps a well-confined subset of C++?), then I'd suggest calling delete
desde adentro DeleteStructElement
.
but got error due to allocated pointers I think
Answering this question might be much more important than others. I'm assuming this was a runtime error? Use a debugger to suss out just exactly where the fault was.
Respondido 26 ago 12, 05:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ c arrays data-structures or haz tu propia pregunta.
new is C++ only which language are you using? - Adrian Cornish
Tu pregunta está etiquetada
C
(y noC++
), yet you're usingnew
. Are you really limited toC
? - Brian Cain"new" is c++. If you are writing C you should use malloc(). - Hannes Landeholm
of course c and c++ .. I am using visual studio C++ 2010 .. Let me tag c++ too.. - maxpayne