0

I am trying to dynamically allocate storage for an array of structures, in the constructor. I am new to C++, and have tried all sorts of syntax variations, but now I am wondering whether this can be done at all.

struct Trade
{
    int index;
}

define MAX_TRADES 5000
struct foo
{
    Trade *trade [MAX_TRADES];

    int cumeTradeCount;

    foo() :
        cumeTradeCount(0),
    {
        // here is where I want to allocate storage for cumeTradeCount Trade structures
        ....

        memset(trade, 0, cumeTradeCount   * sizeof(Trade*));
    }
}

Specifically, what I am trying to figure out is how I can allocate storage for 'cumeTradeCount' structures, in the constructor. If I were doing this in C, I would do the following:

for (int i = 0; i < cumeTradeCount; ++i)
    trade[i] = calloc(1, sizeof(Trade *));
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
  • `memset` does allocate memory - it sets to a particular value. Since you're initializing `cumeTradeCount=0`, the `memset` also won't actually do anything. Plus, the trailing comma will make this code not compile. – MuertoExcobito Feb 29 '16 at 03:40

2 Answers2

1

You need to read a good C++ book.. Your code allocates 5000 pointers on the stack.

To allocate 5000 Trade objects on stack, just use Trade trade[MAX_TRADES]... Example:

struct Trade
{
    int index;
};

#define MAX_TRADES 5000
struct foo
{
    Trade trade[MAX_TRADES];

    int cumeTradeCount;

    foo() : 
        cumeTradeCount(0)
    {
        // allocate storage for cumeTradeCount Trade structures
        //memset(trade, 0, cumeTradeCount   * sizeof(Trade*));
        // You don't need it
    }
};

As for heap, You use the operator new to allocate on the heap.

Change this: Trade *trade [MAX_TRADES]; to Trade *trade = new Trade[MAX_TRADES];

Since its a class member, below is how it's done. But don't forget to delete it in your destructor... A full example below...

struct Trade
{
    int index;
};

#define MAX_TRADES 5000
struct foo
{
    Trade *trade;

    int cumeTradeCount;

    foo() : trade(new Trade[MAX_TRADES]),
        cumeTradeCount(0)
    {
        // allocate storage for cumeTradeCount Trade structures
        //memset(trade, 0, cumeTradeCount   * sizeof(Trade*));
        // You don't need it
    }

    ~foo() { delete[] trade; }
};

. I strongly advice you to use an std::array or std::vector instead of raw arrays. And again, in C++, we prefer to use const and constexpr to #defines --> still, You need to read a good C++ book.

Below, will save you many unforeseen headaches.

#include <vector>
struct Trade
{
    int index;
};

#define MAX_TRADES 5000
struct foo
{
    std::vector<Trade> trade;
    int cumeTradeCount;

    foo() : trade(MAX_TRADES),
        cumeTradeCount(0)
    { }
};
Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
0
Trade* trade [MAX_TRADES];

is not a dynamic allocation. It is a fixed size array of 5000 pointers to Trade.

If this is what you want, you can just set them all to NULL or better nullptr and then fill the ones you need like this:

trade[i] = new Trade();

Don't forget to use delete in your destructor and implement a proper assignment, copy-constructors and copy-assignments.

If you want a dynamic array, use something like this:

Trade* trade[];
//in constructor
trade = new Trade*[sizeOfArray]; //remember those pointers are still uninitialized

or if you dont need pointers:

Trade trade[];
//in constructor
trade = new Trade[sizeOfArray];

Since you are asking about C++, it would be way easier to use

  • std::array<Trade*,NUM_TRADES> (for fixed number of pointers),
  • std::array<Trade,NUM_TRADES> (for fixed number of Trades),
  • std::vector<Trade*> (for variable number of pointers),
  • std::vector<Trade> (for variable number of Trades),
Anedar
  • 4,235
  • 1
  • 23
  • 41