Click here to Skip to main content
16,014,294 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionC++ gotchas Pin
tom groezer10-Jun-07 11:12
tom groezer10-Jun-07 11:12 
AnswerRe: C++ gotchas Pin
CPallini10-Jun-07 11:19
mveCPallini10-Jun-07 11:19 
GeneralRe: C++ gotchas Pin
tom groezer10-Jun-07 11:22
tom groezer10-Jun-07 11:22 
GeneralRe: C++ gotchas Pin
Christian Graus10-Jun-07 11:59
protectorChristian Graus10-Jun-07 11:59 
GeneralRe: C++ gotchas Pin
CPallini10-Jun-07 20:57
mveCPallini10-Jun-07 20:57 
GeneralRe: C++ gotchas Pin
Roger Stoltz10-Jun-07 21:57
Roger Stoltz10-Jun-07 21:57 
GeneralRe: C++ gotchas Pin
CPallini10-Jun-07 22:11
mveCPallini10-Jun-07 22:11 
GeneralRe: C++ gotchas Pin
Roger Stoltz10-Jun-07 23:51
Roger Stoltz10-Jun-07 23:51 
CPallini wrote:
IMHO should be always
a[1] = 1;
because a[i] = i++; is one statement.
What do you think about?


Well, I can agree on that in this context.
However, this can get very complex for the compiler. i++ is a statement that is parsed by the compiler and would generate almost the same instructions if the statement isn't too complex. Consider the following "complier complex" code:
a[i] = foo( i++ );
I would be surprised if the compiler assigned the array index matching i before the incrementation. It will usually put the variable on the stack prior to the function call and then increment it using the incremented i as array index when assigning. This is also how the VC6 compiler resolves the statement.

But...
the VC6 compiler will assign 1 to a[1] with the following statements:
i = 1;
a[i] = i++;
This is a simpler statement and then the compiler gets smart, but it doesn't work the same way as in the example above, i.e. if the function call is replaced by simply incrementing the variable it won't assign the same array index. And this is for the same compiler. D'Oh! | :doh:


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown

GeneralRe: C++ gotchas Pin
CPallini11-Jun-07 0:22
mveCPallini11-Jun-07 0:22 
Questionbasic question Pin
smzhaq10-Jun-07 9:42
smzhaq10-Jun-07 9:42 
AnswerRe: basic question Pin
Christian Graus10-Jun-07 12:24
protectorChristian Graus10-Jun-07 12:24 
GeneralRe: basic question Pin
smzhaq10-Jun-07 22:23
smzhaq10-Jun-07 22:23 
QuestionGetNamedSecurityInfo problem Pin
KellyR10-Jun-07 9:21
KellyR10-Jun-07 9:21 
AnswerRe: GetNamedSecurityInfo problem Pin
KellyR10-Jun-07 18:05
KellyR10-Jun-07 18:05 
GeneralRe: GetNamedSecurityInfo problem Pin
Mark Salsbery10-Jun-07 18:13
Mark Salsbery10-Jun-07 18:13 
QuestionUsing Microsoft Visual C++ 2005EE Compile Error Pin
Blekk10-Jun-07 8:20
Blekk10-Jun-07 8:20 
AnswerRe: Using Microsoft Visual C++ 2005EE Pin
Christian Graus10-Jun-07 12:27
protectorChristian Graus10-Jun-07 12:27 
GeneralRe: Using Microsoft Visual C++ 2005EE Pin
Blekk11-Jun-07 2:46
Blekk11-Jun-07 2:46 
Questionplease show me the way to read file text quickly Pin
Pham duc an10-Jun-07 7:09
Pham duc an10-Jun-07 7:09 
AnswerRe: please show me the way to read file text quickly Pin
Matthew Faithfull10-Jun-07 8:21
Matthew Faithfull10-Jun-07 8:21 
Questionoutput to printer Pin
KARFER10-Jun-07 4:32
KARFER10-Jun-07 4:32 
AnswerRe: output to printer Pin
Randor 10-Jun-07 11:00
professional Randor 10-Jun-07 11:00 
QuestionIs CSocket a blocking socket ? Pin
Girish60110-Jun-07 1:01
Girish60110-Jun-07 1:01 
AnswerRe: Is CSocket a blocking socket ? Pin
S Douglas10-Jun-07 2:11
professionalS Douglas10-Jun-07 2:11 
AnswerRe: Is CSocket a blocking socket ? Pin
Mark Salsbery10-Jun-07 6:44
Mark Salsbery10-Jun-07 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.