Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* mem.c -- this program reports the virtual address of a variety
- of entities in the program:
- 1. function address (text/code segment)
- 2. global variable address (data segment)
- - initialized data
- - uninitialized data
- 3. local variable address (stack segment)
- - function parameters
- - local variables declared inside functions
- */
- #include <unistd.h>
- struct address{ //variable-address pair structure
- char strVar[100];
- unsigned int address;
- };
- struct address adrs[50]; //array of our address
- int cntr = 0;
- int a, b = 3, c, d = 1; /* global variables */
- int main() {
- /* local variables and function
- prototypes */
- int e, f = 2, g(int), *h, *h1, *h2, *h3, i, j = 4, k(char);
- double d1, d2, d3;
- char c1, c2, c3;
- char *s1 = "String literal 1";
- char *s2 = "String literal 2";
- extern end, edata, etext;
- /* dynamically allocate some storage */
- h = (int *)malloc(40);
- h1 = (int *)malloc(40);
- h2 = (int *)malloc(40);
- h3 = (int *)malloc(40);
- strcpy(adrs[0].strVar, "------Program text ends just before------ %u\n");
- adrs[0].address = (int)&etext;
- //printf(adrs[0].strVar, adrs[0].address);
- strcpy(adrs[1].strVar, "------Initialized data region ends just before------ %u\n");
- adrs[1].address = (int)&edata;
- //printf(adrs[0].strVar, adrs[0].address);
- strcpy(adrs[2].strVar, "------Uninitialized data region ends just before------ %u\n");
- adrs[2].address = (int)&end;
- strcpy(adrs[3].strVar, "------Break between data and stack regions------ %u\n");
- adrs[3].address = (int)sbrk(0);
- strcpy(adrs[4].strVar, "Address of function main is %u\n");
- adrs[4].address = (int)&main;
- strcpy(adrs[5].strVar, "Address of a is %u\n");
- adrs[5].address = (int)&a;
- strcpy(adrs[6].strVar, "Address of b is %u\n");
- adrs[6].address = (int)&b;
- strcpy(adrs[7].strVar, "Address of c is %u\n");
- adrs[7].address = (int)&c;
- strcpy(adrs[8].strVar, "Address of d is %u\n");
- adrs[8].address = (int)&d;
- strcpy(adrs[9].strVar, "Address of e is %u\n");
- adrs[9].address = (int)&e;
- strcpy(adrs[10].strVar, "Address of f is %u\n");
- adrs[10].address = (int)&f;
- strcpy(adrs[11].strVar, "Address of function g is %u\n");
- adrs[11].address = (int)&h;
- strcpy(adrs[12].strVar, "Address of function h is %u\n");
- adrs[12].address = (int)&h1;
- strcpy(adrs[13].strVar, "Address of function h1 is %u\n");
- adrs[13].address = (int)&h2;
- strcpy(adrs[14].strVar, "Address of function h2 is %u\n");
- adrs[14].address = (int)&h3;
- strcpy(adrs[15].strVar, "Address h is pointing to is %u\n");
- adrs[15].address = (int)h;
- strcpy(adrs[16].strVar, "Address h1 is pointing to is %u\n");
- adrs[16].address = (int)h1;
- strcpy(adrs[17].strVar, "Address h2 is pointing to is %u\n");
- adrs[17].address = (int)h2;
- strcpy(adrs[18].strVar, "Address h3 is pointing to is %u\n");
- adrs[18].address = (int)h3;
- strcpy(adrs[19].strVar, "Address of i is %u\n");
- adrs[19].address = (int)&i;
- strcpy(adrs[20].strVar, "Address of j is %u\n");
- adrs[20].address = (int)&j;
- strcpy(adrs[21].strVar, "Address of function k is %u\n");
- adrs[21].address = (int)k;
- strcpy(adrs[22].strVar, "Address of double d1 is %u\n");
- adrs[22].address = (int)&d1;
- strcpy(adrs[23].strVar, "Address of double d2 is %u\n");
- adrs[23].address = (int)&d2;
- strcpy(adrs[24].strVar, "Address of double d3 is %u\n");
- adrs[24].address = (int)&d3;
- strcpy(adrs[25].strVar, "Address of char c1 is %u\n");
- adrs[25].address = (int)&c1;
- strcpy(adrs[26].strVar, "Address of char c2 is %u\n");
- adrs[26].address = (int)&c2;
- strcpy(adrs[27].strVar, "Address of char c3 is %u\n");
- adrs[27].address = (int)&c3;
- cntr = 27;
- g(d);
- k(d);
- /* Sort array in ascending order by account number */
- int x, y = 0;
- for (y = 0; y <= cntr; y++)
- {
- for (x = 0; x <= cntr; x++)
- {
- if (adrs[x].address > adrs[x + 1].address)
- {
- struct address temp = adrs[x];
- adrs[x] = adrs[x + 1];
- adrs[x + 1] = temp;
- }
- }
- }
- printf("Memory map: Memory Address:\n");
- printf("---------------------------------- 0\n");
- for(cntr = 0; cntr < 49; cntr++){
- //printf("cntr is %d", cntr);
- printf(adrs[cntr].strVar, adrs[cntr].address);
- }
- /*
- printf("Program text ends just before %u\n", &etext);
- printf("Initialized data region ends just before %u\n", &edata);
- printf("Uninitialized data region ends just before %u\n", &end);
- printf("Break between data and stack regions is %u\n", sbrk(0));
- printf("address of function main is %u\n", main);
- printf("address of a is %u\n", &a);
- printf("address of b is %u\n", &b);
- printf("address of c is %u\n", &c);
- printf("address of d is %u\n", &d);
- printf("address of e is %u\n", &e);
- printf("address of f is %u\n", &f);
- printf("address of function g is %u\n", g);
- printf("address of h is %u\n", &h);
- printf("address of h1 is %u\n", &h1);
- printf("address of h2 is %u\n", &h2);
- printf("address of h3 is %u\n", &h3);
- printf("address h is pointing to is %u\n", h);
- printf("address h1 is pointing to is %u\n", h1);
- printf("address h2 is pointing to is %u\n", h2);
- printf("address h3 is pointing to is %u\n", h3);
- printf("address of i is %u\n", &i);
- printf("address of j is %u\n", &j);
- printf("address of function k is %u\n", k);
- printf("address of double d1 is %u\n",&d1);
- printf("address of double d2 is %u\n",&d2);
- printf("address of double d3 is %u\n",&d3);
- printf("address of char c1 is %u\n",&c1);
- printf("address of char c2 is %u\n",&c2);
- printf("address of char c3 is %u\n",&c3);*/
- }
- int g(int l) { /* 'l' is a parameter */
- int m; /* 'm' is a local variable */
- strcpy(adrs[++cntr].strVar, "Address of l is %u\n");
- adrs[cntr].address = (int)&l;
- strcpy(adrs[++cntr].strVar, "Address of m is %u\n");
- adrs[cntr].address = (int)&m;
- }
- int k(char n) { /* 'n' is a parameter */
- double o; /* 'o' is a local variable */
- strcpy(adrs[++cntr].strVar, "Address of n is %u\n");
- adrs[cntr].address = (int)&n;
- strcpy(adrs[++cntr].strVar, "Address of o is %u\n");
- adrs[cntr].address = (int)&o;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement