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

Pointer problem with v4.043, 4.044

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



Joined: 15 Jan 2007
Posts: 10
Location: EU

View user's profile Send private message

Pointer problem with v4.043, 4.044
PostPosted: Tue Jul 17, 2007 1:48 am     Reply with quote

Hi,

Since v4.043 there seems to be a problem with pointers.
Example:

Code:

// global:
char input_buf[0x1A0]; // input buffer
char *cp;
signed int8 i,j;

//within function:
int1 same=true;
cp=strchr(input_buf,'i'); // goto correct serial input buffer location
for (i=0;i<3;i++) {
    for (j=0;j<2;j++) if (read_eeprom(0x9C+i*2+j)!=*cp++) same=false;
    cp+=0xE;
}


The second 'for' statement will actually increase the 'cp' pointer 3 times instead of the required and expected 2 times.

All other versions of the compiler I used showed a proper behaviour in this respect.

Best regards,
Auke Nauta
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 3:28 am     Reply with quote

If I simplify your code concerning just the cp variable, it is like:
Code:
for (i=0;i<3;i++)
{
    cp+=0xE;
}
This will loop 3 times, so cp _must_ be increased 3 times. If you have other compiler versions doing it otherwise than those other compiler versions are wrong... Cool
Guest








PostPosted: Tue Jul 17, 2007 5:10 am     Reply with quote

I think the OP meant the second nested 'for', which should indeed execute twice and not 3 times?

Code:
// global:
char input_buf[0x1A0]; // input buffer
char *cp;
signed int8 i,j;

//within function:
int1 same=true;
cp=strchr(input_buf,'i'); // goto correct serial input buffer location
for (i=0;i<3;i++) {
    for (j=0;j<2;j++)
        if (read_eeprom(0x9C+i*2+j) != *cp++)
          same=false;
    cp+=0xE;
}


One thing - how do you know the second 'for' executes more than twice? Debugger?
anauta



Joined: 15 Jan 2007
Posts: 10
Location: EU

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 5:51 am     Reply with quote

Anonymous wrote:
I think the OP meant the second nested 'for', which should indeed execute twice and not 3 times?


You are correct. Thanks for pointing that out!

Anonymous wrote:
One thing - how do you know the second 'for' executes more than twice? Debugger?


Yes. Also regular program execution (obviously) failed.
I can recommend the debugger wholeheartedly. The hardware is cheap, the software powerful!

Best regards,
Auke
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 7:10 am     Reply with quote

anauta wrote:
Anonymous wrote:
I think the OP meant the second nested 'for', which should indeed execute twice and not 3 times?


You are correct. Thanks for pointing that out!
Allright, I missed that one. Embarassed

This might however be seen as a weakness in your code. The code is written so dense with many things happening in a single line that it is hard to follow. My own coding style uses more lines for the same code but who cares as the optimizer will create the same compact code anyway. A problem with this dense coding style is that it is very hard to debug, you can only set breakpoints on a line, not on part of a line. Single stepping the debugger over this if-statement you will have executed about six C command (3 additions, a multiply, a function call and a compare).

I don't have v4.043 or newer so can't test your code. Can you provide us with more details about how you know pointer cp is excecuted more than twice in the second for loop? Do you have some example values?
Or maybe you can post the assembly listing of this part?
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