Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® Software > Why I write code creator 2.0, there will be two different situations

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Why I write code creator 2.0, there will be two different situations
Moderator:
JFMD

Post Reply
Follow this topic



Why I write code creator 2.0, there will be two different situations

xuan,Jed posted on 03 Oct 2012 5:22 PM PST
Senior Member
15 Forum Posts

 Why I write code creator 2.0, there will be two different situations

I wrote

{

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr ++];

}

And writing

{

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr];

dptr ++;

}

Will get different results?

(Translation on google translate)

 

 




Re: Why I write code creator 2.0, there will be two different situations

H L posted on 03 Oct 2012 06:53 PM PST
Top Contributor
679 Forum Posts

1. NEVER have a vaiable on the left side changed on the right side

your dptr is modified on the right side, so when saving the value, it would used the CHANGED dptr

In your second example, your dptr doesn't change on the first line, it was incremented in the 2nd line, after the write instruction

 



Re: Why I write code creator 2.0, there will be two different situations

H L posted on 03 Oct 2012 06:54 PM PST
Top Contributor
679 Forum Posts

I also would not use dptr as dptr should be a reserved name in PSOC3 which is a 8051 core.



Re: Why I write code creator 2.0, there will be two different situations

SpiderKenny posted on 04 Oct 2012 02:04 AM PST
Top Contributor
83 Forum Posts

 The basic principle here is that the expression on the right hand side of "=" is evaluated before the one on the left. So the pointer is incremented before being used in the assignment.

 

-Kenny



Re: Why I write code creator 2.0, there will be two different situations

Bob Marlowe posted on 04 Oct 2012 04:46 AM PST
Top Contributor
1768 Forum Posts

The problem is that in C it is not defined what evaluation has to be done first, the address of the result (LValue) or the expression at the right hand side of the equal sign. So a change of / in the compiler could induce a completely different behaveour. A nightmare for every programmer!

Have a look here http://publications.gbdirect.co.uk/c_book/chapter8/sequence_points.html at "Sequence Points" and see, that exactly the problem you have got is mentioned: NEVER use constructs like

a{i] = i++;

 

Happy coding

Bob



Re: Why I write code creator 2.0, there will be two different situations

xuan,Jed posted on 04 Oct 2012 07:25 AM PST
Senior Member
15 Forum Posts

 I use Designer writing

{

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr ++];

}

Will be equal to Create 2.0

{

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr];

dptr + +;

}

(Translation on google translate)



Re: Why I write code creator 2.0, there will be two different situations

H L posted on 05 Oct 2012 02:24 AM PST
Top Contributor
679 Forum Posts

 It would be better to use the second method. 

Don't use the first one.

It is a general pratise for any compiler or IDE.

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20