Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 894
João Olavo Vasconcelos Avatar asked Jun 01 '10 00:06

João Olavo Vasconcelos


People also ask

Can you free part of an array in C?

You can't free part of an array - you can only free() a pointer that you got from malloc() and when you do that, you'll free all of the allocation you asked for. As far as negative or non-zero-based indices, you can do whatever you want with the pointer when you get it back from malloc() .

How do you find the last and first of an array?

To get the first and last elements of an array, access the array at index 0 and the last index. For example, arr[0] returns the first element, whereas arr[arr. length - 1] returns the last element of the array. Copied!

What is the last index of an array in C?

Arrays in C are indexed starting at 0, as opposed to starting at 1. The first element of the array above is point[0]. The index to the last value in the array is the array size minus one. In the example above the subscripts run from 0 through 5.

How does C know when an array ends?

C arrays don't have an end marker. It is your responsibility as the programmer to keep track of the allocated size of the array to make sure you don't try to access element outside the allocated size. If you do access an element outside the allocated size, the result is undefined behaviour.


2 Answers

No. You can only free() a complete block obtained from a call to malloc() (or one of malloc()'s friends), not a piece of that block.

Your best bet is probably to leave the allocated block as-is and just use a pointer to the element at index one as if it were the beginning of the array (and ignore the last element).

Using memcpy works if it is really that important to free the two elements.

You could also shift all of the elements to the left by one (i.e., move the element at index one to index zero and so forth) and then call realloc() to resize the block and remove the last two elements. This isn't really a good idea, though, because the most likely outcome is that either (a) the underlying heap allocation won't actually be resized and you'll have moved thing around and gotten no benefit, or (b) the underlying heap allocation will be resized and everything will get moved a second time.

like image 119
James McNellis Avatar answered Oct 10 '22 15:10

James McNellis


That is what realloc(3) is for. For releasing the first array element I'd suggest revising the algorithm.

like image 35
Nikolai Fetissov Avatar answered Oct 10 '22 13:10

Nikolai Fetissov