分段故障! [英] Segmentation fault!

查看:83
本文介绍了分段故障!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。


在我的主要功能中,我打印测试。作为第一件事。但是,如果我在打印调用之后运行调用

到node_alloc我得到一个分段错误并且测试是没有打印的那个



#include< stdlib.h>

#include< stdio.h>


typedef struct _node_t {

int num_kids;

void * content;

struct _node_t ** kids; //使指向node_ts的指针。

} node_t;


node_t * node_alloc(void * content,int num)

{

node_t * parent;

parent = malloc(sizeof(node_t));

parent-> content = content;

parent-> num_kids = num;

node_t * new_kids [num];

int i;


//将子项初始化为NULL。

for(i = 0; i< num; i ++)

{

parent - > kids [i] = NULL;


}

返回父级;

}

int main(void)

{


printf(" test");

node_t * root = node_alloc(" ; root",4);

返回0;

}

只有当我取消对node_alloc的调用时,它才会打印test。 !为什么

不打印测试然后给我分段错误?


似乎在printf调用之前执行对node_alloc的调用...

I have a wierd problem.

In my main function I print "test" as the first thing. But if I run the call
to node_alloc AFTER the printf call I get a segmentation fault and test is
not printed!

#include <stdlib.h>
#include <stdio.h>

typedef struct _node_t {
int num_kids;
void *content;
struct _node_t **kids; // Makes a pointer to a pointer of node_ts.
} node_t;

node_t *node_alloc(void *content, int num)
{
node_t *parent;
parent = malloc(sizeof(node_t));
parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];
int i;

// Initialize children to NULL.
for (i = 0; i < num; i++)
{
parent->kids[i]=NULL;

}
return parent;
}
int main(void)
{

printf("test");
node_t *root = node_alloc("Root",4);
return 0;
}
Only if I outcomment the call to node_alloc will it print "test"! Why does
it not print "test" and then afterwards give me the "Segmentation Fault"?

It seems that the call to node_alloc is executed before the printf call...

推荐答案

Paminu写道:
Paminu wrote:
我有一个奇怪的问题。

在我的主要功能中,我打印测试。作为第一件事。但是如果我在printf调用之后运行对node_alloc的调用,我会得到一个分段
错误并且不打印测试!

#include< stdlib.h>
#include< stdio.h>

typedef struct _node_t {
int num_kids;
void * content;
struct _node_t ** kids; //创建一个指向node_ts的指针。
} node_t;

node_t * node_alloc(void * content,int num)
{node / node * parent;
parent = malloc(sizeof(node_t));


您应该测试malloc()是否返回NULL。

parent-> content = content;
parent-> num_kids = num;
node_t * new_kids [num];
int i;


你错过了这一行:


parent-> kids = new_kids;


但是,你真的想要:


parent-> kids = malloc(num * sizeof(node_t *));

if (parent-> kids)...


退出函数后,`new_kids`将消失。

//将子项初始化为NULL。
for(i = 0; i< num ++)
{
parent-> kids [i] = NULL;

}
返回父母;
}

int main(无效)

printf(" test");


如果你没有用\ n终止printf(),你可能无法得到任何东西。

node_t * root = node_alloc(" Root" ;,4);
返回0;
}

只有当我取消对node_alloc的调用时,它才会打印出test!为什么
不打印测试然后给我分段
错误?

似乎在printf
调用之前执行对node_alloc的调用...
I have a wierd problem.

In my main function I print "test" as the first thing. But if I run
the call to node_alloc AFTER the printf call I get a segmentation
fault and test is not printed!

#include <stdlib.h>
#include <stdio.h>

typedef struct _node_t {
int num_kids;
void *content;
struct _node_t **kids; // Makes a pointer to a pointer of node_ts.
} node_t;

node_t *node_alloc(void *content, int num)
{
node_t *parent;
parent = malloc(sizeof(node_t));
You should test whether malloc() returns NULL.
parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];
int i;
You''re missing this line:

parent->kids = new_kids;

But then, you''d really want:

parent->kids = malloc(num * sizeof (node_t *));
if (parent->kids) ...

As `new_kids` will disappear after you exit the function.

// Initialize children to NULL.
for (i = 0; i < num; i++)
{
parent->kids[i]=NULL;

}
return parent;
}
int main(void)
{

printf("test");
If you don''t terminate printf() with \n you may not get anything out.
node_t *root = node_alloc("Root",4);
return 0;
}
Only if I outcomment the call to node_alloc will it print "test"! Why
does it not print "test" and then afterwards give me the "Segmentation
Fault"?

It seems that the call to node_alloc is executed before the printf
call...




不,你只是通过尝试

访问未初始化的指针来调用Undefined Behavior的愤怒......


-

BR,弗拉基米尔


什么!?我担心吗?

- A.E.纽曼



No, you just invoked the wrath of Undefined Behaviour, by trying to
access uninitialised pointers...

--
BR, Vladimir

What!? Me worry?
-- A.E. Newman




" Paminu" < SD ** @ asd.com>在消息中写道

news:ds ********** @ news.net.uni-c.dk ...

"Paminu" <sd**@asd.com> wrote in message
news:ds**********@news.net.uni-c.dk...
我有一个奇怪的问题。

在我的主要功能中,我打印测试。作为第一件事。但是如果我在printf调用之后运行对node_alloc的
调用,我会得到一个分段错误并且测试没有打印出来!

#include< stdlib.h>
#include< stdio.h>

typedef struct _node_t {
int num_kids;
void * content;
struct _node_t ** kids; //创建一个指向node_ts的指针。
} node_t;

node_t * node_alloc(void * content,int num)
{node / node * parent;
parent = malloc(sizeof(node_t));
parent-> content = content;
parent-> num_kids = num;
node_t * new_kids [num];
int i;

//将子项初始化为NULL。
for(i = 0; i< num; i ++)
{
parent-> ;孩子[i] = NULL;

}
返回父母;
}

int main(无效)
{

printf(" test");
node_t * root = node_alloc(" Root",4);
返回0;
}

只有当我取消对node_alloc的调用时,它才会打印test!为什么
不打印测试然后给我分段错误?

似乎在printf调用之前执行对node_alloc的调用...
I have a wierd problem.

In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is
not printed!

#include <stdlib.h>
#include <stdio.h>

typedef struct _node_t {
int num_kids;
void *content;
struct _node_t **kids; // Makes a pointer to a pointer of node_ts.
} node_t;

node_t *node_alloc(void *content, int num)
{
node_t *parent;
parent = malloc(sizeof(node_t));
parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];
int i;

// Initialize children to NULL.
for (i = 0; i < num; i++)
{
parent->kids[i]=NULL;

}
return parent;
}
int main(void)
{

printf("test");
node_t *root = node_alloc("Root",4);
return 0;
}
Only if I outcomment the call to node_alloc will it print "test"! Why does
it not print "test" and then afterwards give me the "Segmentation Fault"?

It seems that the call to node_alloc is executed before the printf call...




否,在printf()调用之后执行node_alloc()。既然你没有用b $ b fflush()标准输出或者使用换行符''\\'n'',使用printf(),打印输出

isn'直到您的程序退出时才会显示。此时,stdio通过exit()或return()刷新

,从而显示测试。 分段

故障可能是没有为数据分配空间的结果

''parent-> content''指向。由于你想要使用指针

''parent-> content'',最好为数据添加另一个malloc()分配

空间''content''然后使用strcpy()将数据复制到新的malloc()内存中。


我是很遗憾地说,但是这个确切的情况在过去一个月里已经被大概包含了十个不同的线程或不同程度的

的口才。

Rod Pemberton



No, node_alloc() is executed after the printf() call. Since you didn''t
fflush() stdout or use a newline, ''\n'', with printf(), the output from print
isn''t displayed until your program exits. At which point, stdio is flushed
by either exit() or return(), thereby, displaying test. The "Segmentation
Fault" is probably the result of not allocating space for the data
''parent->content'' points to. Since you are wanting to use pointers for
''parent->content'', it is probably best to add another malloc() allocate
space for the data at ''content'' and then use strcpy() to copy the data to
the newly malloc()''d memory.

I''m sorry to say, but this exact situation has been covered in probably ten
or fifteen different threads here over the past month with different degrees
of eloquence.
Rod Pemberton


>>似乎对node_alloc的调用是在printf
>> It seems that the call to node_alloc is executed before the printf
调用之前执行的...
call...



不,你刚刚调用了通过尝试访问未初始化的指针来解决未定义的行为...



No, you just invoked the wrath of Undefined Behaviour, by trying to
access uninitialised pointers...



好​​的原因是我没有初始化指针指针。


如果我有这个结构:


typedef struct _node_t {

void * content;

struct _node_t **孩子; //创建一个指向node_ts的指针。

} node_t;


我现在可以初始化一个指向这个结构的指针(我知道我应该使用

malloc如果我想在函数外面使用结果):


1)

node_t * np;

np = NULL; // INITIALIZE指针为空


这个编译,当我运行它时我没有分段错误。


为什么这与初始化不同指向

结构中指针的指针:


2)

node_t * np;

np = malloc(sizeof(node_t));

np-> content =" test";

np-> kids [0] = NULL; // INITIALIZE指针为空

在最后一行np-> kids [0]是指向node_t的指针,允许

指向NULL。编译得很好,但是当我运行它时,我得到一个分段

错误。我看不出1)和2)之间的区别。



Ok so the reason is that I have not initialized the pointer to the pointer.

If I have this struct:

typedef struct _node_t {
void *content;
struct _node_t **kids; // Makes a pointer to a pointer of node_ts.
} node_t;

I can now initialize a pointer to this struct (I know that I should use
malloc if I want to use the result outside the function):

1)
node_t *np;
np = NULL; // INITIALIZE A POINTER TO NULL

This compiles and when I run it I get no segmentation fault.

Why is this different from initializing the pointer to the pointer in the
struct by:

2)
node_t *np;
np=malloc(sizeof(node_t));
np->content = "test";
np->kids[0] = NULL; // INITIALIZE A POINTER TO NULL
In the last line "np->kids[0]" is a pointer to node_t which is allowed to
point to NULL. I compiles fine but when I run it I get a segmentation
fault. I can''t see the difference between 1) and 2).



这篇关于分段故障!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆