segmentation error

J

JK

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
 
R

Richard Heathfield

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.
 
J

JK

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
 
J

Jack Klein

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
 
R

Richard Heathfield

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.
 
J

JK

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;
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,

#include <stdlib.h>
stdlib.h, math.h, stdio.h are included.
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
 
R

Richard Heathfield

JK said:

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.
 
M

Martin Ambuhl

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()
^^^^
You've already lost. Anything after this is irrelevant.
 
P

pete

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()
{
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.

/* BEGIN new.c */

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

typedef struct sd {
int ps;
int cs;
} st;

int main(void)
{
st **fe;
int n, j;

n = 11;
fe = calloc(n + 1, sizeof *fe);
if (fe != NULL) {
for (j = 0; j <= n; j++) {
fe[j] = calloc(16, sizeof *fe[j]);
if (fe[j] == NULL) {
printf("fe[%d] is equal to NULL\n", j);
while (j-- != 0) {
free(fe[j]);
}
free(fe);
fe = NULL;
}
}
}
if (fe != NULL) {
for (j = 0; j <= n; j++) {
free(fe[j]);
}
free(fe);
}
puts("teh enb!");
return 0;
}

/* END new.c */
 
J

JK

JK said:
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;
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.

/* BEGIN new.c */

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

typedef struct sd {
int ps;
int cs;

} st;

int main(void)
{
st **fe;
int n, j;

n = 11;
fe = calloc(n + 1, sizeof *fe);
if (fe != NULL) {
for (j = 0; j <= n; j++) {
fe[j] = calloc(16, sizeof *fe[j]);
if (fe[j] == NULL) {
printf("fe[%d] is equal to NULL\n", j);
while (j-- != 0) {
free(fe[j]);
}
free(fe);
fe = NULL;
}
}
}
if (fe != NULL) {
for (j = 0; j <= n; j++) {
free(fe[j]);
}
free(fe);
}
puts("teh enb!");
return 0;

}

/* END new.c */

Thanx Pete.

Regards,
JK
 
E

Eric Sosman

Richard said:
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]?

(It seems to me the fault most likely stems from the
failure to include <stdlib.h>, as you noted earlier.)
 
R

Richard Heathfield

Eric Sosman said:
Richard said:
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.
 
D

Default User

JK said:


Please trim your replies to the minimum needed for context. In
particular, remove signature blocks from the replies (Google won't do
it automatically for you the way most newsreaders do).




Brian
 
K

Keith Thompson

Richard Heathfield said:
Eric Sosman said:
Richard said:
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. For example (using a
constant rather than a variable):

#define N 10
int arr[N];

for (j = 0; j < N; j ++) { ... }

The use of "<=" in a for loop, as above:

for (j=0; j<=n; j++)
...

is often a sign that the author *should* have used "<" rather than
"<=", and is going past the end of the array. There's nothing
incorrect in the original code's handling of the array indices (as far
as I know, I haven't studied it in detail), but it would have been
clearer and more idiomatic to set n to 12 and use:

for (j = 0; j < n; j ++)
...

and perhaps also to give "n" a name that indicates more clearly what
it represents.
 
R

Richard

Martin Ambuhl said:
^^^^
You've already lost. Anything after this is irrelevant.

You must be a bundle of fun to work with. But coming back into the real
world ...
 
D

Dave Vandervies

You must be a bundle of fun to work with. But coming back into the real
world ...

In the real world, getting the basics right is the bare minimum for
being taken seriously.


dave
 
M

Martin Ambuhl

Richard said:
You must be a bundle of fun to work with. But coming back into the real
world ...

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."
 
K

Keith Thompson

Martin Ambuhl said:
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, unfortunately, I doubt that that's true. It's
certainly not the case that "void main()" prevents one from making a
living as an author of books about C.

On the other hand, although "void main()" invokes undefined behavior
(assuming a hosted implementation, unless the implementation
specifically documents it as an extension), most real-world
implementations will either reject it outright *or* treat it as nearly
equivalent to the correct "int main(void)" (possibly with a warning).
I've never seen a compiler do anything else, though of course my
experience is far from universal.

Given the lamentable existence of books that endorce "void main()",
and given that many implementations do not reject it, I generally
prefer to point out that it's an error *and* to go on and comment on
the rest of the code. If a program uses "void main()" and misbehaves,
there's probably some *other* reason for it.
 
R

Richard Heathfield

Keith Thompson said:

Given the lamentable existence of books that endorce "void main()",
and given that many implementations do not reject it, I generally
prefer to point out that it's an error *and* to go on and comment on
the rest of the code. If a program uses "void main()" and misbehaves,
there's probably some *other* reason for it.

Unless it's a VAX, of course. Anyway, the way I see it, until people can
grok something as simple as main's return type, there is little point
in trying to teach them anything else. That is why I am getting into
the habit of either ignoring void main posts completely, or pointing
out just that one error and leaving it there.
 
E

Eric Sosman

Keith said:
Richard Heathfield said:
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?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top