用REALLOC()插入空隙
我正在尝试执行void insertion()
,但始终会得到分段故障,请参见以下内容。同时,我引用了此 link 。
首先,我做了realloc()
,然后在位置
之后将每个内存移到下一个空间,最后将insertion
放在中位置
并增加narraysize
。
#include <stdio.h>
#include <stdlib.h>
void printArray(double array[], unsigned int size)
{
for (unsigned int i = 0; i < size; ++i)
{
printf("%.3f ", array[i]);
}
printf("\n");
}
void insert(double **array,
size_t *nArraySize,
double insertion,
unsigned int position)
{
//realloc the memory space first
*array = realloc(*array, (*nArraySize+1) * sizeof(double));
for(unsigned int i = nArraySize[0]-1; i >= position; i--)
{
*array[i+1] = *array[i];
}
*array[position] = insertion;
*nArraySize += 1;
}
int main()
{
double *t = calloc(4, sizeof(double));
t[0] = 0;
t[1] = 1;
t[2] = 2;
t[3] = 3;
size_t k = 4;
insert(&t, &k, 1, 1);
printf("k is %zu\n", k);
printArray(t, k);
free(t);
}
请帮忙。欢迎任何建议。
I am trying to do a void insertion()
, but always get a segmentation fault, see following. Same time, I referenced this link.
First, I did realloc()
, then move every memory to the next space after the position
, last put the insertion
in the position
and increase nArraySize
.
#include <stdio.h>
#include <stdlib.h>
void printArray(double array[], unsigned int size)
{
for (unsigned int i = 0; i < size; ++i)
{
printf("%.3f ", array[i]);
}
printf("\n");
}
void insert(double **array,
size_t *nArraySize,
double insertion,
unsigned int position)
{
//realloc the memory space first
*array = realloc(*array, (*nArraySize+1) * sizeof(double));
for(unsigned int i = nArraySize[0]-1; i >= position; i--)
{
*array[i+1] = *array[i];
}
*array[position] = insertion;
*nArraySize += 1;
}
int main()
{
double *t = calloc(4, sizeof(double));
t[0] = 0;
t[1] = 1;
t[2] = 2;
t[3] = 3;
size_t k = 4;
insert(&t, &k, 1, 1);
printf("k is %zu\n", k);
printArray(t, k);
free(t);
}
Please help. Any suggestion is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于操作员的优先级,这些陈述是不正确的。
那就是后缀操作员的优先级,其优先级高于单一操作员。
您必须写作
或已经在做的事情,例如
要注意您的功能插入不安全,因为没有检查位置的值是小于或等于传递数组中当前元素数量的值。
此外,循环
也可以调用不确定的行为。首先,变量
i
始终具有非负号。这就是i
的值,因为变量i
具有类型inted int
。现在,假设*narraysize
等于1
和位置
等于0
;在这种情况下,您有循环,在循环的第一次迭代之后,
i
的值将非常大,大于0
。因此,最好以以下方式重写循环
These statements are incorrect due to the operator precedence.
That is postfix operators have a higher precedence than unary operators.
You have to write either
or as you are already doing like
Pay attention to that your function insert is unsafe because there is no check whether the value of position is less than or equal to the current number of elements in the passed array.
Also this for loop
can invoke undefined behavior. First of all the variable
i
has always a non-negative number. That is the value ofi
can not be negative because the variablei
has the typeunsigned int
. Now let's assume that*nArraySize
is equal to1
andposition
is equal to0
; In this case you have the loopand after the first iteration of the loop the value of
i
will be very big that is greater than0
.So it will be better to rewrite the loop the following way