Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   segmentation fault (http://www.velocityreviews.com/forums/t445017-segmentation-fault.html)

ramu 11-06-2006 02:07 PM

segmentation fault
 
Hi,
While am trying to run this code am getting segmentation fault.
What's wrong with this code?
Here am trying to assign a value to the variable b.

#define SIZE 10

struct y {
int a;
struct x {
int b;
} *c[SIZE];
} d;


main()
{

d.c[SIZE] = malloc(10 * sizeof(struct x));
d.c[0]->b=10;
printf("%d",d.c[0]->b);
}

Regards


Chris Dollin 11-06-2006 02:35 PM

Re: segmentation fault
 
ramu wrote:

> Hi,
> While am trying to run this code am getting segmentation fault.
> What's wrong with this code?
> Here am trying to assign a value to the variable b.
>
> #define SIZE 10
>
> struct y {
> int a;
> struct x {
> int b;
> } *c[SIZE];
> } d;


[Horrid names. I assume they're just for illustration.]

> main()
> {
>
> d.c[SIZE] = malloc(10 * sizeof(struct x));


BOOM. `d.c` has only SIZE elements, indexed from 0 to
`SIZE - 1`. So your code has already broken. And you
didn't #include stdlib.h, so your use of `malloc` is
broken too. (Didn't your compiler complain about that?)

It's quite possible that this didn't cause your problem,
however.

> d.c[0]->b=10;


You haven't assigned to `d.c[0]`. Since `d` is static,
its elements are initialised to zero. So `d.c[0]` is
a null pointer. You've tried to assign to a field of
a structure the null pointer doesn't point to.

BOOM. Very likely the point at which you get your
segfault.

--
Chris ".enable proofreading" Dollin
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/


mark_bluemel@pobox.com 11-06-2006 03:21 PM

Re: segmentation fault
 

ramu wrote:

> Hi,
> While am trying to run this code am getting segmentation fault.
> What's wrong with this code?
> Here am trying to assign a value to the variable b.
>
> #define SIZE 10
>
> struct y {
> int a;
> struct x {
> int b;
> } *c[SIZE];
> } d;
>
>
> main()
> {
>
> d.c[SIZE] = malloc(10 * sizeof(struct x));


I think you are trying to dynamically allocate the array of SIZE
pointers to struct x items here. It won't work.

Depending on what you are trying to achieve, you could do this :-

int i;
for (i = 0;i < SIZE;i++) {
d.c[i] = malloc(sizeof(struct x));
} /* error handling is missing */

still remembering that you can only access from d.c[0] to d.c[SIZE -
1], so you could do

d.c[10]->b = 42;

or you could change the structure declaration

struct y {
int a;
struct x {
int b;
} *c;
} d;
....
d.c = malloc(sizeof(struct x) * SIZE);
/* or d.c = calloc(SIZE, sizeof(struct x));
* again error handling is missing
*/
d.c[SIZE - 1].b = 42;

I'll now stand back and let others find the holes in what I've
suggested :-)



All times are GMT. The time now is 02:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.