I have written the code for pset4 but I have an odd problem and I can't find the solution for this malfunctioning issue I have in the ball function.

The code I have is the following:

GOval initBall(GWindow window)
    int ball_diameter = 24;
    GOval ball = newGOval((WIDTH - ball_diameter)/ 2, (HEIGHT - ball_diameter) / 2, ball_diameter, ball_diameter);
    setColor(ball, "RED");
    setFilled(ball, true);
    add(window, ball);
    return ball;

I don't know why this happens: If I try to define ball_diameter as the logical value of RADIUS * 2 (which is defined as a constant at the top of the program) I get a segmentation fault which I don't understand.

If I change the ball_diameter value from 24 to something lower (i.e. 20) I get the same segmentation fault error. That makes me think that it is value-related.

I tried to debug the program and when I use the breakpoint at initBall() line and run next, the program tells me that ball_diameter has a value of 35 (???) which was not defined never before.

The program works as it is supposed to do besides this damn glitch that I don't seem to understand.

Thank you very much and I hope someone can help me!

  • 1
    Your code is pretty similar to mine (except I did instanciate the new ball using the RADIUS constant) and it should work. I'm pretty sure the problem does not lie here. Please follow your code execution (using gdb or even printf statements) and try to determine exactly where your code "segfaults". And please post (perhaps a refuced version of) your while loop from where you actually call initBall()
    – abelinux
    Dec 14, 2014 at 16:01

2 Answers 2


I don't understand what you mean by "If I change the ball_diameter value from 24 to something lower (i.e. 20) I get the same segmentation fault error. That makes me think that it is value-related."

You changed the value and you get the same result. That probably means that it is NOT value related.

After getting my program to mostly work, I believe your problem is that you have defined ball_diameter inside of "GOval initBall(GWindow window)" That makes it local. If you move the ball_diameter definition up above main where RADIUS is defined you might find that solves the problem.

  • 1
    Not really a problem in itself, since he's instanciating the ball also inside the initBall() function. But it's true that he's probably inspecting (with gdb) the value of ball_diameter outside this function (back in main()) and that's why he's getting weird values.
    – abelinux
    Dec 27, 2014 at 21:18
  • 1
    RADIUS is a constant, defined to be a preprocessor replacement. So it doesn't "have" a scope. The preprocessor simply replaces the text RADIUS with the value defined for this "name" and only then it compiles. Therefore, there should be no difference on where (in terms of scope) you define ball_diameter (as long as you pass it to initBall() within the same scope, of course)
    – abelinux
    Dec 27, 2014 at 21:20

You are adding the ball to the window and returning it also, presumably you use it somehow. what do you do with it afterwards?

Your segmentation fault could be happening there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .