When using detectCollision to get the ball to bounce off the paddle, I just need to check if detectCollison returns the object "paddle" and if so, reverse the movement. But for the brick, I have to first check if object == NULL....Why? If I just look for the ball to collide with a brick, via if(strcmp(getType(object), "GRect") ==0) , then making sure it's not the paddle rectangle, it gives me the "core dumped". Here's that section of code - the one that works.



If you're following the distribution code, you're likely to have a variable named paddle that stores an instance of your paddle. If the ball collides with the paddle, detectCollision returns an instance of the same paddle (the one stored in paddle). You may think of the value that detect collision returns as an alias to the value stored in paddle. At this point, you can easily check whether the returned GObject is equal to paddle.

Although you could check whether the returned GObject is equal to NULL before comparing it with paddle, this would be an unnecessary step since if the returned value is already NULL, it won't be equal to paddle.

If you're like me, you never stored the GRects that represent the bricks. If a ball collides with a brick, detectCollision returns an instance of that brick (a GRect) that is of course not the paddle (which is another GRect). But it may also return NULL in which case I'll be risking a segmentation fault if I don't use that properly (e.g., passing this value to getType) and that's why I need to check whether the returned value is equal to NULL to make sure it's a brick before involving it in further operations.

If I had an instance of the brick, like I had one of the paddle, I could have compared the returned value with it directly and I wouldn't need to check whether the returned value is NULL.

Although I could have store the bricks, say in an array of GRects, that would unnecessarily complicate things and I would have to compare the returned value with each of the bricks in the array until I find the matching instance of the brick because obviously I don't know directly which brick was returned.

| improve this answer | |
  • Thanks, but just to clarify: Is the problem really just with passing NULL into getType(object)? So you can't have "getType(NULL)"? Is that when we get a segmentation fault? – Susan Mar 25 '15 at 14:59
  • @Susan yes. And that's because getType as its name suggests, tries to get the type of the passed value, which it assumes a GObject. It basically tries to read some values from the passed value. Since the passed value is NULL in this case and you can't read from NULL, you get a segmentation fault immediately. – Kareem Mar 25 '15 at 15:27

You must log in to answer this question.

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