【发布时间】:2016-10-25 16:13:10
【问题描述】:
我正在尝试连接两个字符串,假设“dest”字符串没有足够的空间来添加另一个,所以我使用动态数组来解决它。
问题是尝试编译代码时出现 mremap_chunk 错误。
我不知道我错过了什么,因为 realloc 调用包含所有正确的参数。
错误:
malloc.c:2869: mremap_chunk: Assertion `((size + offset) & (GLRO (dl_pagesize) - 1)) == 0' failed.
Aborted (core dumped)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *strcatt(char *s1, char *s2)
{
int a = strlen(s1);
int b = strlen(s2);
int i, size_ab = a+b;
s1 = (char *) realloc (s1, size_ab*sizeof(char));
for(i=0; i<b; i++) {
s1[i+a]=s2[i];
}
s1[size_ab]='\0';
return s1;
}
int main()
{
char s1[]="12345";
char s2[]="qwerty";
strcatt(s1,s2);
printf("%s\n", s1);
return 0;
}
【问题讨论】:
-
用
size_ab = a+b+1替换size_ab = a+b