segmentation fault

Discussion in 'C Programming' started by ramu, Nov 6, 2006.

  1. ramu

    ramu Guest

    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
     
    ramu, Nov 6, 2006
    #1
    1. Advertising

  2. ramu

    Chris Dollin Guest

    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/
     
    Chris Dollin, Nov 6, 2006
    #2
    1. Advertising

  3. ramu

    Guest

    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 = 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 :)
     
    , Nov 6, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    868
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    530
  4. Ivan Vecerina
    Replies:
    0
    Views:
    486
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,620
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page