How to get the memory address of an object stored inside an array?

Hi,

I'm trying to create 4 objects of Student on the heap and store them in the pupil array. How do I print out the address of the object contained in the array?

I also get the following warining:

69: warning: deleting array 'pupil'
delete[] pupil;
^

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include <iostream>

using namespace std;

class Student{
private:
    int ID;
    int score;
public:
    void setID(int num);
    int getID();
    void setScore(int num);
    int getScore();
};
void Student::setID(int num)
{
    ID = num;
}

int Student::getID()
{
    return ID;
}
void Student::setScore(int num)
{
    score = num;
}

int Student::getScore()
{
    return score;
}

class Creator
{
public:
    static int nextID;
    Student* getObject();
};
int Creator::nextID = 0;
Student* Creator::getObject()
{
    Creator::nextID++;
    Student* temp = new Student;
    temp->setID(Creator::nextID);
    cout<<"Address for "<<Creator::nextID<<" "<<&temp<<endl;
    return temp;
}

int main()
{
    Creator maker;
    Student *pupil[4];

    int mark = 70;
    for(std::size_t i = 0; i < (sizeof(pupil)/sizeof(pupil[0])); i++)
    {
        pupil[i] = maker.getObject();
        pupil[i]->setScore(mark);
        mark += 10;
        cout<<"Address for "<<pupil[i]->getID()<<" "<<&pupil[i]<<endl;
    }

    for(std::size_t i = 0; i < (sizeof(pupil)/sizeof(pupil[0])); i++)
    {
        cout<< "Sudent ID: "<<pupil[i]->getID()<<" has score of: "<<pupil[i]->getScore()<<endl;
    }

    delete[] pupil;
    return 0;
}


cout<<"Address for "<<pupil[i]->getID()<<" "<<&pupil[i]<<endl; will print the address of the ith element not the address of the object stored in the ith element.

I tried &&pupil[i], didn't work.

Student *temp = &pupil[i];
cout<<"Address for "<<pupil[i]->getID()<<" "<<&temp<<endl;

still didn't work.
Last edited on
delete[] pupil;
This is wrong. You may only delete what you got from new. You did not get pupil from new.


How do I print out the address of the object contained in the array?

The array does not contain the objects. it contains some pointers.

Those pointers are addresses. You can either just output the value of the pointer
cout << pupil[0];
or you can dereference the pointer to get the object, and then get the objects address.
cout << &(*(pupil[0]));

Thank you very much for taking the time to reply :)

I changed delete[] pupil; to

1
2
3
4
for(std::size_t i = 0; i < (sizeof(pupil)/sizeof(pupil[0])); i++)
    {
        delete pupil[i];
    }


Is this the correct way to delete the objects? What if I declare the pupil array as Student *pupil = new Students[4];, will delete[] pupil; delete the objects stored in the array?

When I used pupil[0] instead of &pupil[i], I got the following output:

Address for 1 0x28fe4c
Address for 1 0x692870
Address for 2 0x28fe4c
Address for 2 0x692880
Address for 3 0x28fe4c
Address for 3 0x692890
Address for 4 0x28fe4c
Address for 4 0x6928a0
Sudent ID: 1 has score of: 70
Sudent ID: 2 has score of: 80
Sudent ID: 3 has score of: 90
Sudent ID: 4 has score of: 100

1. the objects created and objects stored in the array have different addresses? I was expecting them to match since the object was created on the heap and I'm copying it's pointer.

2. It seems that Creator::getObject() returns the same pointer in all four calls
Address for 1 0x28fe4c
Address for 2 0x28fe4c
Address for 3 0x28fe4c
Address for 4 0x28fe4c
Does that mean that only one object was created? I was expecting 4 different memory address for each object created?
Last edited on
1
2
3
4
5
6
void bar() {
    Foo * temp = new Foo;
    cout << &temp;
    cout << temp;
    cout << *temp;
}

The 'temp' is a local variable. Variable has an address. Something. That is what you print. (Line 3)

The 'temp' is also a pointer. A variable that holds a value. A value that is the address of some other object.(Line 4 prints the address of the Foo object.)

Dereferencing a pointer gives access to the pointed to object. Line 5 prints the value of the dynamically allocated Foo object.

I was expecting 4 different memory address

Local variables are in stack. Same location of stack may or may not be allocated for a function('s variables) on successive function calls. That depends on many things.
Last edited on
if you mean real memory address you need dos or maybe win 3.x
i dont see purpose of that all
adress (but not real adress) of array unit is
ie
tchar *m = new tchar[55];
//say you need address of 48th unit
thar *t48 = m + sizeof(tchar)*47;
Topic archived. No new replies allowed.