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?

4
  • new is C++ only which language are you using? Commented Aug 26, 2012 at 4:09
  • Your question is tagged C (and not C++), yet you're using new. Are you really limited to C? Commented Aug 26, 2012 at 4:10
  • "new" is c++. If you are writing C you should use malloc(). Commented Aug 26, 2012 at 4:10
  • of course c and c++ .. I am using visual studio C++ 2010 .. Let me tag c++ too.. Commented Aug 26, 2012 at 4:16

3 Answers 3

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 parameter in DeleteStructElement; I'm not sure what you intended that parameter to do.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 ?
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.
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 ?
memcpy is wrong since, in the original code, source and target overlap. I do not see what you want to achieve with temp.
|
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;
   }

3 Comments

I think you have a typo. Shouldn't it be: delete [] b[index].aV; and probably also for(int i = index; i < nb - 2; ++i)
You're right with b{index]. I corrected that. Why i < nb - 2?
Hmmm, not sure why I said that. Doesn't seem right now that I think about it.
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 from within 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.