xlib usage example

This is a discussion on xlib usage example within the Xwindows forums, part of the Tools category; 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 ...

Go Back   Unix Linux Forum > Technologies & Tools > Tools > Xwindows

FixUnix.com - Unix Linux Forums

Unix Content Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-30-2008, 12:49 PM
Default 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.
Reply With Quote
  #2  
Old 09-30-2008, 05:32 PM
Default 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
Reply With Quote
  #3  
Old 09-30-2008, 06:21 PM
Default 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
Reply With Quote
  #4  
Old 10-01-2008, 02:20 AM
Default 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));



Reply With Quote
  #5  
Old 10-01-2008, 10:15 AM
Default 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 With Quote
Reply

Thread Tools


All times are GMT -5. The time now is 01:00 AM.

In an effort to better serve ads to our visitors, cookies are used on Fixunix.com. For more information, check out our Privacy Policy.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Ad Management by RedTyger