-1

So my now completed program works as intended apart from one crash that happens when i keypress 'p' which should only switch an if statement to the else part, however it crashes with the "illegal instruction (core dumped)" message.

#include <stdio.h>
// include the UFCFGL301's standard library
#include <ufcfgl-30-1.h>
// uncomment if you want to use the graphics library
#include <graphics.h>


const uint32 window_Width = 600;
const uint32 window_Height = 400;

using namespace uwe;

struct Rect{
  int x,y,width,height,r,g,b;
};

struct Circle{
  int x,y,radius,r,g,b;
};

union CircleorRect{
  Rect rect;
Circle circle;
};

Rect createRect() {
  int x = rand() % window_Width;
  int y = rand() % window_Height;
  int width = rand() % 200;
  int height = rand() % 200;
  int r = rand()%256;
  int g = rand()%256;
  int b = rand()%256;
  return Rect{x, y, width, height, r, g, b};
};

Circle createCirc() {
  int x = rand() % window_Width;
  int y = rand() % window_Height;
  int radius = rand() % 200;
  int r = rand()%256;
  int g = rand()%256;
  int b = rand()%256;
  return Circle{x, y, radius, r, g, b};
};

Rect createRect();
Circle createCirc();

int main(void) {
// graphics window
initialiseGraphics(window_Width,window_Height);
// variables
int count = 0,pressedcount;
Circle circle[1000];
Rect rects[1000];
bool stopCircs = false;
// creating distinct shapes then mapping them.
loop(
  [&](){

    rects[count] = createRect();
    //if circles = create new circle and paint it
    if (stopCircs == false) {
      circle[count] = createCirc();
      for (size_t i = 0; i < count; i++) {
        setColour(circle[i].r,circle[i].g,circle[i].b);
        drawFilledCircle(circle[i].x,circle[i].y,circle[i].radius);
      }
    }
    else {
      for (size_t i = 0; i < pressedcount; i++) {
        setColour(circle[i].r,circle[i].g,circle[i].b);
        drawFilledCircle(circle[i].x,circle[i].y,circle[i].radius);
      }}

    for (size_t i = 0; i < count; i++) {
      setColour(rects[i].r,rects[i].g,rects[i].b);
      drawFilledRect(rects[i].x,rects[i].y,rects[i].width,rects[i].height);
    }

      count++;
      if (count >= 1000) {
        count = 0;
      }
  },
  [&](KeyPress keyPress){
    if (getTextCharacter(keyPress) == 'q') {
      return true;
    }
    else if (getTextCharacter(keyPress) == 'p') {
      stopCircs = true;
      pressedcount = count;
    }
    else {
      return false;
    }
  });
return 0;
}

The 'p' press should only switch from generating new circles to just loading the older ones, no idea why it causes a crash. This is the whole thing so if anyone wants to try and run it or tell me how to get a better debugger than trying and failing any help would be appreciated.

  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 07 '18 at 12:55
  • 3
    On an unrelated not, you should probably learn more about classes and *inheritance*. I recommend that you get a good book or two from [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Nov 07 '18 at 12:56
  • 2
    As for your crash, I really recommend that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Especially how to catch crashes as they happen with a debugger. Then you can locate where in your code it occurs, and it might help you understand what's happening and possibly even why and how to fix it. – Some programmer dude Nov 07 '18 at 12:57
  • I would suggest this. Comment the body inside your main function and start uncommenting line by line and execute your code after each uncomment. Also add printf's to make sure you're passing the right values to the functions you're calling. This will make debugging much easier and you will understand your code better. – theBuzzyCoder Nov 07 '18 at 13:00

1 Answers1

2

In this lambda not all control-paths have a return statement (compiling with a higher error/warning level might diagnose this kind of error).

[&](KeyPress keyPress){
    if (getTextCharacter(keyPress) == 'q') {
      return true;
    }
    else if (getTextCharacter(keyPress) == 'p') {
      stopCircs = true;
      pressedcount = count;
      //here maybe missing return
    }
    else {
      return false;
    }
// or here maybe missing return
}
engf-010
  • 3,980
  • 1
  • 14
  • 25