View previous topic :: View next topic |
Author |
Message |
anauta
Joined: 15 Jan 2007 Posts: 10 Location: EU
|
Pointer problem with v4.043, 4.044 |
Posted: Tue Jul 17, 2007 1:48 am |
|
|
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
|
|
Posted: Tue Jul 17, 2007 3:28 am |
|
|
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... |
|
|
Guest
|
|
Posted: Tue Jul 17, 2007 5:10 am |
|
|
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
|
|
Posted: Tue Jul 17, 2007 5:51 am |
|
|
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
|
|
Posted: Tue Jul 17, 2007 7:10 am |
|
|
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.
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? |
|
|
|