C语言 简单地创建一个数据结构,它怎么能出现段错误?



我有一个简单的数据结构程序,但它有段错误,这让我非常沮丧,因为我完全不知道为什么。 任何人都可以指出有关此代码的任何内容(甚至不相关(吗? 谢谢

#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{  
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = NULL;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile **king = NULL;
*king = create_profile('K', 1, 1);
return 0;
}

你的国王是一个指向结构的指针。 你需要一些地方来存储要结构的指针,但这不是你分配的。
您可以引入指针来解决此问题。

int main()
{
/* introduce and NULL-init a pointer to struct */
profile* prince = NULL;
/* actually the init to NULL is not necessary,
because prince gets initialised later indirectly via king
(credits to alk), but it does not hurt and initialising everything
is a good habit. */ 
/* Introduce a pointer to pointer to struct,
initialised with the address of above pointer to struct,
the address of the above variable "prince" to be precise and clear.
The space for that automatic local variable is not dynamic,
it does not require a malloc. */
profile **king = &prince;
/* what the king is pointing to, i.e. the prince,
gets assigned what the function returns,
which is a cleanly allocated pointer to a new struct. */
*king = create_profile('K', 1, 1);
/* if king were still NULL, above statement would try to write
(the cleanly allocated pointer) into memory by dereferencing NULL ... 
segfault! 
(Well not necessarily, as alk points out, credits.
But dereferencing NULL is undefibed behaviour,  
an almost guaranteed way to trouble in the long run
and in case a seggault is observed, a likely explanation.
*/ 
return 0;
}

profile **king = NULL;
*king = ...

取消引用NULL,它本身已经调用了未定义的行为。在此之上,代码然后尝试在那里编写。不好,因为NULL不指向有效内存。

您必须先将内存分配给profile **king,然后再将内存分配给*king

#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{  
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = NULL;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile **king = (profile**) malloc(sizeof(profile*)); // <-- see here
*king = create_profile('K', 1, 1);
return 0;
}

代码段错误

*king = create_profile('K', 1, 1);

因为您已经将king定义为

profile **king = NULL;

这意味着 king 现在是指向指针的指针。但是king实际上并没有指向任何地方,你说它指向NULL.取消引用指向NULL的指针将导致段错误。

如果改为将king定义为

profile * king = NULL;

并像

king = create_profile('K', 1, 1);

代码不应再出现段错误。

您对使用双指针感到困惑。尝试像这样使用

#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(profile *head,char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = head;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile *king = NULL;
king = create_profile(king,'K', 1, 1);
return 0;
}

最新更新