分割错误 [英] segmentation error
问题描述
我在下面提到的代码中出现分段错误而且我不是
能够弄清楚原因。
typedef struct sd {
int ps;
int cs;
} st;
void main()
{
st ** fe;
int n,j;
n = 11;
fe =(st **)calloc(n + 1,sizeof(st *));
for(j = 0; j< = n; j ++)
fe [j] =(st *)calloc(16,sizeof(st));
for(j = 0; j< = n; j ++)
free(fe [j]);
free(fe);
}
当我尝试使用gdb进行调试时,它指向fe [j] =(st *)
calloc(16,sizeof( st));
请建议。
问候,
JK
JK说:
我用下面提到的代码得到了分段错误,而且我不是./ b $ b能够弄明白为什么。
typedef struct sd {
int ps;
int cs;
} st;
void main()
int main(void)
获得正确的入口点。当你学会了C程序的开始时,
我们将为第2课做好准备。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名, - www。
嘿理查德,如果我打扰你,我很抱歉,只是试图拿你来
专家帮助解决我的问题。
实际上这是一个很大的代码,我还没写。这部分代码
是在main函数中调用的函数内部。
请耐心 - 我不是程序员。我来自VHDL
编程方面,这对我来说是加班工作...
问题是当我运行RTL回归时,我需要在每个测试用例中调用此c
程序exe。在随机点(测试用例46),这个
c程序输出崩溃。
当我尝试使用gdb进行调试时,它指向上面提到的行。
而不是浪费你的时间,我只是试图展示 - 有问题的
代码。
typedef struct sd {
int ps;
int cs;
} st;
int main()
{
st ** fe;
int n,j;
n = 11;
fe =(st **)calloc(n + 1,sizeof(st *));
for(j = 0; j< = n; j ++)
fe [j] =(st *)calloc(16,sizeof(st));
for(j = 0; j< = n; j ++)
免费(fe [j]);
免费(fe);
返回0;
}
问候,
JK
2007年5月6日21: 14:29 -0700,JK< kr **************** @ gmail.comwrote
comp.lang.c中的
:
< blockquote class =post_quotes>
我得到segm使用下面提到的代码进行错误,我不知道是否能够找出原因。
typedef struct sd {
int ps;
int cs;
} st;
void main()
正如理查德所说,你的程序有不明确的行为,因为
对main()的定义不正确。 C不知道或关心你的计划会发生什么。
$ blockquote class =post_quotes>
{
st ** fe;
int n,j;
n = 11;
fe =(st **)calloc(n +1,sizeof(st *));
删除calloc()返回的指针上的强制类型转换。如果您的
编译器在没有强制转换的情况下抱怨,请查看是否可以找出原因。
for(j = 0; j< = n ; j ++)
fe [j] =(st *)calloc(16,sizeof(st));
for(j = 0; j< = n; j ++)
免费(fe [j]);
免费(fe);
}
>
当我尝试使用gdb进行调试时,它指向fe [j] =(st *)
calloc(16,sizeof(st));
请建议。
问候,
JK
-
Jack Klein
主页: http:// JK-Technology.Com
常见问题解答
comp.lang.c http://c-faq.com/
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp .lang.learn.c-c ++
http://www.club.cc.cmu.edu/ ~ajo / docs / FAQ-acllc.html
Hi,
I am getting segmentation error with below-mentioned code and I am not
able to make out why.
typedef struct sd {
int ps;
int cs;
} st;
void main()
{
st **fe;
int n,j;
n = 11;
fe = (st **) calloc(n+1, sizeof(st *));
for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
for (j=0; j<=n; j++)
free(fe[j]);
free(fe);
}
When I tried to debug with gdb, it is pointing to fe[j] = (st *)
calloc(16, sizeof(st));
Please suggest.
Regards,
JK
JK said:
Hi,
I am getting segmentation error with below-mentioned code and I am not
able to make out why.
typedef struct sd {
int ps;
int cs;
} st;
void main()int main(void)
Get the entry point right. When you''ve learned how C programs start,
we''ll be ready for lesson 2.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Hey Richard, I am sorry if I disturbed you, but just trying to take u
experts help in fixing up my problem.
Actually it is a big code and I haven''t written it. This part of code
is inside a function which is being called in main function.
Please bear with me - I am not a c programmer. I am from VHDL
programming side and this is a overtime work for me...
The problem is when I run my RTL regressions, I need to call this c
program exe for every testcase. At a random point (testcase 46), this
c program output crashes.
When I tried to debug with gdb, it pointed to above mentioned line.
Instead of wasting your time, I just tried to show - the problematic
piece of code.
typedef struct sd {
int ps;
int cs;
} st;
int main()
{
st **fe;
int n,j;
n = 11;
fe = (st **) calloc(n+1, sizeof(st *));
for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
for (j=0; j<=n; j++)
free(fe[j]);
free(fe);
return 0;
}
regards,
JK
On 6 May 2007 21:14:29 -0700, JK <kr****************@gmail.comwrote
in comp.lang.c:
Hi,
I am getting segmentation error with below-mentioned code and I am not
able to make out why.
typedef struct sd {
int ps;
int cs;
} st;
void main()As Richard said, your program has undefined behavior because of the
incorrect definition of main(). C does not know or care what happens
with your program.
{
st **fe;
int n,j;
n = 11;
fe = (st **) calloc(n+1, sizeof(st *));Remove the casts on the pointer returned by calloc(). If your
compiler complains without the casts, see if you can figure out why.
for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
for (j=0; j<=n; j++)
free(fe[j]);
free(fe);
}
When I tried to debug with gdb, it is pointing to fe[j] = (st *)
calloc(16, sizeof(st));
Please suggest.
Regards,
JK--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
这篇关于分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!