calloc上的分段错误 [英] segmentation fault on calloc
问题描述
我在calloc上有一个分段错误,我不明白为什么?
这是我使用的:
typedef struct noeud {
int val;
struct noeud * fgauche;
struct noeud * fdroit;
} * arbre ; //对于那些不会说法语的人来说,arbre意味着树。
这是我使用calloc的函数:
arbre CreerNoeud(int valeur,arbre fg,arbre fd){
arbre A;
A =(arbre)calloc(1,sizeof(struct noeud)); //分段错误只需
这里。
A-> val = valeur;
A-> fgauche = fg;
A-> fdroit = fd;
返回A;
}
我也试过A = (arbre)calloc(1,sizeof(struct noeud));
和
A =(arbre *)calloc(1,sizeof(struct noeud));
但它不起作用。请帮帮我!
I''ve got a segmentation fault on a calloc and I don''tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those who don''t speak french arbre means tree.
An this is the fonction where I use the calloc :
arbre CreerNoeud(int valeur, arbre fg, arbre fd){
arbre A;
A=(arbre)calloc(1,sizeof(struct noeud)); //segmentation fault just
here.
A->val=valeur;
A->fgauche=fg;
A->fdroit=fd;
return A;
}
I also tried A=(arbre)calloc(1,sizeof(struct noeud));
and
A=(arbre*)calloc(1,sizeof(struct noeud));
but it doesn''t work. Help me please !
推荐答案
laberth写道:
laberth wrote:
我有一个分段错误calloc,我不明白为什么?
我能想到的最简单的解释是你
可能已经忘记''#include< stdlib.h>''声明calloc() 。
这让我想起:
A =(arbre)calloc(1,sizeof(struct noeud)); //分段错误只是
I''ve got a segmentation fault on a calloc and I don''tunderstand why?
The simplest explanation I can think of is that you
may have forgotten ''#include <stdlib.h>'' which declares calloc().
Which reminds me:
A=(arbre)calloc(1,sizeof(struct noeud)); //segmentation fault just
在C程序中,不要使用malloc,calloc&合。它不是必需的b $ b,并且隐藏了忘记声明calloc的错误。
无论如何,如果stdlib.h没有解决问题,可能问题是
其他地方 - 例如,你修改一些释放的内存,这可能会破坏
malloc的内部结构。如果是这样,以下程序不应该是b
段错误,你必须在其他地方寻找问题。一个malloc
调试器,如商业Purify或免费的Elecetric Fence
( ftp://ftp.perens.com/pub/ElectricFence/ )可能会检测到你的问题。
#include< stdlib.h>
typedef struct noeud {
int val;
struct noeud * fgauche;
struct noeud * fdroit;
} * arbre;
arbre CreerNoeud(int valeur,arbre fg, arbre fd){
arbre A;
A = calloc(1,sizeof(struct noeud));
A-> val = valeur;
A-> fgauche = fg;
A-> fdroit = fd;
返回A;
}
int main(){
(void)CreerNoeud(0,NULL,NULL);
返回0;
}
-
Hallvard
In C programs, don''t cast the result of malloc, calloc & co. It is not
necessary, and hides bugs like forgetting to declare calloc.
Anyway, if stdlib.h did not fix the problem, maybe the problem is
elsewhere - e.g., you modify some freed memory, which can corrupt
malloc''s internal structures. If so, the following program should not
segfault, and you''ll have to look for the problem elsewhere. A malloc
debugger like the commercial Purify or the free Elecetric Fence
(ftp://ftp.perens.com/pub/ElectricFence/) may detect the problem for
you.
#include <stdlib.h>
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre;
arbre CreerNoeud(int valeur, arbre fg, arbre fd) {
arbre A;
A=calloc(1,sizeof(struct noeud));
A->val=valeur;
A->fgauche=fg;
A->fdroit=fd;
return A;
}
int main() {
(void) CreerNoeud(0, NULL, NULL);
return 0;
}
--
Hallvard
< 1a是***** @ voila.fr>在消息中写道
news:84 ************************** @ posting.google.c om ...
<la*****@voila.fr> wrote in message
news:84**************************@posting.google.c om...
我在calloc上有一个分段错误,我不明白为什么?
这是我使用的:
typedef struct noeud {
int val;
struct noeud * fgauche;
struct noeud * fdroit;
} * arbre; //对于那些不会说法语的人来说,意味着树。
通常不会以这种方式输入defde指针。只需
typedef结构,留下指针(*)以便于
结构noeud对象的后续声明中的可读性。
IE更喜欢...
arbre * A;
....结束......
arbre A;
这是我使用calloc的函数:
arbre CreerNoeud(int valeur,arbre fg,arbre fd){
arbre A;
A =(arbre)calloc(1,sizeof(struct noeud)); //分段错误只是
这里。
我猜你没有包含< stdlib.h>。即你没有为calloc()提供一个
函数原型。
A-> val = valeur;
A-> fgauche = fg;
A-> fdroit = fd;
并不是说calloc会正确地将指针初始化为null(至少
不可移植),但是既然你破坏了零编内容,为什么还要烦恼呢?
calloc?为什么不直接使用...
A = malloc(sizeof * A);
如果编译失败,那么我的猜测是可能是正确的。
返回A;
}
I''ve got a segmentation fault on a calloc and I don''tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those who don''t speak french arbre means tree.
It''s generally not considered good form to typedef pointers this way. Just
typedef the struct, leave the pointer (*) for the readability in the
subsequent declarations of struct noeud objects.
I.E. prefer...
arbre *A;
....over...
arbre A;
An this is the fonction where I use the calloc :
arbre CreerNoeud(int valeur, arbre fg, arbre fd){
arbre A;
A=(arbre)calloc(1,sizeof(struct noeud)); //segmentation fault just
here.
I''m guessing you didn''t include <stdlib.h>. I.e. you didn''t supply a
function prototype for calloc().
A->val=valeur;
A->fgauche=fg;
A->fdroit=fd;
Not that calloc will properly initialise pointers to null anyway (at least
not portably), but since you clobber the zero-ed contents, why bother with
calloc? Why not just use...
A = malloc(sizeof *A);
If this fails to compile, then my guess was likely correct.
return A;
}
-
彼得
--
Peter
Peter Nilsson< ai *** @ acay.com.au>这样说:
Peter Nilsson <ai***@acay.com.au> spoke thus:
并不是说calloc会正确地初始化指向null的指针(至少
Not that calloc will properly initialise pointers to null anyway (at least
^ not
以防万一有人感到困惑,虽然你的其余部分
表明你不是。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。欢迎。
^not
Just in case anyone was confused, although the rest of your sentence
indicates that you were not.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于calloc上的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!