#How can I remove the first element of a dynamic array?

36 messages ยท Page 1 of 1 (latest)

heavy girderBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

silent bane
#

The only real good way to remove the first element is to move all the other elements back one

#

Unless you put in a large amount of effort into making a double ended queue data structure

boreal lintel
#
#include <stdio.h>
#include <memory.h>
#include <malloc.h>

// works for interger arrays, not general.
// to make it work with all arrays  
// we need to use void *arr and also be
// informed for the size of each element.
//
// @return The array without the first element,
// returned element must be freed using free.
int *remove_first_element(int *arr, size_t size)
{
    int *result = (int *) malloc(sizeof(int) * 
                                 (size - 1));

    memcpy(result, arr + 1, sizeof(int) * (size  - 1));

    return result;
}

int main()
{
    int arr[5] = {1,2,3,4,5};

    int *res = remove_first_element(arr, 5);

    for (int i = 0; i < 4; i++) {
        printf("%d\n", res[i]);
    }
}

#

you can do something lilke that

silent bane
#

fair point, I was only considering the scenario of an inplace removal

boreal lintel
#

xmm if you run the above snap of code, this is what it does

#

i think that it will work in your case

#
memcpy(result, arr + 1, sizeof(int) * (size  - 1));

in this line, i just copy all the elements of the array after the first in a new array

#

isn't this what you want? ๐Ÿง ๐Ÿ˜‚

#

( Sorry for my English, I might have some grammar mistakes )

heavy girderBOT
#

@raven cargo Has your question been resolved? If so, run !solved :)

boreal lintel
#

ah it worked? ๐Ÿ˜‚

#

xmm yea I got it know

wooden hemlock
#

@raven cargo If you want to get fancy, a single call to memmove() can move all your PIDs back one index in the array.

boreal lintel
#

xmm I had forgot about this function

wooden hemlock
#

If multiple threads are accessing the PID array at the same time, you'll want to protect all reads/writes to the PID array with some kind of synchronization.

#

To be clear, memcpy() will NOT work. It has to be memmove()

#

memcpy() only works when the source and destination don't overlap at all. memmove() works when the source and destination overlap.

#

What type is unavailablePIDs?

#

You might want to debug by doing something like this:

printf("unavailablePIDs = %p, PID+1 = %p\n", unavailablePIDS, PID+1);

Just to see if the pointers are coming out as expected.

boreal lintel
boreal lintel
#

Sorry, i wrote something, but then I realize it's completely wrong ๐Ÿ˜‚

#

ah ok ๐Ÿ˜‚

boreal lintel
#

dunno ๐Ÿ˜‚

#

not a fancy solution

boreal lintel
#

I'm a little bit confused, what's the purpose of unavailablePIDs? xmm maybe I don't understand something.

wooden hemlock
#

What are you trying to do? Move all the elements to the left one position?

#

That is, pid[1] is moved into pid[0], pid[2] is moved into pid[1], etc.?

#

Hmmm, would that be:

memmove(unavailablePIDs, unavailablePIDs + 1, <size of pid array minus 1>);

#

Perhaps:

memmove(unavailablePIDs, unavailablePIDs + 1, sizeof(unavailablePIDs) - sizeof(unavailablePIDs[0]));

Assuming that unavailablePIDs is an array (i.e., not allocated using e.g. malloc)

#

(Edited the most recent message)

heavy girderBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

heavy girderBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.