Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation of Dynamic Array of Dynamic Objects in C++

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...

like image 955
lakshmen Avatar asked Nov 30 '13 18:11

lakshmen


People also ask

How do you create an array of objects dynamically in C++?

In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically.

What is dynamic creation of objects?

Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.

What is dynamic array How is it created?

A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation.


3 Answers

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;
like image 141
akaltar Avatar answered Oct 17 '22 02:10

akaltar


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;
like image 40
Frederic Lachasse Avatar answered Oct 17 '22 01:10

Frederic Lachasse


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

like image 3
mono Avatar answered Oct 17 '22 02:10

mono