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

Function returns wrong value - fixed by declaring unused int
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 10:31 am     Reply with quote

Quote:
What I was trying to get across was that a 'microcontroller' is NOT a microcomputer,minicomputer,desktop, mainframe or whatever

Yes it isn't a big computer. But that's absolutely no excuse for incorrect results. It would be much more acceptable, if the compiler gives an error message, telling "Sorry I'm not prepared for this complex arithmetic, try a different way".

Of course, any available µP will be stressed sometimes up to it's reasonable limits, or possibly beyond it. Often this isn't obvious when starting a project, or the complexity is increasing incrementally through the application revisions. Typically, the developer get's aware of it, when the program or data memory is exhausted.

But as long as the code is valid C and get compiled apparently correctly, you can expect correct results.
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 11:35 am     Reply with quote

I completely agree FvM. Temtronic you seem to have forgotten about the problem we are discussing. All I'm doing in this code is a few basic sums to work out some geometry. Allocating memory is a fundamental of any compiler and overlapping memory from variables that are in use at the same time is a fault and shouldn't be passed off. If CCS can't handle structures, or nested stuctures or the passing of structures from one function to another then it shouldn't allow you to compile them.
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 3:05 pm     Reply with quote

Just out of curiosity, have you tried changing the optimization level of the compiler?? There's a reason why this is an option, and it's to CCS's credit that its almost never necessary, but us old schoolers will remember always dorking around with compile options in Fortran.
Ttelmah



Joined: 11 Mar 2010
Posts: 20159

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 3:31 pm     Reply with quote

There is a danger that what is happening may be completely correct and normal!...
The code as posted, can't be compiled, since we don't have a declaration for 'point' for example. However there is a classic problem, which this could be an example of, which is pointer back-referencing.
Code:

typedef struct demo{
    char *data;
    int cnt;
} thingy;

thingy func(void) {
   thingy value;
   char buffer[10];
   int ctr=4;
   value.data=buffer;
   value.cnt=ctr;
   return thingy;
}

Now the problem here, is the pointer contained in 'thingy'. After the function, it points to the _local_ variable inside 'func', which is now pointing to a memory area that _is_ available for re-use. This will cause problems even on 'mainstream' C compilers, if memory optimisations are set high. In CCS, it will cause problems. The compiler has to be told to keep the memory for 'buffer' reserved for the function, even after the function exits (by declaring this area static), or it _will_ eventually be re-used.
The pointer is targeted 'back' to an area that is not owned by the external function....

Best Wishes
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 3:55 pm     Reply with quote

Ttelmah, did you mean the last line of your code to read :
Code:
return value

rather than
Code:
return thingy


Can you clarify how this is different from calling any other function that is expected to return a value.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 4:16 pm     Reply with quote

I must confess, that I thought for a moment about an illegal usage of local variables, but came to the conclusion, that it's completely correct. The local variables that are returned are no pointers. They are structures that are copied word for word to the destination storage after return. I don't see the point, where the code does something wrong.

To refer to the example given by Ttelmah in detail, the problem in the example is the pointer referencing a local variable. So it can't be dereferenced correctly, when the function storage area has been disposed. In my opinion, there's no principle difference, if the local storage is on the stack as with standard processors, or in managed global data space, as with CCS C.

But there's no similar problem in the discussed code.

P.S.:
Quote:
The code as posted, can't be compiled, since we don't have a declaration for 'point' for example.
Don't know what you mean. The example code is complete and compilable.

Finally, if you are not sure if returning a local structure possibly causes problems, you can convert it in a global variable for trial. But the reported problem doesn't remove.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Feb 25, 2011 2:39 am     Reply with quote

The below code replacing the local storage (partly) by pointer operations basically works. It uses less RAM but more ROM and executes slower.
Code:
int Test_Plane(plane *pG, coord *pP)
{
   //I is intersection points.  Only need 4, but need 10 extras to allocate RAM
   //to this function.  Looks like a bug with the compiler.
   float I[4];
   line L;
   //get x intersection with line 1 (horizontal base line)
   L = pG->line1;
   I[0] = Get_Intersect_Vert(pP->x, L);
   printf("%.0g\n\r",I[0]);
   //get x intersection with line 2 (horizontal top line)
   L = pG->line2;
   I[1] = Get_Intersect_Vert(pP->x, L);
   printf("%.0g\n\r",I[1]);
   //get y intersection with line 3 (vertical left line)
   L = pG->line3;
   I[2] = Get_Intersect_Hor(pP->y, L);
   printf("%.0g\n\r",I[2]);
   //get y intersection with line 4 (vertical right line)
   L = pG->line4;
   I[3] = Get_Intersect_Hor(pP->y, L);
   printf("%.0g\n\r",I[3]);
   //Test if point P is inbetween the 4 intersection points
   if ( (I[0] < pP->y) & (I[1] > pP->y) & (I[2] < pP->x) & (I[3] > pP->x) ) return(1);
   else return(2);
}
//Returns calibration data in a grid format based on the grid position X and Y
void Get_Plane(int X, int Y, plane *pG)
{
   line L1, L2, L3, L4;
   L1.c1 = CAL_POINT[Y-1][X-1];
   L1.c2 = CAL_POINT[Y-1][X];
   L2.c1 = CAL_POINT[Y][X-1];
   L2.c2 = CAL_POINT[Y][X];
   L3.c1 = CAL_POINT[Y-1][X-1];
   L3.c2 = CAL_POINT[Y][X-1];
   L4.c1 = CAL_POINT[Y-1][X];
   L4.c2 = CAL_POINT[Y][X];
   pG->line1 = L1;
   pG->line2 = L2;
   pG->line3 = L3;
   pG->line4 = L4;
}

If you try
Code:
Get_Intersect_Vert(pP->x, pG->line1);

you get a demonstration of CCS C's dislike of complex code constructs Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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