Gray Hat Hacking: The Ethical Hacker??™s Handbook
130
It is important to note that even though the size of the pointer is set at 4 bytes, the size of
the string has not been set with the preceding command; therefore, this data is considered
uninitialized and will be placed in the .bss section of the process memory.
As another example, if you wanted to store a pointer to an integer in memory, you
would issue the following command in your C program:
int * point1; // this is read, give me 4 bytes called point1 which is a
//pointer to an integer variable.
To read the value of the memory address pointed to by the pointer, you dereference the
pointer with the * symbol. Therefore, if you wanted to print the value of the integer
pointed to by point1 in the preceding code, you would use the following command:
printf("%d", *point1);
where the * is used to dereference the pointer called point1 and display the value of the
integer using the printf() function.
Putting the Pieces of Memory Together
Now that you have the basics down, we will present a simple example to illustrate the
usage of memory in a program:
/* memory.c */ // this comment simply holds the program name
int index = 5; // integer stored in data (initialized)
char * str; // string stored in bss (uninitialized)
int nothing; // integer stored in bss (uninitialized)
void funct1(int c){ // bracket starts function1 block
int i=c; // stored in the stack region
str = (char*) malloc (10 * sizeof (char)); // Reserves 10 characters in
// the heap region */
strncpy(str, "abcde", 5); //copies 5 characters "abcde" into str
} //end of function1
void main (){ //the required main function
funct1(1); //main calls function1 with an argument
} //end of the main function
This program does not do much.
Pages:
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290