[英]String concatenate error with malloc dynamic memory allocation
String *char concatenate error with malloc dynamic memory allocation 字符串* char与malloc动态内存分配的连接错误
I want to make a function to concatenate strings, it works but it gives an error and the processor restarts, I think there's something wrong with pointers, but I do not know what it is, a problem of memory allocation. 我想创建一个连接字符串的函数,它可以工作,但是会出现错误,并且处理器会重启,我认为指针出了点问题,但是我不知道这是什么,这是内存分配的问题。
Thanks in advance! 提前致谢!
char *buf;
int main(void) {
// ...
WriteString("#INIT.\r\n"); //serial output
buf = "";
while(1)
{
char *str1 = "qwe";
char *str2 = "asd";
char *str3 = "zxc";
char *str4 = "123";
buf = my_strcat(buf,str1);
buf = my_strcat(buf,str2);
buf = my_strcat(buf,str3);
buf = my_strcat(buf,str4);
WriteString(buf); //serial output
free(buf);
}
}
char *my_strcat(const char *str1, const char *str2) {
char *new_str;
new_str = malloc(strlen(str1)+strlen(str2)+1);
new_str[0] = '\0';
strcat(new_str,str1);
strcat(new_str,str2);
return new_str;
}
Serial output... 串行输出...
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
Your first call to my_strcat
has undefined behavior because buf
was not initialized before. 您对
my_strcat
第一次调用具有未定义的行为,因为buf
之前未初始化。 Precisely this line is the problem 正是这条线是问题
new_str = malloc(strlen(str1)+strlen(str2)+1);
strlen(str1)
where str1
is uninitialized. strlen(str1)
其中str1
未初始化。
Suggestion, use realloc
建议,使用
realloc
char *my_strcat(char *str1, const char *str2)
{
char *new_str;
size_t length;
size_t str1length;
if (str2 == NULL)
return str1;
str1length = 0;
if (str1 != NULL)
str1length = strlen(str1);
length = strlen(str2) + str1length;
new_str = realloc(str1, 1 + length);
if (new_str == NULL)
return str1;
new_str[str1length] = '\0';
strcat(new_str, str2);
return new_str;
}
and 和
char *buf;
char *str1 = "qwe";
char *str2 = "asd";
char *str3 = "zxc";
char *str4 = "123";
buf = NULL;
buf = my_strcat(buf, str1);
buf = my_strcat(buf, str2);
buf = my_strcat(buf, str3);
buf = my_strcat(buf, str4);
You have memory leaks in the while loop and are running out of memory. while循环中存在内存泄漏,并且内存不足。
buf = my_strcat(buf,str1); // Got some new memory
buf = my_strcat(buf,str2); // Got some more new memory without freeing the previous memory
// The previous memory is lost. You don't even have a pointer
// to it any more.
buf = my_strcat(buf,str3); // Ditto
buf = my_strcat(buf,str4); // Ditto
What you need: 您需要什么:
char* temp = NULL
buf = my_strcat(buf,str1);
temp = buf;
buf = my_strcat(temp,str2);
free(temp);
temp = buf;
buf = my_strcat(temp,str3);
free(temp);
temp = buf;
buf = my_strcat(temp,str4);
free(temp);
Your use of buf
is illogical. 您对
buf
使用是不合逻辑的。 You haven't shown how it was allocated but even if you did malloc()
memory for buf
, you overwrote it with 您尚未显示如何分配它,但是即使您为
buf
做了malloc()
内存,您也可能用
buf = "";
Then, you have an infinite loop with no exit condition 然后,您有一个无限循环,没有退出条件
while(1) {
...
}
which will continue to attempt concatenation until the computer catches fire. 它将继续尝试串联,直到计算机着火。 Worse, at the end of the
while()
loop you 更糟糕的是,在
while()
循环结束时,您
free(buf);
So on the subsequent, infinite, loops, you don't even have buf
to concatenate to. 因此,在随后的无限循环中,您甚至没有串联的
buf
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.