使用指针的结构出错
Structures using pointers get wrong
我编写了构建器 bob 的代码,它是一个结构,构建器的每个 bob 都有名称和另外两个整数(并不重要)。一共有三个函数
初始化结构(使用"bob"和0和3)
第二个函数得到两个结构,需要在这些结构之间复制
第三个功能是释放每个bob的名字(char*)
一是第二个函数(copy)在debug时出错,因为没有复制名字(请大家帮忙分析一下原因),二是free函数代码崩溃了。谁能告诉我如何释放结构的名称(char*)?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LENGTH_OF_BOB 4
typedef struct bobTheBuilder
{
char* name;
int fixed;
int maxFix;
}bob;
//typedef struct bobTHeBuilder bob;
void deleteBob(bob currBob);
void initBob(bob *currBob);
void copyStruct(bob* dst, bob src);
int main(void)
{
bob currBob = {0,0,0};
bob secondBob;
initBob(&currBob);
copyStruct(&secondBob, currBob);
deleteBob(currBob);
deleteBob(secondBob);
system("PAUSE");
return 0;
}
/*
*/
void initBob(bob *currBob)
{
char* str = (char*)calloc(LENGTH_OF_BOB, sizeof(char));
char string[] = "bob";
if (str)
{
strcat(string, "[=10=]");
str = string;
currBob->name = str;
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
/*
*/
void deleteBob(bob currBob)
{
free(currBob.name);
}
void copyStruct(bob* dest, bob src)
{
dest->fixed = src.fixed;
dest->maxFix = src.maxFix;
dest->name = (char*)malloc(sizeof(char) *LENGTH_OF_BOB);
strncpy(dest->name, src.name, LENGTH_OF_BOB);
}
在 initBob
你有:
char* str = (char*)calloc(LENGTH_OF_BOB, sizeof(char));
char string[] = "bob";
str = string;
currBob->name = str;
设置 currBob->name
指向一个 局部自动变量 。不是动态分配的缓冲区。当函数退出时,自动变量超出范围,因此不再有效。当然,它不能被释放,因为它不是动态分配的内存。
我不太确定你想在那里做什么。除了错误地将 str
设置为指向局部变量之外,您还有一个不必要的 strcat
。我猜你正试图 NUL 终止缓冲区。但这是不必要的,因为使用字符串文字初始化未调整大小的 char 数组已经保证 NUL 终止。
考虑到这些问题,initBob
函数应该更像:
void initBob(bob *currBob)
{
currBob->name = calloc(LENGTH_OF_BOB, sizeof(char));
if (currBob->name)
{
strcpy(currBob->name, "bob");
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
我不知道这是否只是一个学习如何做的简单示例,或者它是否真的是您的范围,但如果您需要这样做,请使用 :strdup()
void initBob(bob *currBob)
{
if (currBob->name)
{
currBob->name=strdup("bob");
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
你不必在某个地方释放它,因为 malloc() 字符串...它是 ANSI 标准
我编写了构建器 bob 的代码,它是一个结构,构建器的每个 bob 都有名称和另外两个整数(并不重要)。一共有三个函数
初始化结构(使用"bob"和0和3)
第二个函数得到两个结构,需要在这些结构之间复制
第三个功能是释放每个bob的名字(char*)
一是第二个函数(copy)在debug时出错,因为没有复制名字(请大家帮忙分析一下原因),二是free函数代码崩溃了。谁能告诉我如何释放结构的名称(char*)?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LENGTH_OF_BOB 4
typedef struct bobTheBuilder
{
char* name;
int fixed;
int maxFix;
}bob;
//typedef struct bobTHeBuilder bob;
void deleteBob(bob currBob);
void initBob(bob *currBob);
void copyStruct(bob* dst, bob src);
int main(void)
{
bob currBob = {0,0,0};
bob secondBob;
initBob(&currBob);
copyStruct(&secondBob, currBob);
deleteBob(currBob);
deleteBob(secondBob);
system("PAUSE");
return 0;
}
/*
*/
void initBob(bob *currBob)
{
char* str = (char*)calloc(LENGTH_OF_BOB, sizeof(char));
char string[] = "bob";
if (str)
{
strcat(string, "[=10=]");
str = string;
currBob->name = str;
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
/*
*/
void deleteBob(bob currBob)
{
free(currBob.name);
}
void copyStruct(bob* dest, bob src)
{
dest->fixed = src.fixed;
dest->maxFix = src.maxFix;
dest->name = (char*)malloc(sizeof(char) *LENGTH_OF_BOB);
strncpy(dest->name, src.name, LENGTH_OF_BOB);
}
在 initBob
你有:
char* str = (char*)calloc(LENGTH_OF_BOB, sizeof(char));
char string[] = "bob";
str = string;
currBob->name = str;
设置 currBob->name
指向一个 局部自动变量 。不是动态分配的缓冲区。当函数退出时,自动变量超出范围,因此不再有效。当然,它不能被释放,因为它不是动态分配的内存。
我不太确定你想在那里做什么。除了错误地将 str
设置为指向局部变量之外,您还有一个不必要的 strcat
。我猜你正试图 NUL 终止缓冲区。但这是不必要的,因为使用字符串文字初始化未调整大小的 char 数组已经保证 NUL 终止。
考虑到这些问题,initBob
函数应该更像:
void initBob(bob *currBob)
{
currBob->name = calloc(LENGTH_OF_BOB, sizeof(char));
if (currBob->name)
{
strcpy(currBob->name, "bob");
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
我不知道这是否只是一个学习如何做的简单示例,或者它是否真的是您的范围,但如果您需要这样做,请使用 :strdup()
void initBob(bob *currBob)
{
if (currBob->name)
{
currBob->name=strdup("bob");
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
你不必在某个地方释放它,因为 malloc() 字符串...它是 ANSI 标准