为什么realloc报错Thread 1: signal SIGABRT
Why realloc error Thread 1: signal SIGABRT
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/uio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdarg.h>
void debug(void);
char * string(const char * );
char * strcats(const char * ,const char * );
char * strspe(const char *, ...);
int isDir(const char *);
int main(void){
debug();
return 0;
}
void debug(void){
char * str = "ssssss";
str = string("abcdef");
char * hello;
hello = strspe(str,"hello","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world",NULL);
printf("hello: %s ,Address: %p\n",hello,(char *)hello);
}
char * string(const char * str){
return strcpy(malloc(strlen(str) * sizeof(char) + sizeof(char)),str);
}
char * strcats(const char * str1,const char * str2){
int realSize = (int)strlen(str2) * sizeof(char) + (int)strlen(str1) * sizeof(char) + sizeof(char);
str1 = realloc((char *)str1,realSize);
strcat((char *)str1,str2);
return (char *)str1;
}
char * strspe(const char * format, ...)
{
va_list args;
const char * argv;
char * result = string(format);
//free((char*)format);
va_start(args, format);
while((argv = va_arg(args,const char *)) && argv != NULL){
strcats((char *)result,argv);
}
va_end(args);
return result;
}
int isDir(const char *filename){
return (access(filename, 0) == 0);
}
这是一段 C 代码,有时 运行s,有时不 运行 并给出错误 Thread 1: signal SIGABRT.
Debug (23028,0x1000ebe00) malloc: *** error for object 0x1007af200: pointer being realloc's d was not allocated
Debug (23028,0x1000ebe00) malloc: *** set a breakpoint in malloc_error_break to debug(23028,0x1000ebe00) malloc: *** set a breakpoint in malloc_error_break to debug(23028,0x1000ebe00
(lldb)
并将光标定位到第37行:
str1 = realloc((char *)str1,realSize);
我无法解决问题
问题出在这个循环中:
while((argv = va_arg(args,const char *)) && argv != NULL) {
strcats((char *)result,argv);
}
strcats
重新分配与 result
(名为 str1
的参数)关联的内存,但 realloc
可能会更改地址。您正确 return 新地址但从未使用它,因此 result
永远不会改变。然后在给定的时间,当realloc
将数据移动到另一个地方时,前者不再有效,在下一个循环中再次发送错误地址。
更改为:
while((argv = va_arg(args,const char *)) && argv != NULL){
result = strcats(result,argv);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/uio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdarg.h>
void debug(void);
char * string(const char * );
char * strcats(const char * ,const char * );
char * strspe(const char *, ...);
int isDir(const char *);
int main(void){
debug();
return 0;
}
void debug(void){
char * str = "ssssss";
str = string("abcdef");
char * hello;
hello = strspe(str,"hello","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world","world",NULL);
printf("hello: %s ,Address: %p\n",hello,(char *)hello);
}
char * string(const char * str){
return strcpy(malloc(strlen(str) * sizeof(char) + sizeof(char)),str);
}
char * strcats(const char * str1,const char * str2){
int realSize = (int)strlen(str2) * sizeof(char) + (int)strlen(str1) * sizeof(char) + sizeof(char);
str1 = realloc((char *)str1,realSize);
strcat((char *)str1,str2);
return (char *)str1;
}
char * strspe(const char * format, ...)
{
va_list args;
const char * argv;
char * result = string(format);
//free((char*)format);
va_start(args, format);
while((argv = va_arg(args,const char *)) && argv != NULL){
strcats((char *)result,argv);
}
va_end(args);
return result;
}
int isDir(const char *filename){
return (access(filename, 0) == 0);
}
这是一段 C 代码,有时 运行s,有时不 运行 并给出错误 Thread 1: signal SIGABRT.
Debug (23028,0x1000ebe00) malloc: *** error for object 0x1007af200: pointer being realloc's d was not allocated Debug (23028,0x1000ebe00) malloc: *** set a breakpoint in malloc_error_break to debug(23028,0x1000ebe00) malloc: *** set a breakpoint in malloc_error_break to debug(23028,0x1000ebe00 (lldb)
并将光标定位到第37行:
str1 = realloc((char *)str1,realSize);
我无法解决问题
问题出在这个循环中:
while((argv = va_arg(args,const char *)) && argv != NULL) {
strcats((char *)result,argv);
}
strcats
重新分配与 result
(名为 str1
的参数)关联的内存,但 realloc
可能会更改地址。您正确 return 新地址但从未使用它,因此 result
永远不会改变。然后在给定的时间,当realloc
将数据移动到另一个地方时,前者不再有效,在下一个循环中再次发送错误地址。
更改为:
while((argv = va_arg(args,const char *)) && argv != NULL){
result = strcats(result,argv);
}