Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
what causes this program to seg fault?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
nvangogh  
View profile  
 More options Aug 8 2012, 2:43 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: nvangogh <nvang...@invalid.net>
Date: Wed, 08 Aug 2012 19:43:17 +0100
Local: Wed, Aug 8 2012 2:43 pm
Subject: what causes this program to seg fault?
#include <X11/Xlib.h>
#include <unistd.h>
#define NIL (0)

int main()
{
       // Open the display

       Display *dpy = XOpenDisplay(NIL);
       assert(dpy);

       // Get some colors

       int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
       int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

       // Create the window

       Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
                                     200, 100, 0, blackColor, blackColor);

       // We want to get MapNotify events

       XSelectInput(dpy, w, StructureNotifyMask);

       // "Map" the window (that is, make it appear on the screen)

       XMapWindow(dpy, w);

       // Create a "Graphics Context"

       GC gc = XCreateGC(dpy, w, 0, NIL);

       // Tell the GC we draw using the white color

       XSetForeground(dpy, gc, whiteColor);

       // Wait for the MapNotify event

       for(;;) {
            XEvent e;
            XNextEvent(dpy, &e);
            if (e.type == MapNotify)
                  break;
       }

       // Draw the line

       XDrawLine(dpy, w, gc, 10, 60, 180, 20);

       // Send the "DrawLine" request to the server

       XFlush(dpy);

       // Wait for 10 seconds

       sleep(10);

}

It compiles without error, but if i run it from the console it will
generate a segmentation fault error.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Aug 8 2012, 3:03 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: Keith Thompson <ks...@mib.org>
Date: Wed, 08 Aug 2012 12:03:10 -0700
Local: Wed, Aug 8 2012 3:03 pm
Subject: Re: what causes this program to seg fault?

Once I add

    #include <assert.h>

it compiles and runs without error for me (tested under Cygwin, Ubuntu,
and Solaris).

assert() is defined as a macro.  My best guess is that you have a
function or other symbol with the same name defined somewhere in your
library, and your compiler is permitting the implicit declaration and
calling that function, which is incompatible with the assert() macro.

Try adding "#include <assert.h>" and see if that fixes it.

Incidentally, what is the purpose of

    #define NIL (0)

?  Why not just use the predefined NULL macro?  And in any case, the
parentheses in the definition are unnecessary (but harmless).

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nvangogh  
View profile  
 More options Aug 8 2012, 5:59 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: nvangogh <nvang...@invalid.net>
Date: Wed, 08 Aug 2012 22:59:42 +0100
Local: Wed, Aug 8 2012 5:59 pm
Subject: Re: what causes this program to seg fault?
On 08/08/12 20:03, Keith Thompson wrote:

Hi Keith,
I got it to run under debian but i am having problems getting it to work
on the console / terminal in freebsd. I have been trying to find out if
a window manager is necessary to run this but am not able to find clear
documentation on this point. I am trying to get this to work on a system
that does have x windows configured - but I am not invoking it from
within x windows.
Regards

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Naar  
View profile  
 More options Aug 8 2012, 6:21 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: Ike Naar <i...@faeroes.freeshell.org>
Date: Wed, 8 Aug 2012 22:21:50 +0000 (UTC)
Local: Wed, Aug 8 2012 6:21 pm
Subject: Re: what causes this program to seg fault?
On 2012-08-08, nvangogh <nvang...@invalid.net> wrote:

> I am trying to get this to work on a system
> that does have x windows configured - but I am not invoking it from
> within x windows.

If the DISPLAY environment variable is not pointing to a
working X display,

  Display *dpy = XOpenDisplay(NULL);

will not succeed, and dpy will be set to NULL.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Aug 8 2012, 6:33 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: Keith Thompson <ks...@mib.org>
Date: Wed, 08 Aug 2012 15:33:14 -0700
Local: Wed, Aug 8 2012 6:33 pm
Subject: Re: what causes this program to seg fault?

Hmm, that's odd.  I definitely wrote "#include <assert.h>" both times,
with a space between "#include" and "<", but the quoted material in your
followup shows it as "#include<assert.h>" without the space.  Did your
newsreader do that?

Anyway ...

> I got it to run under debian but i am having problems getting it to
> work on the console / terminal in freebsd. I have been trying to find
> out if a window manager is necessary to run this but am not able to
> find clear documentation on this point. I am trying to get this to
> work on a system that does have x windows configured - but I am not
> invoking it from within x windows.

Did you try adding the "#include <assert.h>" as I suggested?  Did it
make a difference?

A window manager shouldn't be necessary, as long as you have a usable X
server running.  But it would be quite unusual to be running an X server
with no window manager.

And if you don't, the assert() should fail unless you've defined NDEBUG.

But really, assert() should be used to catch programming errors, not
run-time errors.  It's a way of saying "This should never happen, but if
it does, tell me about it and give up on doing anything else."

For XOpenDisplay(NULL) to work, you need to have the environment
variable $DISPLAY set to something valid, such as ":0.0".  This *should*
be set for you automatically.  Try running a simple known-to-work X11
program such as xlogo; this will tell you whether it's a problem with
the X server or a problem in your program.

First see if you can get the assert() to work without causing a
segmentation fault.  Then I suggest replacing the assert() with
something like:

    if (dpy == NULL) {
        fprintf(stderr, "XOpenDisplay failed\n");
        exit(EXIT_FAILURE);
    }

And what *exactly* does "problems getting it to work" mean?  Is this
still the segmentation fault problem?

If your problem is with X11, you might try posting in one of the
comp.windows.x newsgroups (assuming they're still active, I haven't
visited lately).

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Aug 8 2012, 6:34 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: Keith Thompson <ks...@mib.org>
Date: Wed, 08 Aug 2012 15:34:06 -0700
Local: Wed, Aug 8 2012 6:34 pm
Subject: Re: what causes this program to seg fault?

Ike Naar <i...@faeroes.freeshell.org> writes:
> On 2012-08-08, nvangogh <nvang...@invalid.net> wrote:
>> I am trying to get this to work on a system
>> that does have x windows configured - but I am not invoking it from
>> within x windows.

> If the DISPLAY environment variable is not pointing to a
> working X display,

>   Display *dpy = XOpenDisplay(NULL);

> will not succeed, and dpy will be set to NULL.

Which he checks for, but I suspect the check isn't being done correctly.

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vincenzo Mercuri  
View profile  
 More options Aug 9 2012, 3:58 am
Newsgroups: alt.comp.lang.learn.c-c++
From: Vincenzo Mercuri <vincenzo.merc...@yahoo.it>
Date: Thu, 09 Aug 2012 09:58:42 +0200
Local: Thurs, Aug 9 2012 3:58 am
Subject: Re: what causes this program to seg fault?
Il 08/08/2012 21:03, Keith Thompson ha scritto:
[..]

> Try adding "#include <assert.h>" and see if that fixes it.

> Incidentally, what is the purpose of

>      #define NIL (0)

> ?  Why not just use the predefined NULL macro?  And in any case, the
> parentheses in the definition are unnecessary (but harmless).

Hi Keith,
I think he's following this tutorial:
http://tronche.com/gui/x/xlib-tutorial/2nd-program-anatomy.html
which does actually add "#include <assert.h>". I got it compile
and run on my Xubuntu 12.04 64-bit, with no problems.

--
Vincenzo Mercuri


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Freund  
View profile  
 More options Aug 28 2012, 3:15 am
Newsgroups: alt.comp.lang.learn.c-c++
From: "Christian Freund" <fre...@wrz.de>
Date: Tue, 28 Aug 2012 09:15:11 +0200
Local: Tues, Aug 28 2012 3:15 am
Subject: Re: what causes this program to seg fault?
"Keith Thompson"  schrieb im Newsbeitrag
news:lnvcgtui75.fsf@nuthaus.mib.org...

>   Display *dpy = XOpenDisplay(NULL);

> will not succeed, and dpy will be set to NULL.

Which he checks for, but I suspect the check isn't being done correctly.

__

Please "nvangogh" instead of that assert() try out something like:

"if(!dpy) return 0xffff;"

Or finally use a debugger, or print out the return-codes of everything on
stderr.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Aug 28 2012, 4:51 am
Newsgroups: alt.comp.lang.learn.c-c++
From: Keith Thompson <ks...@mib.org>
Date: Tue, 28 Aug 2012 01:51:48 -0700
Local: Tues, Aug 28 2012 4:51 am
Subject: Re: what causes this program to seg fault?

Christian, quoted text should be marked by prepending "> " to each line.
Your newsreader *should* do this for you.

Returning 0xffff from main() is not a good idea.  If you want to do that
kind of error check, you can do `return EXIT_FAILURE;` or
`exit(EXIT_FAILURE);`, both of which require `#include <stdlib.h>`.

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Glassborow  
View profile  
 More options Aug 28 2012, 5:21 am
Newsgroups: alt.comp.lang.learn.c-c++
From: Francis Glassborow <francis.glassbo...@btinternet.com>
Date: Tue, 28 Aug 2012 10:21:44 +0100
Local: Tues, Aug 28 2012 5:21 am
Subject: Re: what causes this program to seg fault?
On 28/08/2012 09:51, Keith Thompson wrote:

I am sure it would if he had not replied below the '__' signature separator.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Aug 28 2012, 1:02 pm
Newsgroups: alt.comp.lang.learn.c-c++
From: Keith Thompson <ks...@mib.org>
Date: Tue, 28 Aug 2012 10:02:49 -0700
Local: Tues, Aug 28 2012 1:02 pm
Subject: Re: what causes this program to seg fault?

"__" isn't a signature separator; "-- " is (the trailing space is part
of it).

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »