59

I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it? What I have now is:

Student * db = new Student[5];

But each element in that array is the student object, not a pointer to the student object. Thanks.

5 Answers 5

108
Student** db = new Student*[5];
// To allocate it statically:
Student* db[5];
Sign up to request clarification or add additional context in comments.

2 Comments

How is Student* db[5] the same thing as Student** db = new Student*[5]? What is the benefit of using one over the other?
When you wish to return an array from a function the array needs to be allocated on the heap. If it s allocated on the stack it is destroyed immediately the scope of that function ends.
20
#include <vector>
std::vector <Student *> db(5);
// in use
db[2] = & someStudent;

The advantage of this is that you don't have to worry about deleting the allocated storage - the vector does it for you.

4 Comments

While vector is the suggested way to have a list of stuff, strictly speaking, it's not an array.
It can be used in every way that an array can be used. If it quacks like a duck, it's a duck.
The vector will not delete the allocated storage automatically, it will free the storage for the pointers but not for the memory that the pointer points to. Use boost::ptr_vector for that.
Did you read my example code? Should the storage pointed to by element [2] be deleted?
13

An array of pointers is written as a pointer of pointers:

Student **db = new Student*[5];

Now the problem is, that you only have reserved memory for the five pointers. So you have to iterate through them to create the Student objects themselves.

In C++, for most use cases life is easier with a std::vector.

std::vector<Student*> db;

Now you can use push_back() to add new pointers to it and [] to index it. It is cleaner to use than the ** thing.

Comments

0

If the size of the array is known at compile time, I tend to use std::array

class Student {
public:
    Student() {
        std::cout << "Student Created\n";
    }
    ~Student() {
        std::cout << "Student Deleted\n";
    }

    int a;
};


int main() {
    std::array<Student*, 5> arr;
    arr[1] = new Student();
    arr[1]->a = 42;
    std::cout << arr[1]->a << std::endl;
    
    delete arr[1];    // Don't forget to call the destructor
    return 0;
}

Output:

Student Created
42
Student Deleted

Note, that arr won't initialize elements to nullptr, might contain memory garbage. Also, don't forget to clean up when you're done.

Better approach would be to use RAII as follows:

class Student {
public:
    Student() {
        std::cout << "Student Created\n";
    }
    ~Student() {
        std::cout << "Student Deleted\n";
    }

    int a;
};


int main() {
    std::array<std::unique_ptr<Student>, 5> arr;
    arr[1] = std::make_unique<Student>();
    arr[1]->a = 42;
    std::cout << arr[1]->a << std::endl;
    
    // No delete needed here, unique_ptr  will take care of lifetime management
    return 0;
}

Same output, but safer:

Student Created
42
Student Deleted

Comments

-6
    void main()
    {
    int *arr;
    int size;
    cout<<"Enter the size of the integer array:";
    cin>>size;
    cout<<"Creating an array of size<<size<<"\n";
        arr=new int[size];
    cout<<"Dynamic allocation of memory for memory for array arr is successful";
    delete arr;
    getch();enter code here
}

3 Comments

This adds nothing
this doesn’t answer the question : in this code you create an array of int, not an array of pointers
This is not what OP is asking. Can you elaborate on the answer please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.