Page 1 of 2

Sequence Points (C/C++)

PostPosted: Wed Jan 02, 2008 1:07 pm
by L. Spiro
Quick Quiz:
What is the value of I after the following:

Code: Select all
INT J = 9;
INT I = 1;
INT I = (((++J) + (J++)) + J++) + I++;



Answer:
Undefined.


Everyone should know what the ++ and -- operators are, and everyone should know that the “prefix increment” (++I) works before the statement is executed while the “postfix” increment (I++) works after, but there are a lot of other rules no one ever cares to mention.

For most people, this extra information isn’t important to know. If you code like the above then you deserve to have your programs backfire.

Although you should always avoid ambiguous (and undefined) coding constructs such as the above, you should still understand how they work and why they are so bad.



The above code is undefined. Why?
According to the C99 specifications (the document that lets everyone know the rules for C—every compiler is supposed to conform to these rules): “Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.”.

This rule raises 2 questions:
1: Why?
2: What does it mean by “sequence points”?


#1: Because the document offers no specifications for how the compiler is supposed to implement the -- and ++ operators. It leaves it up to the compiler to decide how to make the -- and ++ operators work.

#2: A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed (from Wikipedia).
In other words, inside the code statement above, we are guaranteed that the ++J has already been done, but none of the J++’s or the I++.
After the statement finishes, the sequence point must be “closed” (for lack of better explaining) by finishing all the remaining ++ operations.



The sequence points are in the following places:

* The semicolon (;).
* The non-overloaded comma-operator (,).
* The non-overloaded || operator.
* The non-overloaded && operator.
* The ternary ?: operator.
* After evaluation of all a function's parameters but before the first expression within the function is executed.
* After a function’s returned object has been copied back to the caller, but before the code just after the call has yet been evaluated.
* After the initialization of each base and member.


Microsoft additionally uses the following (which are a bit redundant):
* The controlling expression in a selection (if or switch) statement.
* The controlling expression of a while or do statement.
* Each of the three expressions of a for statement.


When you enter any of these situations in your code, your prefix -- and ++ will all be executed, then your coded expression will be evaluated, and then all of the postfix -- and ++ operations will be executed.

Knowing this, you might think that you should be able to define an actual answer to the code in the example above.
But you can’t, because as mentioned each compiler may implement ++ and -- in its own way.
If you have 2 of these on the same variable within a sequence point, the compiler may decide to do something along the following logic:
1: Buffer 1st into a register.
2: Buffer 2nd into register (which is the same as 1st because it is the same variable).
3: Increase buffer 1 and write the value to 1st. 1st has been increased.
4: Increase buffer 2 and write the value to 2nd. But 2nd is actually the same variable as 1st, so we just overwrote everything we did in buffer 1.

The result would be that the variable would be incremented only once.


This is just an example.




If you did not already, now you know why this is terrible coding practice and should always be avoided. Other good reasons to avoid this include the simple fact that it is hard to read, nevermind the fact that it will not work the same from one compiler to the next.

My implementation of -- and ++ in L. Spiro Script uses a purely logical approach and would give you the value you would expect if you interpreted the rules strictly, however you should still never use it in your code.


L. Spiro

Lolz

PostPosted: Fri Jan 18, 2008 7:24 am
by abysusslynx
I dont understand it lol
I took business keyboarding/Business Applications Last year and im 14, but i dont understand any thing u wrote lol....Where did u learn all that from?

PostPosted: Fri Jan 18, 2008 8:22 am
by kth_prkns
that has nothign to do with stuff like Excel and PowerPoint -.-

Re: Lolz

PostPosted: Fri Jan 18, 2008 9:53 am
by L. Spiro
abysusslynx wrote:I dont understand it lol
I took business keyboarding/Business Applications Last year and im 14, but i dont understand any thing u wrote lol....Where did u learn all that from?

This has nothing to do with Microsoft Office. This is C/C++/L. Spiro Script/Java—programming languages.

I learned it on my own; I am forced by my interest in (or obsession for) C/C++ to study the language(s) in my spare time.
I happened into information regarding sequence points several years ago when I wrote L. Spiro Script.


The Knowledge Base will have much more in it than just information for C/C++/L. Spiro Script soon, so stay tuned.


L. Spiro

Oh

PostPosted: Sat Jan 19, 2008 4:47 am
by abysusslynx
Oh ok lolz =P

Honestly ive been learning about pc's since i was like 4, because my mom thought we should learn, and last year ive been learning about hacks and stuff, It was not fun, but i learned how 2 use the cheatengine a while ago, of course I just wasnt doing somethign right, the instructions 4 it were hard, and i was tryign 2 hack "Conquer Online" And it worked, i could use a bot, i never learned how 2 use aim bot or speed bot tho....And 4 some1 who doesnt kno much about scripts C.E. and ur program I must say ur program is pretty easy 2 firuge out, once u have a guide, its rlly simple and much easier than 'CheatEngine",All the tutorials on this site, require a "Hex Editor" and they say it doesnt work with urs...im not sure y, but they all use diff. hex editors, which i dont have...It would be much more convient if they would learn 2 use urs or give a link 2 get theirs ^^

PostPosted: Sat Jan 19, 2008 4:53 am
by SpeedWing
This has nothing to do with Microsoft Office. This is C/C++/L. Spiro Script/Java—programming languages.


im 15 atm,

learning java myself just using java editor and a book with a guide.
i can make some easy programs atm. just a dollar calc etc... :P

i really look up to people like u L.Spiro, making their own program that really works and that many people use

Oh

PostPosted: Sat Jan 19, 2008 6:44 am
by abysusslynx
Cool..well i dunno much bout hacking, codes etc.....
But thanks 2 every1 in this forums who try 2 help ignorant ppl like me , and of course a BIG THANKS 2 L.Spiro who even when in unhealth spends him time wokring on something 4 ppl other than himself THANSK EVERY1 ^^

Re: Oh

PostPosted: Thu Jan 31, 2008 3:10 am
by JB Gzn
abysusslynx wrote:Cool..well i dunno much bout hacking, codes etc.....
But thanks 2 every1 in this forums who try 2 help ignorant ppl like me , and of course a BIG THANKS 2 L.Spiro who even when in unhealth spends him time wokring on something 4 ppl other than himself THANSK EVERY1 ^^


your absolutly right.
all other ppl who can make UCE/TRAINERS/Memory hackers/ hax
keep them for themself or sell them.

L. spiro is one of the nicest persons u can meet online in hacking.
(like sean from Gzn he's a real *******)

PostPosted: Tue Feb 12, 2008 11:28 am
by emocore
I know programming a bit, and the quiz above was really something

PostPosted: Tue Mar 25, 2008 2:12 pm
by shinnsohai
any links for download delphi7?

PostPosted: Tue Mar 25, 2008 4:07 pm
by mezzo
shinnsohai wrote:any links for download delphi7?


Google wrote:Borland Delphi 7 Studio: Released!- [ Vertaal deze pagina ]
As with Delphi 6, Personal edition of Delphi 7 will be available for free at www.borland.com/downloads.
Of course, every "bigger" version includes all the ...
delphi.about.com/od/productreviews/a/bld7ann.htm - 24k -

PostPosted: Wed Mar 26, 2008 11:50 am
by maxgenesis
l spiro means how every we create sure can be backfire or the program can use for a temporary time before been bypass back...omg...

PostPosted: Wed Mar 26, 2008 11:59 am
by L. Spiro
Either this is meant to be in this topic but you completely misunderstood every single part or this post is meant for another topic and is entirely off-topic here.


L. Spiro

PostPosted: Wed Mar 26, 2008 1:16 pm
by shinnsohai
thank you

ooooooo

PostPosted: Wed May 28, 2008 2:00 pm
by gooblaster
oooooo. u use borland copilers toooooo. i use the free version. no money to buy the paid version. -.-"