用REALLOC()插入空隙

发布于 2025-01-24 04:54:54 字数 1201 浏览 3 评论 0原文

我正在尝试执行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 技术交流群。

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

发布评论

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

评论(1

我的痛♀有谁懂 2025-01-31 04:54:54

由于操作员的优先级,这些陈述是不正确的。

*array[i+1] = *array[i];
*array[position] = insertion;

那就是后缀操作员的优先级,其优先级高于单一操作员。

您必须写作

( *array )[i+1] = ( *array )[i];
( *array )[position] = insertion;

或已经在做的事情,例如

array[0][i+1] = array[0][i];
array[0][position] = insertion;

要注意您的功能插入不安全,因为没有检查位置的值是小于或等于传递数组中当前元素数量的值。

此外,循环

for(unsigned int i = nArraySize[0]-1; i >= position; i--)
{
   ( *array )[i+1] = ( *array )[i];
}

也可以调用不确定的行为。首先,变量i始终具有非负号。这就是i的值,因为变量i具有类型inted int。现在,假设*narraysize等于1位置等于0;在这种情况下,您有循环

for(unsigned int i = 0; i >= 0; i--)
{
   ( *array )[i+1] = ( *array )[i];
}

,在循环的第一次迭代之后,i的值将非常大,大于0

因此,最好以以下方式重写循环

for(unsigned int i = *nArraySize; i > position; i--)
{
   ( *array )[i] = ( *array )[i-1];
}

These statements are incorrect due to the operator precedence.

*array[i+1] = *array[i];
*array[position] = insertion;

That is postfix operators have a higher precedence than unary operators.

You have to write either

( *array )[i+1] = ( *array )[i];
( *array )[position] = insertion;

or as you are already doing like

array[0][i+1] = array[0][i];
array[0][position] = insertion;

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

for(unsigned int i = nArraySize[0]-1; i >= position; i--)
{
   ( *array )[i+1] = ( *array )[i];
}

can invoke undefined behavior. First of all the variable i has always a non-negative number. That is the value of i can not be negative because the variable i has the type unsigned int. Now let's assume that *nArraySize is equal to 1 and position is equal to 0; In this case you have the loop

for(unsigned int i = 0; i >= 0; i--)
{
   ( *array )[i+1] = ( *array )[i];
}

and after the first iteration of the loop the value of i will be very big that is greater than 0.

So it will be better to rewrite the loop the following way

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