我有一个数组,但我不需要它的第一个(或最后一个)位置。所以我将一个新变量指向数组的其余部分,但我应该释放数组的第一个/最后一个位置。例如:
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;
否则我必须将数组 memcpy 到其他变量,不包括第一个位置,然后释放原始数组。像这样:
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;
但是,我将有复制所有数组的开销。
这是否可以只释放数组的一个位置?