realloc 可以缩小左侧的数组吗(仅限 C)?

发布于 2024-09-25 22:12:19 字数 819 浏览 7 评论 0原文

我想移动内存中的大量数据。不幸的是,这些数据被保存为数组,我无法更改它。我无法使用循环数组,因为我不想更改的几个 Fortran 方法也使用了相同的内存。最重要的是,在移动之间,数组的访问非常频繁。所以我可以这样做:

int *array = (int*) malloc(sizeof(int)*5);
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array2=array+1;
memmove(array,array2,5*sizeof(int));
array=(int*) realloc(array,5);

这应该可以正常工作,但看起来很浪费;)。如果我可以告诉我的编译器删除缩小数组左侧的数据,我的数据就会在内存中蠕动,但我不必进行任何复制。像这样:

int *array = (int*) malloc(sizeof(int)*5);
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array=(int*) realloc_using_right_part_of_the_array(array,5);

所以基本上我想以一个指向 array+1 的指针结束,并释放它左边的 4 个字节。我尝试了 free()malloc() 但它不起作用...... 我知道 realloc 也可能导致 memcpy 调用,但不是每次都会!所以它可以更快,不是吗?

I want to move a large chunk of data i've got in my memory. Unfortunately this data is saved as an array, and i cannot change that. I can't use circular arrays, because the same memory is also used by a couple of fortran methods i do not want to change. On top of it, the arrays are accessed very very often in between the movement. So i can do this:

int *array = (int*) malloc(sizeof(int)*5);
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array2=array+1;
memmove(array,array2,5*sizeof(int));
array=(int*) realloc(array,5);

This should work fine, but it looks so wasteful ;). If i could tell my compiler to take away data on the left side of a shrinking array, my data would sort of creep through the memory, but i wouldn't have to do any copying. Like this:

int *array = (int*) malloc(sizeof(int)*5);
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array=(int*) realloc_using_right_part_of_the_array(array,5);

So basically i want to finish with a pointer to array+1, with the 4 byte left of it freed. I played around with free() and malloc() but it didn't work...
I'm aware that the realloc could also result in a memcpy call, but not everytime! So it could be faster, couldn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-10-02 22:12:19

不可以。没有办法归还您分配的内存的较低部分。另外,您的原始代码是错误的,因为您正在复制不确定的内存。

int *array = (int*) malloc(sizeof(int)*5);
// Fill memory:
// array - {'J', 'o', h', 'n', '\0'}; 
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
// array - {'J', 'o', h', 'n', '\0', X};
array2=array+1;
// array2 pointer to 'o of array.
memmove(array,array2,5*sizeof(int));
// This copies the indeterminate x:
// array - {'o', h', 'n', '\0', X, X}
array=(int*) realloc(array,5);
// array - {'o', h', 'n', '\0', X}

X表示不确定。

No. There is no way to give back the lower part of the memory you allocated. Also, your original code is wrong, since you're copying indeterminate memory.

int *array = (int*) malloc(sizeof(int)*5);
// Fill memory:
// array - {'J', 'o', h', 'n', '\0'}; 
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
// array - {'J', 'o', h', 'n', '\0', X};
array2=array+1;
// array2 pointer to 'o of array.
memmove(array,array2,5*sizeof(int));
// This copies the indeterminate x:
// array - {'o', h', 'n', '\0', X, X}
array=(int*) realloc(array,5);
// array - {'o', h', 'n', '\0', X}

X means indeterminate.

葮薆情 2024-10-02 22:12:19

为什么不简单地把元素一一复制呢?

#define NELEMS 5
for (i = 0; i < NELEMS - 1; i++) {
    array[i] = array[i + 1];
}
array[NELEMS - 1] = 0;

或者,像您一直在做的那样使用 memmove ,但不进行重定位

#define NELEMS 5
memmove(array, array + 1, (NELEMS - 1) * sizeof *array);
array[NELEMS - 1] = 0;

Why don't you simply copy the elements one by one?

#define NELEMS 5
for (i = 0; i < NELEMS - 1; i++) {
    array[i] = array[i + 1];
}
array[NELEMS - 1] = 0;

or, use memmove like you've been doing, but without the relocation

#define NELEMS 5
memmove(array, array + 1, (NELEMS - 1) * sizeof *array);
array[NELEMS - 1] = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文