Sequence Points (C/C++)

Learn or Teach General Knowledge Related to Coding or Hacking

Moderators: g3nuin3, SpeedWing, WhiteHat, mezzo

Sequence Points (C/C++)

Postby L. Spiro » Wed Jan 02, 2008 1:07 pm

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
Last edited by L. Spiro on Sat Jul 19, 2008 11:04 am, edited 1 time in total.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Lolz

Postby abysusslynx » Fri Jan 18, 2008 7:24 am

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?
Legends of ares i am asad 2 say is dead, but wolfteam and soldierfront r great, so c'mon hack hack hack
User avatar
abysusslynx
Hackleberry Fin
 
Posts: 26
Joined: Fri Jan 18, 2008 6:10 am
Location: USA,Florida

Postby kth_prkns » Fri Jan 18, 2008 8:22 am

that has nothign to do with stuff like Excel and PowerPoint -.-
kth_prkns
NULL
 
Posts: 101
Joined: Sun Jan 14, 2007 5:29 am

Re: Lolz

Postby L. Spiro » Fri Jan 18, 2008 9:53 am

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
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Oh

Postby abysusslynx » Sat Jan 19, 2008 4:47 am

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 ^^
Legends of ares i am asad 2 say is dead, but wolfteam and soldierfront r great, so c'mon hack hack hack
User avatar
abysusslynx
Hackleberry Fin
 
Posts: 26
Joined: Fri Jan 18, 2008 6:10 am
Location: USA,Florida

Postby SpeedWing » Sat Jan 19, 2008 4:53 am

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
User avatar
SpeedWing
Defragler
 
Posts: 2031
Joined: Tue Jan 01, 2008 1:00 am
Location: If there is a Will there is a Solution.

Oh

Postby abysusslynx » Sat Jan 19, 2008 6:44 am

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 ^^
Legends of ares i am asad 2 say is dead, but wolfteam and soldierfront r great, so c'mon hack hack hack
User avatar
abysusslynx
Hackleberry Fin
 
Posts: 26
Joined: Fri Jan 18, 2008 6:10 am
Location: USA,Florida

Re: Oh

Postby JB Gzn » Thu Jan 31, 2008 3:10 am

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 *******)
User avatar
JB Gzn
Pro++
 
Posts: 1985
Joined: Sun Jan 27, 2008 7:56 pm
Location: Unknown, please use a pointer.

Postby emocore » Tue Feb 12, 2008 11:28 am

I know programming a bit, and the quiz above was really something
User avatar
emocore
Been Around More
 
Posts: 380
Joined: Tue Feb 12, 2008 10:55 am
Location: Taman Kencana

Postby shinnsohai » Tue Mar 25, 2008 2:12 pm

any links for download delphi7?
User avatar
shinnsohai
n00b
 
Posts: 973
Joined: Mon Feb 18, 2008 7:31 pm
Location: l_ A /\/ G l< A \/\/ I

Postby mezzo » Tue Mar 25, 2008 4:07 pm

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 -
- No thanks, I already have a penguin -
User avatar
mezzo
El Mariachi
 
Posts: 739
Joined: Mon Apr 30, 2007 10:27 pm
Location: Antwerp

Postby maxgenesis » Wed Mar 26, 2008 11:50 am

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...
maxgenesis
I Have A Few Questions
 
Posts: 3
Joined: Wed Mar 26, 2008 3:02 am

Postby L. Spiro » Wed Mar 26, 2008 11:59 am

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
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Postby shinnsohai » Wed Mar 26, 2008 1:16 pm

thank you
User avatar
shinnsohai
n00b
 
Posts: 973
Joined: Mon Feb 18, 2008 7:31 pm
Location: l_ A /\/ G l< A \/\/ I

ooooooo

Postby gooblaster » Wed May 28, 2008 2:00 pm

oooooo. u use borland copilers toooooo. i use the free version. no money to buy the paid version. -.-"
(\__/)
(='.'=)
(")_(") This is bunny. Copy him into your sig to help him acheive world dominance.



Image
User avatar
gooblaster
Sir Hacks-A-Lot
 
Posts: 35
Joined: Sat Apr 19, 2008 12:53 pm
Location: Singapore

Next

Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 0 guests