小编Aka*_*aks的帖子

使用strcat和realloc进行连接会产生意外错误

我遇到过所谓的神秘realloc invalid next size error,我gcclinux我的代码上使用的是

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

int main()
{
 int i;
 char *buf;
 char loc[120];
 buf = malloc(1);
 int size;

 for(i=0;i<1920;i++)
  {
    sprintf(loc,"{Fill_next_token = my_next_token%d; Fill_next_token_id = my_next_token_id = my_next_token_id%d}",i,i);
    size = strlen(buf)+strlen(loc);
    printf("----%d\n",size);

    if(!realloc(buf,size))
    exit(1);
    strcat(buf,loc);
    }
  }
Run Code Online (Sandbox Code Playgroud)

(我的可能是重复的问题)这里解决方案的某个地方是避免strcat和使用memcpy,但在我的情况下,我真的想连接字符串.上面的代码适用于这样的920次迭代,但是在1920的情况下realloc给出了无效的新大小错误.请帮助找到连接的替代方案,期待对像我这样的懒惰程序员来说是一个有用的问题.

c linux string gcc string-concatenation

3
推荐指数
1
解决办法
2350
查看次数

标签 统计

c ×1

gcc ×1

linux ×1

string ×1

string-concatenation ×1