-1

我正在使用 realloc 在运行时在动态数组中分配内存。首先,我用 calloc 分配了一个内存,其 sizeof 是一个随机整数 a。在我的程序中,我采用了 a=2。之后我想存储生成的 14 个随机值,所以我必须使用 realloc 调整内存大小。我在 for 循环中做同样的事情。对于 1 次迭代,realloc 可以工作,但在该大小不会增加之后,会出现“堆损坏”错误。我无法理解这个问题。如果可以的话,请帮助我,了解问题发生在哪里以及如何解决它。非常感谢。下面是我的代码:

j=j*a; //a=3
    numbers = (int*) calloc(b, j); //b=14, no of elements I want to store

    printf("Address:%p\n",numbers);
    if (numbers == NULL)
    {
        printf("No Memory Allocated\n");
    }
    else
    {
    printf("Initial array size: %d elements\n", a);
    printf("Adding %d elements\n", b);
    }



    srand( (unsigned) time( NULL ) );
    for(count = 1; count <= b ; count++)
    {


        if(i <= j)
        {

        numbers[count] = rand() % 100 + 1;
        printf( "Adding Value:%3d Address%p\n", numbers[count],numbers[count] );

           i++;

        }

        if (i > j)
        {
                printf("Increasing array size from %d bytes to %d bytes\n",j,j*a);
                j=j*a;  
                numbers = (int*) realloc(numbers,j);
                printf("Address:%p\n",numbers);
                if(numbers == NULL)
            {
                printf("No Memory allocated\n");
            }


        }

    }   

    free(numbers);

    return 0;
}
4

2 回答 2

1
  • 初始数组长度(长度和大小不相同)是b,不是a
  • 添加b元素?我不认为你是。
  • 数组在 C 中是从零开始的。你的循环应该是for(count=0; count<b ; count++).
  • count是循环变量的可怕名称。count应该保存元素的数量而不是循环变量。
  • 很难想象j会是什么。由于您在调用它时使用它作为元素大小,calloc因此它应该至少是 4 的倍数,即 int 的大小。它是什么?!
  • realloc似乎与 没有任何关系calloc

我敢肯定还有很多其他问题。如果您需要更多帮助,则需要明确说明您的目标是什么。

编辑

听起来你想要这样的东西:

int capacity = 10;
int count = 40;
int i;

int* array = (int*)malloc(capacity*sizeof(int));
for (i=0; i<count; i++)
{
    if (i==capacity)
    {
        capacity *= 2;
        array = (int*)realloc(array, capacity*sizeof(int));
    }
    array[i] = RandomIntInRange(1, 100);
}
free(array);

备注

  1. 没有错误检查。在生产代码中,您将检查分配是否成功,如果以这种方式执行的 realloc 失败,则会泄漏。但是,当您仍处于这种理解水平时,将消息与错误检查混淆是没有意义的。
  2. 没有阅读输入 - 你可以这样做。
  3. 没有写作输出——你可以做到。
于 2011-03-13T08:00:39.650 回答
0

整数“j”未在您的代码中初始化,导致 a = 0 * 3,这意味着 a 将为零并且不会分配内存。段错误是由于您没有处理该数字为 NULL。更改为并将 j 设置为有意义的东西

#include <stdlib.h>
#include <stdio.h>

void
main (int argc, char *argv[])
{
  int a = 3;
  int j = 1 * a;        //a=3
  int b = 14;
  int *numbers = calloc (b, j); //b=14, no of elements I want to store
  int count = 0, i = 0;

  printf ("Address:%p\n", numbers);
  if (numbers == NULL)
    {
      printf ("No Memory Allocated\n");
      return;
    }
  else
    {
      printf ("Initial array size: %d elements\n", a);
      printf ("Adding %d elements\n", b);
    }



  srand ((unsigned) time (NULL));
  for (count = 1; count <= b; count++)
    {
      if (i <= j)
    {
      numbers[count] = rand () % 100 + 1;
      printf ("Adding Value:%3d Address%p\n", numbers[count],
          &(numbers[count]));

      i++;

    }

      if (i > j)
    {
      printf ("Increasing array size from %d bytes to %d bytes\n", j,
          j * a);
      j = j * a;
      numbers = (int *) realloc (numbers, j);
      printf ("Address:%p\n", numbers);
      if (numbers == NULL)
        {
          printf ("No Memory allocated\n");
        }


    }

    }

  free (numbers);
}
于 2011-03-13T08:00:22.120 回答