xlib usage example - Xwindows

This is a discussion on xlib usage example - Xwindows ; Dear All! I have the following code, which produces segmentation fault. Any reasons why? #include #include #include #include #include #include #include struct Server_class{ Display *dpy; Window win; Window root; Pixmap pm; GC gc; } *app; void setup_connection(){ app->dpy = XOpenDisplay(NULL); ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: xlib usage example

  1. xlib usage example

    Dear All!

    I have the following code, which produces segmentation fault.
    Any reasons why?

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    struct Server_class{
    Display *dpy;
    Window win;
    Window root;
    Pixmap pm;
    GC gc;
    } *app;

    void setup_connection(){
    app->dpy = XOpenDisplay(NULL);
    printf("Hello\n");
    }

    int main(){
    setup_connection();
    return 0;
    }

    Thanks.

  2. Re: xlib usage example

    On Sep 30, 9:49*am, eotcl wrote:
    > Dear All!
    >
    > I have the following code, which produces segmentation fault.
    > Any reasons why?
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > struct Server_class{
    > Display *dpy;
    > * Window win;
    > * Window root;
    > * Pixmap pm;
    > * GC gc;
    >
    > } *app;
    >
    > void setup_connection(){
    > * app->dpy = XOpenDisplay(NULL);
    > * printf("Hello\n");
    >
    > }
    >
    > int main(){
    > * setup_connection();
    > * return 0;
    >
    > }
    >


    You have declared app as a pointer, but haven't pointed it to
    anything.
    Thus the dereference app->dpy is dereferencing an uninitialized
    pointer.
    --
    Fred Kleinschmidt

  3. Re: xlib usage example

    Fred writes:

    > On Sep 30, 9:49*am, eotcl wrote:
    >> Dear All!
    >>
    >> I have the following code, which produces segmentation fault.
    >> Any reasons why?
    >>
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >> struct Server_class{
    >> Display *dpy;
    >> * Window win;
    >> * Window root;
    >> * Pixmap pm;
    >> * GC gc;
    >>
    >> } *app;
    >>
    >> void setup_connection(){
    >> * app->dpy = XOpenDisplay(NULL);
    >> * printf("Hello\n");
    >>
    >> }
    >>
    >> int main(){
    >> * setup_connection();
    >> * return 0;
    >>
    >> }
    >>

    >
    > You have declared app as a pointer, but haven't pointed it to
    > anything.
    > Thus the dereference app->dpy is dereferencing an uninitialized
    > pointer.


    That's not entirely accurate. Since his pointer has file scope, it
    will be a null pointer when he dereferences it. Dereferencing a null
    pointer is obviously a bad thing to do.

    --
    Måns Rullgård
    mans@mansr.com

  4. Re: xlib usage example


    > You have declared app as a pointer, but haven't pointed it to
    > anything.
    > Thus the dereference app->dpy is dereferencing an uninitialized
    > pointer.

    Thanks, already fixed by adding

    app=(struct Server_class *) calloc(1, sizeof(struct Server_class));




  5. Re: xlib usage example

    On Sep 30, 11:20*pm, eotcl wrote:
    > > You have declared app as a pointer, but haven't pointed it to
    > > anything.
    > > Thus the dereference * app->dpy *is dereferencing an uninitialized
    > > pointer.

    >
    > Thanks, already fixed by adding
    >
    > * app=(struct Server_class *) calloc(1, sizeof(struct Server_class));


    Don't cast the result of malloc, calloc, or realloc. Thye cast will
    hide the
    compiler's warning message if you happen to omit the #include of
    stdlib.h.

    A better way:
    app = calloc( 1, sizeof (*app) );
    --
    Fred Kleinschmidt

+ Reply to Thread