Pointer program that creates a memory map table

For a HW assignment I have to use an incomplete program with pointers to create a memory map table. How do I use a loop to create a table for this program?
Prompt:
Show the memory map at the point the program reaches the system("pause"); statement.
I'm on macOS by the way, so the system pause statement is irrelevant and does not work for me.

VARIABLE	CONTENTS	ADDRESS
age	         Answer1	1200
number	         Answer1	1204
pressure	 Answer2	1208
mass	         Answer3	1212
name	         Answer4	1216
 	              \0	1220
p_data	         Answer5	1224
p_value	         Answer6	1228
p_one	         Answer7	1232
p_two	         Answer8	1236
p_name	         Answer9	1240
 	 	 
NOTE: Address 1220 Contains string terminator \0  


My code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
int main(void)
{

int number = 7;			// Integer variable
int age = 30;				// Integer variable
float pressure =  45.7;		// Float variable
float mass = 2.3489;		// Float variable
string name = "John";		// String variable

int *p_value = nullptr;		// Pointer to an integer 
int *p_data = nullptr;		// Pointer to another integer
float *p_one = nullptr;		// Pointer to a float type
float *p_two = nullptr;		// Pointer to a float type
string *p_name = nullptr; 		// Pointer to a string type
int j = 0, k = 0;			// For use in loops
int array_one[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; // An array

	// Initialize the pointers. NOTE: * is not used here.

	p_data = &age;
   p_value = &number;
	p_one = &pressure;
p_two = &mass;
p_name = &name;

cout << "age = " << age << endl;
cout << "value = " << number << endl;
cout << "pressure = " << pressure << endl;
cout << "Mass = " << mass << endl;
cout << "name = " << pressure << endl;

cout << setw(15) << "Variable" << setw(15) << "CONTENTS" << setw(15) << "ADDRESS" << endl;
    
return 0;
}

Last edited on
You have the name and the contents. To get the address of a variable use &. So to get the address of age use &age. Similar for the other variables. Note if you need to display the address of a char, you need to cast to void* - otherwise it is treated as a c-style string.
@seeplus
1
2
3
4
5
6
7
8
9
10
11
cout << setw(15) << "Variable" << setw(15) << "CONTENTS" << setw(15) << "ADDRESS" << endl;
cout << setw(15) << "age " << setw(15) << age << endl;
cout << setw(15) << "value " << setw(15) << number << endl;
cout << setw(15) << "pressure " << setw(15) << pressure << endl;
cout << setw(15) << "Mass " << setw(15) << mass << endl;
cout << setw(15) << "name " << setw(15) << pressure << endl;
cout << setw(15) << "p_data" << setw(15) << p_data << endl;
cout << setw(15) << "p_value" << setw(15) << p_value << endl;
cout << setw(15) << "p_one" << setw(15) << p_one << endl;
cout << setw(15) << "p_two" << setw(15) << p_two << endl;
cout << setw(15) << "p_name" << setw(15) << p_name << endl;

I did not include the address because I just want to find the contents

age = 30
value = 7
pressure = 45.7
Mass = 2.3489
name = 45.7
       Variable       CONTENTS        ADDRESS
           age              30
         value               7
      pressure            45.7
          Mass          2.3489
          name            45.7
         p_data 0x7ffee3560584
        p_value 0x7ffee3560588
          p_one 0x7ffee3560580
          p_two 0x7ffee356057c
         p_name 0x7ffee3560560

I think I am doing this right but would there be a better way to print the memory map?
Topic archived. No new replies allowed.