#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
extern char **environ;
int global_x = 10; // initialised global variable
int global_y; // un-initialised global variable
char global_array1[] = "Hello, world!"; // initialised global array and a string literal
char global_array2[10]; // un-initialised global array
char *global_pointer1 = "bye!"; // global pointer to a string literal
char *global_pointer2; // un-initialised global pointer
float global_float = 100.1; // initialised global variable
double global_double; // un-initialised global variable
#define ONEGB 1073741824
#define ONEMB 1048576
#define ONEKB 1024
char *addr(unsigned long a)
{
unsigned long r; // remainder
r = (unsigned long) a;
int gb = (int) ( r / ONEGB );
r -= gb * ONEGB;
int mb = (int) ( r / ONEMB );
r -= mb * ONEMB;
int kb = (int) ( r / ONEKB );
r -= kb * ONEKB;
int b = (int) ( r );
char *p = malloc(64);
sprintf(p, "%4dGB, %4dMB, %4dKB, %4d", gb, mb, kb, b);
return p;
}
int f2(int x)
{
char * f2_p;
int f2_x = 21;
f2_p = malloc(1000); // dynamically allocated memory
// print out the address of x
// print out the addresses of f2_p, and f2_x
// print out the starting address of the dynamically allocated memory
.....
L: f2_x = 10;
return f2_x;
}
void f1(int x1, int x2, float x3, char x4, double x5, int x6)
{
int f1_x = 10;
int f1_y;
char *f1_p1 = "This is inside f1"; // pointer to another string literal
char *f1_p2;
f1_p2 = malloc(100); // dynamically allocated memory
// print out the addresses of x1, x2, x3, x4, x5, x6
// print out the addresses of f1_x, f1_y, f1_p1, f1_p2
// print out the address of the string literal "This is inside f1"
.....
f1_y = f2(10);
return;
}
int main(int argc, char *argv[])
{
// print out the addresses of argc, argv
// print out the starting address and end address of the command line arguments of this process
// print out the starting address and end address of the environment of this process
// print out the starting addresses of function main, f1, and f2
// print out the addresses of global_x, global_y, global_array1, global_array2, global_pointer1,
// global_pointer2, global_float, global_double
// print out the addresses of string literals 10, "Hello, world!", "bye", 100.1
.....
// call function f1 with suitable arguments such as 12, -5, 33.7, 'A', 1.896e-10, 100
f1( .... );
exit(0);
}
I tried to search on google, but cannot find sth useful, and in this case I just want to figure out how to print out the starting address of the dynamically allocated memory; print out the starting address and end address of the command line arguments of this process;print out the starting address and end address of the environment of this process;print out the starting addresses of function main, f1, and f2.
anybody can help me?..thank you!
In
main:print out the addresses of
argc, argv–printf ("%d, %d", &argc, argv);print out the starting address and end address of the command line arguments of this process –
printf ("%d", (void *)argv);print out the starting address and end address of the environment of this process –
printf ("%d", (void *)environ);print out the starting addresses of function
main, f1, and f2–printf ("%d %d %d", &main, &f1, &f2);print out the addresses of
global_x, global_y, global_array1, global_array2, global_pointer1, global_pointer2, global_float, global_double– just use the&operator in front of each variable whose address you want to print.print out the addresses of string literals
10, "Hello, world!", "bye", 100.1– printing addresses of string literals is not allowed.In
f1:print out the addresses of x1, x2, x3, x4, x5, x6 –
printf ("%d %d %d %d %d %d", &x1, &x2, &x3, &x4, &x5);print out the addresses of f1_x, f1_y, f1_p1, f1_p2 –
printf ("%d %d %d %d", &f1_x, &f1_y, f1_p1, f2_p2);print out the address of the string literal
"This is inside f1"– Taking address of a string literal is not allowedIn
f2:print out the address of x –
printf ("%d", &x);print out the addresses of f2_p, and f2_x –
printf("%d", f2_p, &f2_x);print out the starting address of the dynamically allocated memory –
printf ("%d", f2_p);