0

I have an array of sockets (pfd[nfd].fd). I also have an array of pointers (event_tracking* track[10]). If there comes a point in my code where I try and receive data from a socket that is closed I would like to remove that array element in both arrays and then shift the arrays to fill the empty spot.

 for(j=1; j<nfd; j++) {
        if(pfd[j].revents&POLLIN) {  

            char* p = track.receive_datagram();

            if (p == 0) { 
            delete track[j-1];
            //Delete element in pfd[nfd].fd
            //Reorder elements of track array
            //Reorder elements of pfd array
            }

        }
  }

I know you can call the delete operator to call the destructor for track but I'm not sure how to reorder the elements in the array now that one is missing? Or how to delete and reorder the pfd array?

Any help would be appreciated! I can't find any examples of deleting and reordering arrays in my text.

2
  • 3
    Could you use vectors? Commented Sep 16, 2015 at 23:50
  • 4
    If the order of the elements is not important, you can just overwrite the deleted item with the last one and decrement the length. Commented Sep 16, 2015 at 23:51

2 Answers 2

1

A C++ solution should be to use a std::vector or std::array and forget about everything (possibly storing smart pointers, eg std::unique_ptr<T>).

If you really want to go the hard way you could setup things like these:

  • find element to remove (index i)
  • if i == LENGTH_OF_ARRAY - 1 do nothing, otherwise swap element at i and at LENGTH_OF_ARRAY - 1 so that the element to be removed is in last position
  • call delete array[LENGTH_OF_ARRAY - 1] to destroy last element
  • call array = realloc(array, (LENGTH_OF_ARRAY - 1) * sizeof(array[0])) to release memory in the array for removed element, and update LENGTH_OF_ARRAY
Sign up to request clarification or add additional context in comments.

4 Comments

Note that you shouldn't mix realloc with new/new[] or delete/delete[]. You should instead use malloc or calloc and free. You can safely use new and delete with objects whose pointers are in an array created with malloc - that is, you can mix allocators within a given codebase - but you must not mix them on any particular allocation.
Indeed there is no allocation mixed since delete is called on an element of the array but the OP should take care of allocating array with calloc/malloc.
Right, just wanted to make sure they knew not to use new and delete[] with an array that they'll want to call realloc on. Huh.. now I'm wondering if there's an equivalent of realloc for new/delete...
There is not an equivalent of realloc(), but you can resize a std::vector.
0

Without changing your data structures, the solution is to copy each element after the one you remove over the previous element of the array. This is more efficient for a linked list. With an array allocated by new[], you would not reallocate anything, but you could resize a vector.

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.