Jump to content
Sign in to follow this  
Stringer

Stack overflow/underflow

Recommended Posts

I have following code:

main()
{
char temppi;

// initialize peripherals and the application
on_init();

Clock->write_hours(23);
Clock->write_minutes(59);
Clock->write_seconds(50);

Clock->write_year(4);
Clock->write_month(2);
Clock->write_day(7);


Display->gotoxy(0,0);  Display->kirjoitarivi("TIME ALARMS INFO\n");
Display->gotoxy(0,1);  Display->kirjoitarivi("CLOCK: \n"); 
Display->gotoxy(0,2);  Display->kirjoitarivi("DATE: \n");
Display->gotoxy(0,3);  Display->kirjoitarivi("ALARMING: \n");


while(1)
{
 asm nop
 Display->gotoxy(7,1);
 temppi = Clock->read_hours();
 Display->kirjoita10x( temppi, 2, 2 ); 
 Display->kirjoita(':');
 temppi = Clock->read_minutes();
 Display->kirjoita10x( temppi, 2, 2 ); 
 Display->kirjoita(':');
 temppi = Clock->read_seconds();
 Display->kirjoita10x( temppi, 2, 2 ); 

 
 Display->gotoxy(6,2);
 temppi = Clock->read_day();
 Display->kirjoita10x( temppi, 2, 2 ); 
 Display->kirjoita('.');
 temppi = Clock->read_month();
 Display->kirjoita10x( temppi, 2, 2 ); 
 //Display->kirjoitarivi(".20\n");
 temppi = Clock->read_year();
 Display->kirjoita10x( temppi, 2, 2 ); 
}
}

 

Before while loop (notice this comment!!) Output window in simulation starts to scream

stack overflow

stack underflow

 

Now, if I comment out what is in inside the while loop, it doesn't scream anything about stack overflow or underflow.

 

Care you explain that?

Share this post


Link to post
Share on other sites
Guest Dave

Stringer,

 

The stack overflow and underflow messages were added into the simulator on the SourceBoost 5.3 release.  ::):

 

Stack overflow  means that the call depth is greater than the stack can hold, when this happens the stack pointer goes off the top of the stack and wraps to the bottom.

Now, when the code hits the next return, the stack point will go from the bottom of the stack to the top, flagging a stack underflow.

 

Overflow and underflow normally indicate problems in the program.

 

Check how many functions deep you calling goes.

 

Step the code to see when exactly the problem is flagged.

Display the stack bar to keep an eye on how deep the stack is filled.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Yes, but I don't understand why it starts happening in place where it doesn't happen without code later. It sounds like the compiler would do something odd.

 

Anyway, by removing "const char *" from my function parameter helpped. Negative side from that is now I have to reserve RAM for each message I want to display :)

Share this post


Link to post
Share on other sites
Guest Pavel
Anyway, by removing "const char *" from my function parameter helpped. Negative side from that is now I have to reserve RAM for each message I want to display :)

The code that deals with const char* uses one extra call. If the call stack already has 8 calls that using const char* (or one of the */% operations or new/delete etc.) will produce stack overflow.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Anyway, by removing "const char *" from my function parameter helpped. Negative side from that is now I have to reserve RAM for each message I want to display :)

The code that deals with const char* uses one extra call. If the call stack already has 8 calls that using const char* (or one of the */% operations or new/delete etc.) will produce stack overflow.

 

Regards,

Pavel

I still don't understand why removing non-relevant code from place that has no intentional relation to the place where stack problems come, makes the problem go away? Once it did even disappear just by removing C-based comments.

 

Anyway, the stack (from high-level viewpoint) is somewhat like this:

 

1. call printf-function (from main)

2. it calls putchar-function

3. it calls low-level write

4. it calls LCD_WaitForNotBusy-function and later LCD_enable_data-function

 

Putchar-function has one %-operation incase character is linefeed (not happening in this project). There are no multiplys, divides or even new/delete in this case. All those functions are part of a class which was created using new-operator. Does that make difference?

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...