In C, is it possible do free only an array first or last position?

Posted by user354959 on Stack Overflow See other posts from Stack Overflow or by user354959
Published on 2010-06-01T00:30:51Z Indexed on 2010/06/01 0:43 UTC
Read the original article Hit count: 206

Filed under:
|
|
|
|

Hi there!

I've an array, but I don't need its first (or last) position. So I point a new variable to the rest of the array, but I should free the array first/last position. For instance:

p = read_csv_file();
q = p + 1; // I don't need the first CSV file field
// Here I'd like to free only the first position of p
return q;

Otherwise I've to memcpy the array to other variable, excluding the first position, and then free the original array. Like this:

p = read_csv_file();
q = (int*) malloc(sizeof(int) * (SOME_SIZE - 1));
memcpy(q, p+1, sizeof(int) * (SOME_SIZE - 1));
free(p);
return q;

But then I'll have the overhead of copying all the array.

Is this possible to only free a single position of an array?

© Stack Overflow or respective owner

Related posts about c

    Related posts about array