首页 > c > 错误:在macOS上“未分配指针被重新分配”但在尝试重新分配两次时在Windows上运行

错误:在macOS上“未分配指针被重新分配”但在尝试重新分配两次时在Windows上运行 (Error :“pointer being realloc'd was not allocated” on macOS but works on Windows whe)

问题

我正在尝试实现一个连接两个字符串的函数,但我一直得到同样的错误。“没有分配指针被重新分配”当我在Windows机器上编译相同的代码时,它是否有效,是否是我缺少的东西?下面的代码基本上就是我想要做的。主要:

int main() {

int length = 4096;
char *string = malloc(length * sizeof(char));
createString(string, length);
realloc(string, 30);
return 0;
}

createString:

void createString(char * string, int length) {
  char *copyAdress = string;
  char *temp ="";
  int counter2 = 0;

  fflush(stdin);
  fgets(string, length,stdin);
  while(*string != EOF && *string != *temp ) {
    string++;
    counter++;
  }
  string = copyAdress;
  realloc(string, (counter)*sizeof(char));
}

谢谢!编辑:我希望createString将字符串的大小更改为我用fgets获取的字符串的长度,同时具有与我发送的字符串相同的地址,因此我可以在以后添加更多内存时添加内容另一个字符串。

解决方法

让我们按照执行顺序解决这个问题。

fflush(stdin);未定义的行为。如果你真的需要清除stdin中的所有内容,你必须找到另一种方式(例如循环)。有编译器/系统具有已定义的实现,但我不会指望它。

string++;string在循环后覆盖时是超级的。

realloc(string, (counter)*sizeof(char));

应该

char *temp = realloc(string, (counter)*sizeof(char));
if (temp != NULL)
  string = temp;

通过这种方式,您可以获得新指针所在的指针string,但我建议您阅读这篇文章的参考资料realloc。从本质上讲,您不知道它是否已被移动,旧地址从那时起可能无效。因此,取消引用它也是未定义的行为。

在此之后,您将不得不返回新地址string或将指针的地址传递给您的函数。

第二个问题重复同样的问题realloc。你只知道你的第一次通话是错误的,因为第二次通话注意到你没有有效的数据,你认为你的string


关于您的评论:无法使用realloc并确保重新分配的内存与以前位于同一位置。

问题

I'm trying to implement a function that concatenate two strings, but I keep getting the same error. "pointer being realloc'd was not allocated" When I compiled the same code on a windows machine it worked, is it something that I'm missing? The code below is basically what I'm trying to do. main:

int main() {

int length = 4096;
char *string = malloc(length * sizeof(char));
createString(string, length);
realloc(string, 30);
return 0;
}

createString:

void createString(char * string, int length) {
  char *copyAdress = string;
  char *temp ="";
  int counter2 = 0;

  fflush(stdin);
  fgets(string, length,stdin);
  while(*string != EOF && *string != *temp ) {
    string++;
    counter++;
  }
  string = copyAdress;
  realloc(string, (counter)*sizeof(char));
}

Thanks! Edit: I want createString to change the size of string to the length of the string that I get with fgets, while having the same address as the string that I sent in, so I can allocate more memory to it later when I want to add another string to it.

解决方法

Let's work through this in order of execution.

fflush(stdin); is undefined behaviour. If you really need to clear everything in the stdin you have to find another way (a loop for example). There are compilers/systems with a defined implementation but I would not count on it.

string++; is superflous as you overwrite string after the loop.

realloc(string, (counter)*sizeof(char));

should be

char *temp = realloc(string, (counter)*sizeof(char));
if (temp != NULL)
  string = temp;

This way you get the pointer where your new string is located, but I suggest you read the refecerence for realloc. In essence you do not know if it has been moved and the old address might be invalid from that point on. So dereferencing it is also undefined behaviour.

After this you would have to return the new address of string or pass the address of the pointer to your function.

The same problem repeats with the second realloc. You only got to know your first call was wrong, because the second call noticed that you do not have valid data in what you thought would be your string.


In regards to your comment: It is not possible to use realloc and to be sure that the reallocated memory is in the same place as before.

相似信息