Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Stack Underflow When the Number of an Array is Increased

Status
Not open for further replies.

apsua

Member level 3
Joined
Feb 13, 2015
Messages
62
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
538
Hello,

I have an array defined as below:

Code:
int messagePriorityLevels[] = {NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								PRIORITY_LOW,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_LOW,
								PRIORITY_HIGH,
								PRIORITY_MID,
								PRIORITY_MID,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_MID,
								PRIORITY_MID,
								PRIORITY_LOW,
								PRIORITY_HIGH,
								};

When I add one more element in it, it gives stack underflow error on both Proteus and also in real simulation. New code is as below:

Code:
int messagePriorityLevels[] = {NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								NO_PRIORITY,
								PRIORITY_LOW,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_LOW,
								PRIORITY_HIGH,
								PRIORITY_MID,
								PRIORITY_MID,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_HIGH,
								PRIORITY_MID,
								PRIORITY_MID,
								PRIORITY_LOW,
								PRIORITY_HIGH,
                                                                PRIORITY_HIGH
								};

Only one more PRIORITY_HIGH element is added to array. I made debug on the code on Proteus, the reset occurs at the line shown below:

Code:
void ShiftToRecords(int8 *ptr, int8 iterNum, int1 comType)
{
	iterNum++;
	blankShiftNumber = 24 - iterNum;

	while(iterNum != 0)
	{
		shiftBit = shift_right(inputRecordsStable, 3, 0);	
		shift_right(ptr, 3, shiftBit);
		iterNum--;
	}

	while(blankShiftNumber != 0)
	{
		shift_right(ptr, 3, 0);       /* **** The reset evet happens in this line **** */
		blankShiftNumber--;
	}
	
	inputRecordsStable[0] = 0;
	inputRecordsStable[1] = 0;
	inputRecordsStable[2] = 0;

	return;
}

When the code "shift_right" processed, every time, the stack pointer is decreased, till around 0x0060.

Can anyone please give a hint about this to me?
Note: It happens in also when I place the mcu to my controller board, too.

Thanks.
 

We might need full code and information about the used compiler.

I presume the array size effect is just a symptom.

You may be using the pointer incorrectly. But it can't be seen without the definition of shift_right(). Or ptr is not correctly initialized, pointing to a non-existing memory location.

Although it's possible to perform assembly level simulation with Proteus (if you load a binarly with debug information) you better trace the problem with the original compiler and debugger tools of your microprocessor. It's always possible to see why the code execution fails, either in hardware debugging or simulation.
 

    V

    Points: 2
    Helpful Answer Positive Rating
Also what is the device? Does it have a fixed call stack or is the stack allocated in the general RAM somewhere?
How close to filling the RAM are you?
Susan
 

Hello again,
The shift_right function is compiler's function, not defined by me. The device is pic18f46k22, and the ram is 60% used.
I have overcome the problem, by defining a global pointer as pointer0, and at the beginning of the function, I put,
Code:
pointer0 = ptr;
And the problem seems to be solved.
What do you think the problem was, should I leave using local pointers?
Thanks.
 

Can you give a hint please? So I would check it. I couldn't see any other possibility, will be glad if you tell one.
 

Can you give a hint please?
Too many possibilities to make faulty code. As said the problem can be either with the pointer initialization (the pointer value submitted to ShiftToRecords() or the pointer operations inside shift_right(). Or a different fault that corrupts unrelated data. Changing the size of an arbitrary data entity changes the memory map and thus the symptoms of data corruption.

You should relevant code or start to catch the error in debugging.
 

I do not know for sure, but the bug appears to be in the shift_ functions: you need to debug...
 

Thanks for replies.

The ptr pointer is a globally defined array:
Code:
int	recordsMix[3];

And the shift_right() code generated by compiler:
Code:
07D14:  MOVFF  440,FEA
07D18:  MOVFF  43F,FE9
07D1C:  MOVLW  02
07D1E:  ADDWF  FE9,F
07D20:  BCF    FD8.0
07D22:  RRCF   FED,F
07D24:  RRCF   FED,F
07D26:  RRCF   FED,F

In my first post, in the ShiftToRecords function, you can see that there is two lines of codes;
that shift_right processes ptr pointer. The first pointer works right, but the second function seems to shift stack address, as I had seen on debug.
 

If the code was compiled in Debug mode on simulation, in general this takes extra resources from core. Being the case, you could try doing that in release mode. Another issue is when you create long arrays which can't be easily managed with the core instruction set ( e.g look up table addressing over 256 bytes ), sometimes this force compiler to turn around by using additional resources.

Considering that your above array has many int variables ( int16 ? ), a lot of them even repeated, you could try creating another array with char variables ( int8 ) that points to the message itself; in fact there are just 4 different messages there.
 

Declare this as const int

Code:
const int messagePriorityLevels[]
 

And the shift_right() code generated by compiler:

If you expect meaningful answers, please post the related source code lines.

- - - Updated - - -

Declare this as const int
Why? It will surely change the code behavior by moving the variable into flash memory and generating table reads when accessing the constant. But it's not related to the reported problem.
 

If RAM is less then it is better to store the constant array as const int. This will free some RAM and reduces the chance of stack overflow error. Without seeing the full code a solution cannot be provided.
 

Also, without seeing the compiler report for the microcontroller resources usage all hints we can give you are just guesses.
 

Full source code is big. I can supply the .STA statistics file if you want. It is in the attachment.

As for defining the messagePriorityLevels array as const int, yes, it also overcomes the problem. But I want to learn the problem source, if it is because of my code.

Thanks.
 

Attachments

  • mainsta.txt
    12.9 KB · Views: 67

The file you posted is of no use. Use syntax tags and post the full code.
 

Okay, attached as code below:

Code:
ROM used:   38390/65536 (59%)
            38390/65536 (59%) including unused fragments

            2.6 Average locations per line
            6.9 Average locations per statement

RAM used:   1086/3648 (30%) at main() level
            1117/3648 (31%) worst case

Stack used: 11 worst case (10 in main + 1 for interrupts)
Stack size: 31

Lines Stmts  %   Files
----- ----- ---  -----
   79    30   1  main.c
  936     0   0  ..\..\PICC5\PICC\Devices\18F46K80.h
 2747     0   0  F:\DropBox\TK-107\cal\PIC18F46K80_registers.h
  423   125   2  ..\..\PICC5\PICC\Drivers\string.h
   32     0   0  ..\..\PICC5\PICC\Drivers\stddef.h
   28     0   0  ..\..\PICC5\PICC\Drivers\ctype.h
 1077     0   0  DataDefinitions.h
  244     0   0  Language_TR.h
  164     0   0  Prototypes.h
   26     0   0  IncludeFunctions.h
  354   122   2  LCD4Bit.c
   79     0   0  F:\DropBox\TK-107\cal\Lcd4BitDefs.h
   12     5   0  Interrupts.h
  126    92   2  AutoD.h
   99    66   1  BSProcesses.h
  207   136   2  ButtonControl.h
  669   460   8  CProcesses.h
  159   106   2  ClearRecs.h
  130    77   1  DataIn.h
  251   168   3  DataOut.h
  450   255   5  EProcesses.h
  596   333   6  ErrorManagement.h
  122    71   1  FCounters.h
  594   349   6  GFunctions.h
 1832  1188  21  Initialize.h
  490   339   6  InputControls.h
   46    30   1  IMove.h
 1066   594  11  LCDProcesses.h
  624   427   8  PReading.h
   69    47   1  ProcessesBeforeMain.h
   95    50   1  RecordsProcess.h
  147    80   1  SFMovements.h
  397   211   4  StartUp.h
  333   225   4  TimerFunctions.h
----- -----
14703  5586 Total

Page ROM  %  RAM   Vol Diff  Functions:
---- --- --- ---   --- ----  ----------
0     48   0    1             @delay_ms1
0     18   0    0             @const3376
0     18   0    0             @const3379
0     14   0    0             @const3382
0     12   0    0             @const3385
0     12   0    0             @const3388
0     18   0    0             @const3391
0     18   0    0             @const3394
0     16   0    0             @const3397
0     14   0    0             @const3400
0     12   0    0             @const3403
0     16   0    0             @const3415
0     12   0    0             @const3418
0     12   0    0             @const3421
0     12   0    0             @const3424
0     12   0    0             @const3427
0     14   0    0             @const3430
0     18   0    0             @const3433
0     26   0    0             @const3436
0     14   0    0             @const3439
0     10   0    0             @const3442
0      8   0    0             @const3445
0     14   0    0             @const3448
0     18   0    0             @const3451
0     18   0    0             @const3454
0     12   0    0             @const3457
0     16   0    0             @const3460
0     12   0    0             @const3463
0     14   0    0             @const3466
0     18   0    0             @const3469
0     18   0    0             @const3472
0     18   0    0             @const3475
0     16   0    0             @const3478
0     16   0    0             @const3481
0     18   0    0             @const3484
0     18   0    0             @const3487
0     16   0    0             @const3490
0     18   0    0             @const3493
0     16   0    0             @const3496
0     16   0    0             @const3499
0     14   0    0             @const3502
0     14   0    0             @const3505
0     14   0    0             @const3508
0     12   0    0             @const3511
0     12   0    0             @const3514
0     14   0    0             @const3517
0     14   0    0             @const3520
0     18   0    0             @const3523
0     14   0    0             @const3526
0     18   0    0             @const3529
0     18   0    0             @const3532
0     14   0    0             @const3535
0     16   0    0             @const3538
0     14   0    0             @const3541
0     16   0    0             @const3544
0     10   0    0             @const3547
0     12   0    0             @const3550
0     16   0    0             @const3553
0     18   0    0             @const3556
0     16   0    0             @const3559
0     16   0    0             @const3562
0     12   0    0             @const3565
0     12   0    0             @const3568
0     14   0    0             @const3571
0     18   0    0             @const3574
0     18   0    0             @const3577
0     18   0    0             @const3580
0     12   0    0             @const3583
0     12   0    0             @const3586
0      8   0    0             @const3589
0      8   0    0             @const3592
0     14   0    0             @const3595
0     16   0    0             @const3598
0     14   0    0             @const3601
0     16   0    0             @const3604
0     18   0    0             @const3607
0      8   0    0             @const3610
0     10   0    0             @const3613
0      6   0    0             @const3616
0     10   0    0             @const3619
0     10   0    0             @const3622
0     16   0    0             @const3625
0     12   0    0             @const3628
0     14   0    0             @const3631
0     14   0    0             @const3634
0     14   0    0             @const3637
0     16   0    0             @const3640
0     10   0    0             @const3643
0     14   0    0             @const3646
0      6   0    0             @const3649
0      6   0    0             @const3652
0     12   0    0             @const3655
0     16   0    0             @const3658
0     12   0    0             @const3661
0     16   0    0             @const3664
0     14   0    0             @const3667
0     18   0    0             @const3670
0     14   0    0             @const3673
0     18   0    0             @const3676
0     16   0    0             @const3679
0     34   0    0             @const3682
0     64   0    0             @const3685
0     64   0    0             @const3688
0     54   0    0             @const3691
0     40   0    0             @const3694
0     36   0    0             @const3697
0     30   0    0             @const4242
0     90   0    0             @const4245
0     62   0    3   463  4.3  LCDReadByte
0     36   0    1   123  2.6  LCDSendNibble
0     86   0    4   485  7.3  LCDSendByte
0     62   0    6   228  5.2  LCDRowClear
0     86   0    2   338  5.1  LCDInit
0    108   0    5   315  5.3  LCDSendSpecialChars
0     18   0    1    43  1.8  LCDPutChar
0     96   0    3   485  8.5  LCDGotoXY
0     86   0    4   232  6.5  LCDWriteArray
0    102   0    7   284  6.3  LCDWriteConstant
0    116   0   10   396  6.8  LCDWriteScrolling
0     20   0    0    78  2.0  TimerInt
0    246   0    1  1968  6.4  ADManagement
0     52   0    0   226  4.4  OnLevel
0    238   0    0  1505  5.4  BSCircuit
0    166   0    0  1071  3.9  ButtonControl
0    376   0    2  1965  7.9  ButtonInputTest
0     66   0    0   309  4.8  CProcesses
0    100   0    0   419  4.2  IFunctions
0     60   0    1   287  4.1  WRFunctions
0     36   0    0   153  3.2  HSFunctions
0    162   0    0   434  5.6  LSFunctions
0   1494   2    1 15447  6.0  CRecords
0     82   0    2   331  5.8  CDRecords
0     80   0    2   318  5.8  CURecords
0    168   0    5   517  7.3  CRExist
0   1048   1    1  3762  8.4  ClearCFR
0     60   0    0   877  3.9  CARecs
0      6   0    0    28  1.3  DataIn
0    202   0    1  1053  8.3  DataInRecs
0    206   0    1  1156  7.8  DataInSignals
0     12   0    0    51  1.6  DataOut
0    632   1    0  5289  8.4  ProduceOutRecs
0    160   0    2   863  6.3  ProduceDisplayOuts
0     26   0    0   132  2.6  DataOutRecs
0     36   0    0   141  2.2  DataOutSignals
0     42   0    1   254  4.6  ShiftOut
0     42   0    1   254  4.6  ShiftOut2
0    834   1    5  3634  8.0  UpdateE
0    152   0    0   780  5.8  CorrectEPos
0    896   1    6  8364  6.0  WriteEValuesToE2
0    950   1    6  7950  6.0  ReadEValuesFromE2
0    148   0    4   458  5.4  PulseVals
0     34   0    5   458  5.4  @MUL1616
0   1100   1   16  3127 10.2  AddADiff
0    374   0    0  1756  5.9  ErrorDetection
0    244   0    0  1082  4.8  ErrorRecoverFunctions
0    194   0    7  1288  6.7  SaveErrorRecords
0    210   0    1   730  3.3  GetErrorCode
0    154   0    3   472  4.5  ShowErrorRecordMessage
0    130   0    3  1118  6.0  ShowErrorRecordUpRow
0    480   1    1  1872  4.9  ShowErrorRecordDownRow
0     72   0    0  1872  4.9  @goto13312
0    100   0    5   587  6.1  ReadErrorRecordFromE2
0    106   0    5   629  6.1  ReadErrorFloorFromE2
0     98   0    0   212  2.7  ShowEraseMessage
0     38   0    1   187  4.4  EraseErrorRecords
0     34   0    0   176  4.4  FloorCounters
0    328   0    1   966  6.8  ECFunction
0     84   0    0   386  6.0  MFunction
0     62   0    3    96  2.1  WriteEeprom
0     34   0    3    29  1.6  ReadEeprom
0    130   0    8   142  3.8  WriteEeprom2Bytes
0     94   0    7   138  3.8  ReadEeprom2Bytes
0     14   0    0    76  3.0  CPULedFlash
0     82   0    0   431  4.2  LProcess
0     32   0    0   136  3.0  SwitchToWR
0     56   0    0   160  3.5  RunE
0     52   0    0   160  3.5  SwitchToLS
0    132   0    0   911  4.9  StopE
0     52   0    1   169  4.0  SetLCDState
0     78   0    0   287  3.2  WaitAtEnd
0     52   0    1   270  3.7  DAssign
0    422   1    2  1719  7.4  SetS
0     24   0    0   110  2.8  TestM
0     42   0    1   188  3.6  RPTFunction
0     34   0    1   188  3.6  RTRFunction
0     70   0    6   302  6.5  CalculateDivisionResults
0     50   0    2   318  5.8  ChangeC
0    176   0    0   963  4.8  TestR
0    170   0    1   490  5.1  CheckExistance
0     36   0    1   132  3.4  StabilizeDataIn
0    904   1    4  4399 11.6  Initialize
0     48   0    0  4399 11.6  @goto14017
0     52   0    0             @goto14029
0   1472   2    2  7036 10.5  SettingsMenuFunction
0     52   0    0  7036 10.5  @goto14077
0     42   0    2   208  5.4  ClosedSettingsCheck
0    162   0    3   855  8.0  ErrorMenuFunction
0     52   0    0   855  8.0  @goto14173
0     84   0    0   328  5.4  EraseMenuFunction
0    966   1    8  5702 11.1  DisplayMenuFunction
0     52   0    0  5702 11.1  @goto14237
0    150   0    4   604  8.0  DisplayMenuExitProcess
0     52   0    0   604  8.0  @goto14387
0   1802   2    5 16904 12.0  ChoiceMenuFunction
0     52   0    0 16904 12.0  @goto14433
0    952   1    1  3701  5.9  DisplaySettingsSelection
0     44   0    0  3701  5.9  @goto14811
0     96   0    0             @goto14845
0   1232   2    6  5982 10.0  DisplaySettingsNumericSelections
0     80   0    0  5982 10.0  @goto14957
0    950   1    4  6106 12.1  EAdjustments
0     52   0    0  6106 12.1  @goto15026
0   1394   2    0  9809  9.0  InputControls
0     54   0    0   318  4.6  ProcessInputs
0    200   0    0   710  4.8  IMove
0     68   0    1   346  4.1  LCDProcesses
0    314   0    6  2321  9.1  LCDE2Function
0    578   1   14  4160  7.7  ShowPRMessages
0     68   0    0   176  3.5  ShowFirstRow
0   1076   1    8  6869  9.6  ShowSecondRow
0    816   1    1  3168  6.4  LearnMessageAddress
0    102   0    8   428  6.9  LCDVarToNumber8Bit
0    626   1    4  1688  5.4  LCDUnitDisplay
0    130   0    4   508  5.7  LCDScrollingText
0   1090   1    2  6927  8.0  PRFunction
0    236   0   12  1099  6.9  FindE
0    280   0    0   555  3.4  CalculatePulses
0     12   0    0    78  2.0  SetInitialPreFlags
0    808   1    4  2030  6.9  FindFlags
0    334   0   15   790  7.9  CalculatePos
0    258   0    1  1365  4.9  ErrorControl
0     34   0    0   141  2.6  ControlsBeforeMainLoop
0    116   0    0   711  4.8  PReset
0    268   0    1  1090  6.3  RecordsVarsDelivery
0    108   0    4   479  6.3  ShiftToRecords
0     10   0    1    52  1.8  RecordsProcess
0    100   0    0   375  5.1  PFProcess
0    226   0    0  1354  6.8  FFProcess
0    214   0    0  1017  3.4  StartUp
0     42   0    0   212  2.7  Timer0Init
0    260   0    1  2590  5.9  InitVariables
0    292   0    3  1690  6.9  EInit
0    256   0    0  1874  5.9  AssignConfToVariables
0    102   0    2   267  5.9  AssignConfigurations
0    114   0    3   208  5.0  AssignDefaultConfigurations
0    114   0    6   343  6.3  AssignDisplayValues
0    110   0    4   220  5.0  AssignDefaultDigitalValues
0     96   0    0   863  3.9  GlobalTimers
0    360   0    0  2601  4.9  TimerProduce
0     64   0    0   458  3.8  ProcessesAccordingToTimers
0    356   0    0   457  2.3  MAIN
 Inline         0   457  2.3  @cinit1

Program metrics:
    Functions               142
    Statements              5586
    Comments                2403
    Volume (V)              455873
    Difficulty (D)          825.5
    Effort to implement (E) 376314324
    Time to implement (T)   5807 hours, 19 minutes
    Est Delivered Bugs (B)  174
    Cyclomatic Complexity   1539
    Maintainability (MI)    72

 Segment     Used  Free
-----------  ----  ----
00000-00002     4  0  
00004-00006     0  4  
00008-00094   142  0  
00096-0FFFE  38244  27142
 

You are posting Compiler output and not code.
 

I told I will send the .STA file.
Around thirty files are included in the source code. If you can tell a specific part, it will be easier.
For eg. I can attach the function calling ShiftToRecords, and that function is called from main loop in void main.
Timer interrupt is working at the same time. I can attach sym file if you request. If these won't be enough, I think I will have to go on with the temporary solution until the next problem if happens.
 

Which PIC are you using ?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top