字符串连接错误与 malloc 动态内存分配
String concatenate error with malloc dynamic memory allocation
字符串 *char 与 malloc 动态内存分配连接错误
我想做一个连接字符串的函数,它可以工作但是它给出了一个错误并且处理器重新启动,我认为指针有问题,但我不知道它是什么,内存分配问题。
提前致谢!
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] = '[==]';
strcat(new_str,str1);
strcat(new_str,str2);
return new_str;
}
串行输出...
#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
您对 my_strcat
的第一次调用具有未定义的行为,因为 buf
之前未初始化。正是这一行是问题所在
new_str = malloc(strlen(str1)+strlen(str2)+1);
strlen(str1)
其中 str1
未初始化。
建议,使用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] = '[=11=]';
strcat(new_str, str2);
return new_str;
}
和
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);
您在 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
您需要:
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);
您对 buf
的使用不合逻辑。你没有显示它是如何分配的,但即使你为 buf
做了 malloc()
内存,你也用
覆盖了它
buf = "";
然后,你有一个没有退出条件的无限循环
while(1) {
...
}
它将继续尝试串联,直到计算机着火。更糟糕的是,在 while()
循环结束时你
free(buf);
所以在随后的无限循环中,您甚至没有 buf
可以连接到。
字符串 *char 与 malloc 动态内存分配连接错误
我想做一个连接字符串的函数,它可以工作但是它给出了一个错误并且处理器重新启动,我认为指针有问题,但我不知道它是什么,内存分配问题。
提前致谢!
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] = '[==]';
strcat(new_str,str1);
strcat(new_str,str2);
return new_str;
}
串行输出...
#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
您对 my_strcat
的第一次调用具有未定义的行为,因为 buf
之前未初始化。正是这一行是问题所在
new_str = malloc(strlen(str1)+strlen(str2)+1);
strlen(str1)
其中 str1
未初始化。
建议,使用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] = '[=11=]';
strcat(new_str, str2);
return new_str;
}
和
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);
您在 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
您需要:
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);
您对 buf
的使用不合逻辑。你没有显示它是如何分配的,但即使你为 buf
做了 malloc()
内存,你也用
buf = "";
然后,你有一个没有退出条件的无限循环
while(1) {
...
}
它将继续尝试串联,直到计算机着火。更糟糕的是,在 while()
循环结束时你
free(buf);
所以在随后的无限循环中,您甚至没有 buf
可以连接到。