 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JK Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: segmentation error |
|
|
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 |
|
| Back to top |
|
 |
Martin Ambuhl Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
JK wrote:
| Quote: | 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()
^^^^ |
You've already lost. Anything after this is irrelevant. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
JK said:
<snip>
| Quote: | stdlib.h, math.h, stdio.h are included.
|
We are not mind-readers. When asking for help, please post the smallest
COMPLETE program that demonstrates the problem you are having.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
JK Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
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 |
|
| Back to top |
|
 |
JK Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
On May 7, 10:02 am, Richard Heathfield <r...@see.sig.invalid> wrote:
| Quote: | JK said:
Hey Richard, I am sorry if I disturbed you,
You didn't.
but just trying to take u experts help in fixing up my problem.
Great!
Please bear with me - I am not a c programmer.
Consider hiring one.
typedef struct sd {
int ps;
int cs;
} st;
int main()
{
st **fe;
int n,j;
n = 11;
fe = (st **) calloc(n+1, sizeof(st *));
Here is your second problem, which you have managed to obscure with a
pointless cast. When you rewrite it like this:
fe = calloc(n + 1, sizeof *fe);
and re-compile, you will discover that the compiler issues a diagnostic
message. Why? Well, the compiler will not be able to find a prototype
for calloc, so it will make a default assumption (as the Standard
requires) about the return value of the calloc function, and that
assumption is unfortunate where calloc is concerned. This error is
happening anyway, but your cast conceals it.
You must fix the real problem by telling the compiler the true return
type of calloc, which you do by adding this line at the top of your
program:
#include <stdlib.h
Also, you mustn't just assume that the memory request succeeded. If
calloc could not allocate the memory you wanted, it will return NULL.
Test for this.
for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
Same applies here. Also note that you allocated space for twelve (n is
11, and you asked for n + 1) objects, but here you try to access
thirteen (indices 0 through 12, count them). Hence your segfault.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
|
Richard,
| Quote: | #include <stdlib.h
stdlib.h, math.h, stdio.h are included. |
| Quote: | for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
Same applies here. Also note that you allocated space for twelve (n is
11, and you asked for n + 1) objects, but here you try to access
thirteen (indices 0 through 12, count them). Hence your segfault.
|
I didn't get this point. How I am trying to access thirteen??? indices
are 0 to 11, so it will be 12 only...
Regards,
JK |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
JK said:
| Quote: | Hey Richard, I am sorry if I disturbed you,
|
You didn't.
| Quote: | but just trying to take u experts help in fixing up my problem.
|
Great!
| Quote: | Please bear with me - I am not a c programmer.
|
Consider hiring one.
| Quote: | typedef struct sd {
int ps;
int cs;
} st;
int main()
{
st **fe;
int n,j;
n = 11;
fe = (st **) calloc(n+1, sizeof(st *));
|
Here is your second problem, which you have managed to obscure with a
pointless cast. When you rewrite it like this:
fe = calloc(n + 1, sizeof *fe);
and re-compile, you will discover that the compiler issues a diagnostic
message. Why? Well, the compiler will not be able to find a prototype
for calloc, so it will make a default assumption (as the Standard
requires) about the return value of the calloc function, and that
assumption is unfortunate where calloc is concerned. This error is
happening anyway, but your cast conceals it.
You must fix the real problem by telling the compiler the true return
type of calloc, which you do by adding this line at the top of your
program:
#include <stdlib.h>
Also, you mustn't just assume that the memory request succeeded. If
calloc could not allocate the memory you wanted, it will return NULL.
Test for this.
| Quote: | for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
|
Same applies here. Also note that you allocated space for twelve (n is
11, and you asked for n + 1) objects, but here you try to access
thirteen (indices 0 through 12, count them). Hence your segfault.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Mon May 07, 2007 9:11 am Post subject: Re: segmentation error |
|
|
On 6 May 2007 21:14:29 -0700, JK <krishna.janumanchi (AT) gmail (DOT) com> wrote
in comp.lang.c:
| Quote: | 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.
| Quote: | {
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.
| Quote: | 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 |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Mon May 07, 2007 9:12 am Post subject: Re: segmentation error |
|
|
JK said:
| Quote: | 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. |
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Tue May 08, 2007 7:05 am Post subject: Re: segmentation error |
|
|
Keith Thompson wrote:
| Quote: | Richard Heathfield <rjh (AT) see (DOT) sig.invalid> writes:
Eric Sosman said:
Richard Heathfield wrote:
JK said:
for (j=0; j<=n; j++)
fe[j] = (st *) calloc(16, sizeof(st));
Same applies here. Also note that you allocated space for twelve (n
is 11, and you asked for n + 1) objects, but here you try to access
thirteen (indices 0 through 12, count them). Hence your segfault.
Where is the use of index [12]?
In my fetid imagination, it appears. My apologies to the OP.
In Richard's defense, the OP's code is a bit misleading here. The
usual idiom in C is to keep track of the number of elements in an
array rather than the index of the last element. [...]
|
Confession: I, too, said "Bug! Bug! Bug!" (cue sound
track from the shower murder scene in "Psycho") as soon as I
saw the O.P.'s `<=', but (for a change) caught my mistake
before posting.
There may be dual lessons about patterns here: One, that
they help us to write correct code because we've debugged the
patterns themselves and needn't reconsider them, but Two, that
we are prone to misconstrue a program that departs from the
pattern. We see `malloc(strlen(s))' and think "Bug! Bug! Bug!"
and only later notice that the allocated area is not being used
to hold a copy of the string. We see `isalpha(*str)' and think
"Bug! Bug! Bug!" until we see that `str' is an `unsigned char*'.
We see `defualt:' and think "Bug! Bug! Bug!" until we find the
corresponding `goto defualt;' and instead turn our attention to
the author of the code: "Bastard! Bastard! Bastard!"
Patterns are useful, patterns are powerful, but patterns can
also deceive. "Christ! What are patterns for?"
--
Eric Sosman
esosman@acm-dot-org.invalid |
|
| Back to top |
|
 |
Richard Bos Guest
|
Posted: Wed May 09, 2007 9:11 am Post subject: Re: segmentation error |
|
|
Martin Ambuhl <mambuhl (AT) earthlink (DOT) net> wrote:
| Quote: | Bart van Ingen Schenau wrote:
Martin Ambuhl wrote:
In the real world, "void main()" insures that you don't work anywhere
very long, so no one knows what kind of other mad antics you pull to
make yourself "a bundle of fun."
In the real world, most programmers don't get to write a function
called 'main' (at least not for production code), so they can't screw
up there.
In the real world, a programmer so clueless that he would, given the
chance to write a function called 'main', write "void main()" will
assuredly screw up other things to the point of unemployment.
|
Oh, if only. But after all these years and with all this evidence,
Microsoft are still in business.
Richard |
|
| Back to top |
|
 |
jaysome Guest
|
Posted: Wed May 09, 2007 9:11 am Post subject: Re: segmentation error |
|
|
On Wed, 09 May 2007 08:01:27 GMT, rlb@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
| Quote: | Martin Ambuhl <mambuhl (AT) earthlink (DOT) net> wrote:
Bart van Ingen Schenau wrote:
Martin Ambuhl wrote:
In the real world, "void main()" insures that you don't work anywhere
very long, so no one knows what kind of other mad antics you pull to
make yourself "a bundle of fun."
In the real world, most programmers don't get to write a function
called 'main' (at least not for production code), so they can't screw
up there.
In the real world, a programmer so clueless that he would, given the
chance to write a function called 'main', write "void main()" will
assuredly screw up other things to the point of unemployment.
Oh, if only. But after all these years and with all this evidence,
Microsoft are still in business.
|
And so are a lot of embedded compiler vendors, who realize that
99.9999999% of their users write code like this:
void main(void)
{
/* loop forever */
for ( ; ; )
{
/* handle whatever */
}
}
I prefer a return type of void for main() in embedded (free-standing)
applications, provided the compiler supports it, of course.
--
jay |
|
| Back to top |
|
 |
Chris Torek Guest
|
Posted: Thu May 10, 2007 9:11 am Post subject: Re: segmentation error |
|
|
In article <fm13439od1ljrgvecbfffbht8el3fuksoi (AT) 4ax (DOT) com>
jaysome <jaysome (AT) hotmail (DOT) com> wrote:
| Quote: | I prefer a return type of void for main() in embedded (free-standing)
applications, provided the compiler supports it, of course.
|
Personally, in such systems I prefer not to call the C entry point
"main" at all. This avoids any confusion.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|