Author Topic: crash after calling function twice  (Read 3552 times)

0 Members and 1 Guest are viewing this topic.

Offline klebue

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
crash after calling function twice
« on: May 24, 2010, 12:28:30 pm »
Hi there!
I'm trying to get into c programming for nspire (actually I'm trying to get into c programming at all ;) )

I can have a very simple function, say to draw a dot of 2*2 pixels.
The function works just fine and draws my dot, but when the function is called for a second time the nspire crashes.
Here is the code I use:
Code: [Select]
int main(void) {
clearScreen();

void drawdot(int posx, int posy, int colour) {
setPixel(posx, posy, colour);
setPixel(posx+1, posy, colour);
setPixel(posx, posy+1, colour);
setPixel(posx+1, posy+1, colour);
}

drawdot(10,10,0); //works fine
drawdot(10,15,0); //crashes when I add this line

while (!isKeyPressed(KEY_NSPIRE_ESC)) {}
return 0;
}

There is also another problem: When I press ESC the calculator will stop responding instead of redrawing the normal screen.
What am I missing to make him do so? I can't figure out what makes it work in other programs.

Thanks in advance!

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: crash after calling function twice
« Reply #1 on: May 24, 2010, 05:40:44 pm »
Don't define your functions inside main().
In fact, place main() in its own file and all your functions in other header files. I've had trouble with functions defined in the same file as main() crashing.

Offline klebue

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Re: crash after calling function twice
« Reply #2 on: May 26, 2010, 04:53:13 pm »
Thanks for the advice, the function thing is working now.
However, I still couldn't figure out how to quit a program and make the calculator continue working the old-fashioned way.

/edit:
problem solved, the last lines should be
Code: [Select]
while (!isKeyPressed(KEY_NSPIRE_ESC)) {
rand();
}
return 0;
The nspire doesn't like doing nothing ;)
« Last Edit: May 26, 2010, 06:03:53 pm by klebue »

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: crash after calling function twice
« Reply #3 on: May 27, 2010, 12:56:03 am »
while (!isKeyPressed(KEY_NSPIRE_ESC)); also works.