why this program is crashing

Discussion in 'C Programming' started by James, Apr 17, 2005.

  1. James

    James Guest

    hello,
    The program given below is giving segmentation fault. By debugging I
    inferred that the problem is in the statement t1.f(), but I am not sure
    that what is the problem.

    #include <stdio.h>

    struct type {
    int i;
    void (*f)(void);
    };

    void funct(void)
    {
    printf("this is funct\n");
    return;
    }

    int construct(struct type t1,int i,void (*f)(void))
    {
    t1.i=i;
    t1.f=f;
    return 0;
    }

    int main(void)
    {
    struct type t1;
    printf("This is main\n");
    construct(t1,2,&funct);
    t1.f(); // Most probably the problem is in this line.
    return 0;
    }

    Can anybody please tell me that what is the real problem?

    Thanks
    James, Apr 17, 2005
    #1
    1. Advertising

  2. This works:

    [...]

    int construct(struct type* t1,int i,void (*f)(void))
    {
    t1->i=i;
    t1->f=f;
    return 0;
    }

    int main(void)
    {
    struct type t1;
    printf("This is main\n");
    construct(&t1,2,&funct);
    t1.f(); // Most probably the problem is in this line.
    return 0;
    }

    You have to call "construct" with the address of the struct.

    Regards,
    Daniel

    James wrote:
    > hello,
    > The program given below is giving segmentation fault. By debugging I
    > inferred that the problem is in the statement t1.f(), but I am not sure
    > that what is the problem.
    >
    > #include <stdio.h>
    >
    > struct type {
    > int i;
    > void (*f)(void);
    > };
    >
    > void funct(void)
    > {
    > printf("this is funct\n");
    > return;
    > }
    >
    > int construct(struct type t1,int i,void (*f)(void))
    > {
    > t1.i=i;
    > t1.f=f;
    > return 0;
    > }
    >
    > int main(void)
    > {
    > struct type t1;
    > printf("This is main\n");
    > construct(t1,2,&funct);
    > t1.f(); // Most probably the problem is in this line.
    > return 0;
    > }
    >
    > Can anybody please tell me that what is the real problem?
    >
    > Thanks
    >
    Daniel Etzold, Apr 17, 2005
    #2
    1. Advertising

  3. James

    Artie Gold Guest

    James wrote:
    > hello,
    > The program given below is giving segmentation fault. By debugging I
    > inferred that the problem is in the statement t1.f(), but I am not sure
    > that what is the problem.
    >
    > #include <stdio.h>
    >
    > struct type {
    > int i;
    > void (*f)(void);
    > };
    >
    > void funct(void)
    > {
    > printf("this is funct\n");
    > return;
    > }
    >
    > int construct(struct type t1,int i,void (*f)(void))

    You realize, of course, that `t1' is being passed by value? Any change
    you make to `t1' only affects the local copy.
    > {
    > t1.i=i;
    > t1.f=f;
    > return 0;
    > }


    Make that:

    int construct(struct type * t1, int i, void (*f)(void))
    /* why are you bothering to return anything here? */
    {
    t1->i = 1;
    t1->f = f;
    return 0;
    }
    >
    > int main(void)
    > {
    > struct type t1;
    > printf("This is main\n");
    > construct(t1,2,&funct);

    You should have looked at the value of t1 *here*. ;-(
    construct(&t1, 2, funct);
    > t1.f(); // Most probably the problem is in this line.
    > return 0;
    > }
    >
    > Can anybody please tell me that what is the real problem?
    >

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Apr 17, 2005
    #3
  4. James wrote:
    > hello,
    > The program given below is giving segmentation fault. By debugging I
    > inferred that the problem is in the statement t1.f(), but I am not sure
    > that what is the problem.


    [OP's code replaced]

    #include <stdio.h>

    struct type
    {
    int i;
    void (*f) (void);
    };

    void funct(void)
    {
    printf("this is funct\n");
    return;
    }

    int OPs_construct(struct type t1, int i, void (*f) (void))
    {
    t1.i = i;
    t1.f = f;
    return 0;
    }


    int corrected_construct(struct type *t1, int i, void (*f) (void))
    {
    t1->i = i;
    t1->f = f;
    return 0;
    }

    int main(void)
    {
    struct type t1 = { 0, 0 };
    printf("t1.i starts as %d.\n"
    "It should be 2 after \"construct\"\n", t1.i);
    OPs_construct(t1, 2, funct);
    printf("After calling OP's construct function, \n"
    " t1.i = %d\n", t1.i);
    printf("Since the OP's construct function doesn't work,\n"
    " we won't bother with 't1.f();'\n\n");

    corrected_construct(&t1, 2, funct);
    printf("After calling the corrected construct function, \n"
    " t1.i = %d\n", t1.i);
    printf("Calling t1.f:\n");
    t1.f();
    return 0;
    }



    [output]
    t1.i starts as 0.
    It should be 2 after "construct"
    After calling OP's construct function,
    t1.i = 0
    Since the OP's construct function doesn't work,
    we won't bother with 't1.f();'

    After calling the corrected construct function,
    t1.i = 2
    Calling t1.f:
    this is funct
    Martin Ambuhl, Apr 17, 2005
    #4
  5. On Sun, 17 Apr 2005 07:20:18 -0700, James wrote:

    > hello,
    > The program given below is giving segmentation fault. By debugging I
    > inferred that the problem is in the statement t1.f(), but I am not sure
    > that what is the problem.
    >
    > #include <stdio.h>
    >
    > struct type {
    > int i;
    > void (*f)(void);
    > };
    >
    > void funct(void)
    > {
    > printf("this is funct\n");
    > return;
    > }
    >
    > int construct(struct type t1,int i,void (*f)(void))
    > {
    > t1.i=i;
    > t1.f=f;
    > return 0;
    > }
    >
    > int main(void)
    > {
    > struct type t1;
    > printf("This is main\n");
    > construct(t1,2,&funct);
    > t1.f(); // Most probably the problem is in this line.
    > return 0;
    > }
    >
    > Can anybody please tell me that what is the real problem?
    >
    > Thanks


    When you call your "construct" function you are passing a copy of t1 on
    which the function operates, the original struct is unchanged, this is
    because arguments are passed by-value in C as opposed to by-reference.
    When your program calls t1.f(), it is trying to execute whatever happens
    to be at the (uninitialized) address stored in t1.f causing your
    segmentation fault. You have a couple of options, you can return a struct
    from your "construct" function assigning the new value to your old struct,
    or you can pass your function a pointer to the struct and have the
    function operate on the original struct.

    Example 1 (Return struct):

    /* Change return type */
    struct type construct(struct type t1, int i, void (*f)(void))
    {
    t1.i=i;
    t1.f=f;
    return t1; /* Return the modified copy */
    }

    int main(void)
    {
    struct type t1;
    printf("This is main\n");
    t1 = construct(t1, 2, &funct); /* Store the return value in t1 */
    t1.f();
    return 0;
    }

    Example 2 (Pass pointer to struct):

    /* Change first parameter to be pointer to struct */
    int construct(struct type * t1, int i, void (*f)(void))
    {
    t1->i=i; /* Operate on original struct */
    t1->f=f;
    return 0;
    }

    int main(void)
    {
    struct type t1;
    printf("This is main\n");
    construct(&t1, 2, &funct); /* Pass address of t1 */
    t1.f();
    return 0;
    }


    Rob Gamble
    Robert Gamble, Apr 17, 2005
    #5
  6. On Sun, 17 Apr 2005 16:34:54 +0200, Daniel Etzold <>
    wrote:

    >This works:
    >
    >[...]
    >
    >int construct(struct type* t1,int i,void (*f)(void))
    >{
    > t1->i=i;
    > t1->f=f;
    > return 0;
    >}
    >
    >int main(void)
    >{
    > struct type t1;
    > printf("This is main\n");
    > construct(&t1,2,&funct);
    > t1.f(); // Most probably the problem is in this line.
    > return 0;
    >}
    >
    >You have to call "construct" with the address of the struct.


    Or return the updated copy of the struct back to the calling function.

    >
    >Regards,
    >Daniel
    >
    >James wrote:
    >> hello,
    >> The program given below is giving segmentation fault. By debugging I
    >> inferred that the problem is in the statement t1.f(), but I am not sure
    >> that what is the problem.
    >>
    >> #include <stdio.h>
    >>
    >> struct type {
    >> int i;
    >> void (*f)(void);
    >> };
    >>
    >> void funct(void)
    >> {
    >> printf("this is funct\n");
    >> return;
    >> }
    >>
    >> int construct(struct type t1,int i,void (*f)(void))
    >> {
    >> t1.i=i;
    >> t1.f=f;
    >> return 0;
    >> }
    >>
    >> int main(void)
    >> {
    >> struct type t1;
    >> printf("This is main\n");
    >> construct(t1,2,&funct);
    >> t1.f(); // Most probably the problem is in this line.
    >> return 0;
    >> }
    >>
    >> Can anybody please tell me that what is the real problem?
    >>
    >> Thanks
    >>




    <<Remove the del for email>>
    Barry Schwarz, Apr 18, 2005
    #6
  7. On 17 Apr 2005 07:20:18 -0700, "James" <> wrote:

    >hello,
    >The program given below is giving segmentation fault. By debugging I
    >inferred that the problem is in the statement t1.f(), but I am not sure
    > that what is the problem.
    >
    >#include <stdio.h>
    >
    >struct type {
    > int i;
    > void (*f)(void);
    > };
    >
    >void funct(void)
    >{
    > printf("this is funct\n");
    > return;
    >}
    >
    >int construct(struct type t1,int i,void (*f)(void))


    struct type construct(...

    >{
    > t1.i=i;
    > t1.f=f;
    > return 0;


    return t1;

    >}
    >
    >int main(void)
    >{
    > struct type t1;
    > printf("This is main\n");
    > construct(t1,2,&funct);


    t1 = construct(...

    > t1.f(); // Most probably the problem is in this line.
    > return 0;
    >}
    >
    >Can anybody please tell me that what is the real problem?
    >
    >Thanks




    <<Remove the del for email>>
    Barry Schwarz, Apr 18, 2005
    #7
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  3. why this program is not crashing

    , Feb 11, 2005, in forum: C Programming
    Replies:
    20
    Views:
    667
    Dave Thompson
    Feb 21, 2005
  4. code break

    Why this program is crashing ???

    code break, Mar 3, 2006, in forum: C Programming
    Replies:
    8
    Views:
    336
    Keith Thompson
    Mar 3, 2006
  5. Pallav singh

    why this program is Crashing

    Pallav singh, Jul 14, 2009, in forum: C++
    Replies:
    5
    Views:
    361
    James Kanze
    Jul 16, 2009
Loading...

Share This Page