在使用strcpy、malloc和struct时,我有一个分段错误(核心转储)
好的,当我运行这段代码时,我有一个分段错误:在使用strcpy、malloc和struct时,我有一个分段错误(核心转储),c,segmentation-fault,C,Segmentation Fault,好的,当我运行这段代码时,我有一个分段错误: #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 64 struct example { char *name; }; int main() { struct example *s = malloc (MAX); strcpy(s->name ,"Hello World!!"); re
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 64
struct example {
char *name;
};
int main()
{
struct example *s = malloc (MAX);
strcpy(s->name ,"Hello World!!");
return !printf("%s\n", s->name);
}
有人能解释一下发生了什么吗?您可能已经为结构分配了内存,但没有为其字符指针分配内存 无法对未分配的内存执行strcpy。你可以说
s->name = "Hello World";
相反
或者,为您的字符分配内存,然后执行复制。
注意:我绝不赞同以下代码是好的,只是它会工作
int main()
{
struct example *s = malloc(MAX);
s->name = malloc(MAX);
strcpy(s->name ,"Hello World!!");
return !printf("%s\n", s->name);
}
编辑:这里可能有一个更简洁的实现,但我仍然讨厌C风格的字符串
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define KNOWN_GOOD_BUFFER_SIZE 64
typedef struct example {
char *name;
} MyExample;
int main()
{
MyExample *s = (MyExample*) malloc( sizeof(MyExample) );
s->name = (char*) malloc(KNOWN_GOOD_BUFFER_SIZE);
strcpy(s->name ,"Hello World!!");
printf("%s\n", s->name);
free(s->name);
free(s);
return 0;
}
#包括
#包括
#包括
#定义已知的缓冲区大小64
typedef结构示例{
字符*名称;
}MyExample;
int main()
{
MyExample*s=(MyExample*)malloc(sizeof(MyExample));
s->name=(char*)malloc(已知缓冲区大小);
strcpy(s->name,“你好,世界!!”;
printf(“%s\n”,s->name);
免费(s->姓名);
免费的;
返回0;
}
您正在为结构分配内存,但char*名称仍然指向未初始化的内存。您还需要为char*分配内存。如果希望它是一个最大大小为64的字符串(创建后可以更改),请尝试以下操作:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 64
struct example {
char *name;
};
int main()
{
struct example *s = malloc(sizeof(struct example));
s->name = malloc(MAX * sizeof(char));
strcpy(s->name ,"Hello World!!");
return !printf("%s\n", s->name);
}
#包括
#包括
#包括
#定义最大64
结构示例{
字符*名称;
};
int main()
{
结构示例*s=malloc(sizeof(结构示例));
s->name=malloc(MAX*sizeof(char));
strcpy(s->name,“你好,世界!!”;
return!printf(“%s\n”,s->name);
}
注意,我只将MAX分配给char*。示例结构只需要是sizeof(struct example)),因此没有必要将其设为最大值。这是一种更好的方法,因为您的malloc将不断为您提供准确的大小,即使您更改了示例结构的成员。问题在于,您正在为
s
分配内存,而不是为s->name
分配内存,因此您正在对未初始化的指针执行间接寻址,该指针为。假设您确实希望为一个结构示例
分配空间,并且希望字符串的大小为MAX
,则需要以下分配:
struct example *s = malloc (sizeof(struct example));
s->name = malloc(MAX*sizeof(char)) ;
请注意,使用来确定为其分配内存的数据类型的正确大小
struct example *s = malloc (MAX);
该行指向一个能够容纳64个示例结构的内存。每个示例结构只有足够的内存来容纳指针(称为name)
这是无效的,因为s->name没有指向任何地方,它需要指向已分配的内存
您可能想要:
struct example *s = malloc(sizeof(struct example)); //Allocate one example structure
s->name = malloc(MAX); //allocate 64 bytes for name
strcpy(s->name,"Hello world!"); //This is ok now.
这与:
struct example s; //Declare example structure
s.name = malloc(MAX); //allocate 64 bytes to name
strcpy(s.name,"Hello world!");
你想让你的绳子最大尺寸吗?
struct example *s = malloc(sizeof(struct example)); //Allocate one example structure
s->name = malloc(MAX); //allocate 64 bytes for name
strcpy(s->name,"Hello world!"); //This is ok now.
struct example s; //Declare example structure
s.name = malloc(MAX); //allocate 64 bytes to name
strcpy(s.name,"Hello world!");