Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1
Programming 43

printf("The 'unsigned int' data type is\t %d bytes\n", sizeof(unsigned int));
printf("The 'short int' data type is\t %d bytes\n", sizeof(short int));
printf("The 'long int' data type is\t %d bytes\n", sizeof(long int));
printf("The 'long long int' data type is %d bytes\n", sizeof(long long int));
printf("The 'float' data type is\t %d bytes\n", sizeof(float));
printf("The 'char' data type is\t\t %d bytes\n", sizeof(char));
}


This piece of code uses the printf() function in a slightly different way.


It uses something called a format specifier to display the value returned from


the sizeof() function calls. Format specifiers will be explained in depth later,


so for now, let’s just focus on the program’s output.


reader@hacking:~/booksrc $ gcc datatype_sizes.c
reader@hacking:~/booksrc $ ./a.out
The 'int' data type is 4 bytes
The 'unsigned int' data type is 4 bytes
The 'short int' data type is 2 bytes
The 'long int' data type is 4 bytes
The 'long long int' data type is 8 bytes
The 'float' data type is 4 bytes
The 'char' data type is 1 bytes
reader@hacking:~/booksrc $


As previously stated, both signed and unsigned integers are four bytes in


size on the x86 architecture. A float is also four bytes, while a char only needs


a single byte. The long and short keywords can also be used with floating-point


variables to extend and shorten their sizes.


0x263 Pointers


The EIP register is a pointer that “points” to the current instruction during a


program’s execution by containing its memory address. The idea of pointers


is used in C, also. Since the physical memory cannot actually be moved, the


information in it must be copied. It can be very computationally expensive to


copy large chunks of memory to be used by different functions or in differ-


ent places. This is also expensive from a memory standpoint, since space for


the new destination copy must be saved or allocated before the source can be


copied. Pointers are a solution to this problem. Instead of copying a large


block of memory, it is much simpler to pass around the address of the begin-


ning of that block of memory.


Pointers in C can be defined and used like any other variable type.


Since memory on the x86 architecture uses 32-bit addressing, pointers are


also 32 bits in size (4 bytes). Pointers are defined by prepending an asterisk (*)


to the variable name. Instead of defining a variable of that type, a pointer is


defined as something that points to data of that type. The pointer.c program


is an example of a pointer being used with the char data type, which is only


1byte in size.

Free download pdf