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;
Answer from akaltar on Stack OverflowIf 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;
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;
Dynamic Array of Objects - C++ Forum
A dynamic array objects in a class - C++ Forum
Declaration of a Dynamic Array of Objects
The best/easiest way to do generic dynamic arrays in C
Videos
The functionality you look for already exits in the stl collection classes and and with out knowing you application it would be had to tell you weather you needed a pointer or not.
The basic layout of you underlying container could be something like this.
class Album{
public:
void addPhoto(Photo p){
Photos.push_back(p);
}
private:
std::vector<Photo> Photos;
};
You should use an std::vector.
I recently came across libcello which enables alot of magical stuff ontop of C by utilizing what they call "Fat Pointers" which are essentially pointers with additional information concerning the data it points to, what caught my attention was when the author described an example of how to implement arrays using this approach, so I decided to try out a very simple example and its actually very elegant and easy
The way you do it is basically have a generic header structure with additional data you want to preserve
typedef struct ptr_header{
size_t len;
size_t cap;
}ptr_header;when you want to create a new dynamic array you just allocate extra memory for the header
int *arr = NULL; // allocate 10 elements + space for the header arr = malloc(sizeof(ptr_header) + sizeof(*ar) * 10); // skip the header and make arr point to the actual data arr = (void *)((char *)arr + sizeof(ptr_header)); // access the length by going back and cast as header ((ptr_header *)arr-1)->len = 0; // access the capacity the same way ((ptr_header *)arr-1)->cap = 10;
now you can get the length and capacity by very simple arithmetic and you can do your reallocations and all the nice stuff you want to do and get creative with some macro magic
The best thing about it is you dont have to create a struct for every dynamic array of the type and that subscripting works -which I didnt even think of when I implemented it- and everything works
here is a simple full example to get the point across
Given what you've described in the comments (wanting to create a 2-dimensional array), there is an alternative that avoids both explicitly using placement new to create objects into previously "raw" memory, and the terrible cache usage of an array of arrays model (implemented either as an array of pointers to separately allocate memory blocks, or a std::vector<std::vector<T>> sort of thing.
That alternative is to allocate a single block of memory for the entire rectangular array, and do a little bit of operator overloading to get 2-D addressing into that block.
A simple version of this looks something like this:
class array2D {
std::vector<double> data;
size_t columns;
public:
array2D(size_t x, size_t y) : columns(x), data(x*y) {}
double &operator(size_t x, size_t y) {
return data[y*columns+x];
}
};
This is "dynamic" only to the extent that it allows you to specify the size of the array when you create it, but (as it stands right now) you can't change the size after creation.
If you want to write your own imitation of std::vector as well, that's always entirely possible--but it's a little tricky in a few respects. I wrote a blog post a few years ago that covers at least a few of the basic points, and @Loki Astari wrote a rather more extensive series about it, complete with a Code Review of his implementation.
is there a nice and clean way that allows you to allocate uninitialised memory, and then fill it with custom-constructed objects, that does not degrade performance?
Use std::vector, reserve() and emplace_back().
Why reinvent the wheel if the standard library has solved your problem already!
However, the former [using vector] is not an option in my case,
Why is it not an option?
If some manager set some rule to prohibit vector, try to change that rule. Or find some open source implementation of std::vector and rename it to not_std::not_a_vector.
Because if you solve this problem properly, you will end up not only having reinvented std::vector, but you will have lost a lot of time as well.