7

I know how to create a array of dynamic objects.

For example, the class name is Stock.

Stock *stockArray[4];
for(int i = 0 ; i < 4;i++)
{
   stockArray[i] = new Stock();
}

How do you change this to dynamic array of dynamic objects?

What I tried:

Stock stockArrayPointer = new Stock stock[4];

It doesn't work and the error is "The value of Stock** cannot be used to initalize an entity of type Stock.

Second question is after the creation of dynamic array of dynamic objects, what is the syntax to access the pointers in the array.

Now, I use stockArray[i] = new Stock(); How will this change?

Need some guidance on this...

4
  • why dont u use vectors? Commented Nov 30, 2013 at 18:28
  • What do you mean by dynamic array of dynamic objects? Can you elaborate? Commented Nov 30, 2013 at 18:32
  • 1
    an array that is in heap and things in the array are also in heap... Commented Nov 30, 2013 at 18:34
  • 1
    Why not just Stock *stockArray = new Stock[4]; Commented Nov 30, 2013 at 18:38

4 Answers 4

12

If you are using c++ then you shouldn't reinvent the wheel, just use vectors:

#include <vector>

std::vector< std::vector< Stock > > StockVector;

// do this as many times as you wish
StockVector.push_back( std::vector< Stock >() );

// Now you are adding a stock to the i-th stockarray
StockVector[i].push_back( Stock() );

Edit:

I didn't understand your question, if you just want to have and array of arrays allocated on the heap just use:

Stock** StockArrayArray = new Stock*[n]; // where n is number of arrays to create
for( int  i = 0; i < n; ++i )
{
    StockArrayArray[i] = new Stock[25];
}

// for freeing
for( int i = 0; i < n; ++i )
{
    delete[] StockArrayArray[i];
}
delete[] StockArrayArray;
Sign up to request clarification or add additional context in comments.

9 Comments

just arrays. just want to use arrays.
@Lance : Just because a[i] is equivalent to *(a+i) does not mean that these two data types are closely related, nor that they can be referred to as "syntactic sugar". Please observe the C FAQ on arrays and pointers : c-faq.com/aryptr, particularly c-faq.com/aryptr/practdiff.html
@DanielKamilKozar it is the requirement in my program. that is why. hope u understand..
@lakesh : Then I can only feel sad for your assignment and your teacher.
@lakesh I believe you misinterpreted your assignment. An array allocated with new is already stored on the heap, and its size can be adjusted at runtime( as opposed to statically declared arrays, which have a compile-time fixed size, and are stored on the stack ).
|
8

The type of a variable to a dynamic array is a pointer to the first object of the array. You want an array of dynamically allocated Stock objects, so an array of pointers to Stock, so your variable is a pointer to a pointer to Stock:

int n = 4; // dynamic size of the array;
Stock** stockArray = new Stock*[n];
for (int i = 0; i != n; ++i)
{
    stockArray[i] = new Stock();
}

and freeing it:

for (int i = 0; i != n; ++i)
{
    delete stockArray[i];
}
delete[] stockArray;

Comments

4

I did something which worked perfectly:

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std; 

class student {
    string name;
    int age;
    int roll;
    public:
        student() {
            name="";
            age=0;
            roll=0;
        }
        student (string n, int a, int r) {
            name=n;
            age=a;
            roll=r;
        }
        void show_details (); 
};
void student::show_details() {
    cout << "Name: " << name << "\n";
    cout << "Age: " << age << "\n";
    cout << "Roll No: " << roll << "\n";
}
int main() {
    string a; int b, c, n;
    cin >> n;
    student **obj;
    obj=(student**)malloc(n*sizeof(student*));
    for (int i=0; i<n; i++) {
        cin >> a;
        cin >> b;
        cin >> c;
        obj[i]=new student(a,b,c);
    }
    for (int i=0; i<n; i++) {
        obj[i]->show_details();
    }
    for (int i=0; i<n; i++) free (obj[i]);
    free (obj);
}

Yes... I used pointer to pointer for the array part, and it worked perfectly for variable sized arrays.

1 Comment

Great, so you are assigning a new class object to every ith location in the pointer to pointer array called obj.
3
Stock* stockArrayPointer = new Stock [4];

works only if the Stock class has a zero argument constructor if it does not have any zero argument constructor you cannot create an array of dynamic objects dynamically

you can as said create a array of dynamic object with a static array like

Stock stockArrayPointer[4]={Stock(args),Stock (args)};

but the syntax

Stock* stockArrayPointer=new Stock[4]{Stock(args),Stock (args)}; does not hold

or as said
use vectors...
vectors are memory allocated on heap
so the vector is a dynamic allocation

vector<Stock> V;
V.push_back(Stock(args));

or

V.push_back(new Stock(args));

The reason why

Stock* stockArrayPointer=new Stock[4]{Stock(args),Stock (args)};

does not hold is because this means you are using the new operator incorrectly

Comments

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.