CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Strange code results using an array [solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
/dev/human



Joined: 01 Sep 2008
Posts: 19
Location: Earth / Europe / Germany

View user's profile Send private message

Strange code results using an array [solved]
PostPosted: Wed Apr 22, 2009 11:03 pm     Reply with quote

Hi All

I programmed a simple piece of code to handle a simple stack.

Code:

   float32 TemppH;
   float32 TempT;
   int loop;

   StackHnd (0, 0, Clear);
   for (loop = 0; loop < 20; loop++) {
      TempT = loop+1;
      TemppH = loop+1;
      StackHnd (&TemppH, &TempT, Push);
   }


int StackHnd (float32* pH, float32* T, int Cmd) {

   static int StackPntr = 0;
   float32 phStack [Stacksize];
   float32 TStack [Stacksize];
   
   switch (Cmd) {
      case Clear:
         for (StackPntr = 0; StackPntr < Stacksize; StackPntr++) {
            pHStack [StackPntr] = 0.0;
            TStack [StackPntr] = 0.0;
         }
      break;
     
      case Push:
         for (StackPntr = 1; StackPntr < Stacksize; StackPntr++) {   // Top of stack is "StackSize", bottom is 0
            phStack [StackPntr -1] = phStack [StackPntr];            // Push all pH values down
            TStack [StackPntr -1] = TStack [StackPntr];              // Push all temp values down
         }
         pHStack [StackSize-1] = *pH;                                // Put actual value on top
         TStack [StackSize-1] = *T;                                  // Put actual value on top
      break;
     
      case Calc:
      break;
     
      default:
     
   }
   return 0;
}


This piece does not seem to difficult to me but checking the stack I do see:

Quote:

pHStack: (2.172,3.000,4.000,5.000,6.000,7.000,8.000,9.000,10.000,11.000)
TStack: (2.000,3.000,4.000,5.000,6.000,7.000,8.000,9.000,10.000,11.000)

Any ideas for pHStack [0] Question

Thanks in advance!
_________________
Never check your brain at the door!


Last edited by /dev/human on Thu Apr 23, 2009 10:51 pm; edited 1 time in total
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Apr 22, 2009 11:30 pm     Reply with quote

The problem could be traced, if you had provided complete code, but you preferred not to do.

I guess, that phStack is overwritten, cause it's not declared static. Can be easily checked in the projects *.sym file. Generally, the compiler can be expected (respectively is required) to overlay phStack with other variables outside the function, to save RAM space.
/dev/human



Joined: 01 Sep 2008
Posts: 19
Location: Earth / Europe / Germany

View user's profile Send private message

PostPosted: Thu Apr 23, 2009 10:51 pm     Reply with quote

Thanks, stupidly blind. The ram space was actually used by another var.

... and yeah, I actually preferred not to send the remaining about 564 lines of code. Very Happy
_________________
Never check your brain at the door!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group