简体   繁体   English

使用realloc增加数组的大小

[英]Using realloc to increase the size of an array

I am trying to scan a bunch of characters into an array. 我正在尝试将一堆字符扫描到一个数组中。 I have used malloc to set the original size of the array, but I want to use realloc to increase the size if the user enters more characters than the initial size allows. 我已经使用malloc设置数组的原始大小,但是如果用户输入的字符数超出了初始大小的允许范围,我想使用realloc来增加大小。 I am not quite sure where to put the realloc, or if it should be within a conditional statement. 我不太确定将重新分配放在哪里,或者是否应该在条件语句中。

char *strscan(void) {
  int size = sizeof(char) * 10;
  char *a = malloc(size);

  // Below I try to read character input from the user and store it in the array.

 while (a != EOF) {

    scanf("%c", a);
    if (trace) printf("%c", *a);
    ++a;
    a = realloc(a, 2 * size);
 }

    return a;

}

As of now I still get heap buffer overflow upon entering, for example, 15 characters. 到目前为止,输入15个字符后,仍然会出现堆缓冲区溢出的情况。

++a;
a = realloc(a, 2 * size);

This is the problem. 这就是问题。 The first argument of realloc must be a pointer that is returned by the malloc family functions, or a null pointer. realloc的第一个参数必须是malloc系列函数返回的指针或空指针。 a used to be one, but by ++a; a曾经是一个,但++a; , it's not any more. ,已不再。

I see two problems here. 我在这里看到两个问题。

The first is that you're incrementing a , then passing the incremented value to realloc . 首先是您要递增a ,然后将递增的值传递给realloc Since the pointer passed to realloc was not a value returned from malloc , calloc , or realloc , or is NULL, this can cause errors. 由于传递给realloc的指针不是malloccallocrealloc返回的值,或者为NULL,因此可能导致错误。

The second problem is that you're not increasing the size of your memory buffer after the first call to realloc , since you're always passing it 2 * size and size never changes. 第二个问题是,在您第一次调用realloc之后,您没有增加内存缓冲区的大小,因为您总是将其传递给2 * size并且size永不改变。 So you eventually run past the end of the buffer. 因此,您最终会越过缓冲区的末端。

You need a separate pointer to keep track of where the next character should go, and you need to keep track of how big your buffer currently is and realloc only when your existing buffer is almost full. 你需要一个单独的指针跟踪的下一个字符应该去,和你需要跟踪你的缓冲区目前有多大和realloc时候您现有的缓冲区只差一点满。

char *strscan(void) {
  size_t size = sizeof(char) * 10;
  char *a = malloc(size);
  char *current;  // The current character
  ptrdiff_t diff;

  current = a;
  do {
    scanf("%c", current);
    if (trace) printf("%c", *current);
    if (current - a >= size - 1) {
      size *= 2;
      diff = current - a;
      a = realloc(a, size);
      current = a + diff;   // Since "a" could change, we need to modify "current" as well
    }
  } while (*current++ != '\n');
  *current = '\x0';

  return a;
}

Remove that ++a . 删除++ a。 It is the culprit here . 这是罪魁祸首。

realloc() accepts the either the NULL pointer or the malloc returned pointer. realloc()接受NULL指针或malloc返回的指针。

Read from the man page. 从手册页中读取。

Hope you understand . 希望你能理解 。 Happy Coding.. 快乐编码

You need two pointers. 您需要两个指针。 One is the location of the buffer. 一种是缓冲区的位置。 The other is the location of the current character. 另一个是当前字符的位置。 When the difference between them is large enough, reallocate the buffer and reset the pointers. 当它们之间的差异足够大时,请重新分配缓冲区并重置指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM