Click to See Complete Forum and Search --> : Dinamic array of structures


jangel
April 10th, 2002, 05:48 PM
Hi, i would like to know if there is a way I can build a dinamic array of structures.
I'v got this:

struct m_Data{
ULONG m_Size;
ULONG m_Address;
ULONG m_RawData;
ULONG m_PointerToRawData;
};

is there a way I can build an array from this struct??

I need to do something like this

m_Data *m_DinamicData[] = new m_Data[y];

where y is a value i get from another function.

The compiler expect for a constant value instead of y, and gives me an error


Tnx

NMTop40
April 10th, 2002, 06:56 PM
you must omit the [] symbols.

and you don't want to do that anyway because you have std::vector


The best things come to those who rate

Gvido
April 10th, 2002, 06:58 PM
m_Data *m_DinamicData = new m_Data[y];

jangel
April 10th, 2002, 07:09 PM
nop it doesn't compile, y must be a constant value
eg: 1,2,3 etc...

Gvido
April 10th, 2002, 08:23 PM
Are you sure you use C++ compiler?

this code:

some_type *p = new some_type[some_var];




must work - it is standard
it's same as

some_type *p = (some_type*) malloc(some_var * sizeof(some_type)); // this is C style allocation

Paul McKenzie
April 10th, 2002, 09:26 PM
#include <vector>
//...
struct m_Data{
ULONG m_Size;
ULONG m_Address;
ULONG m_RawData;
ULONG m_PointerToRawData;
};
//...
// Create a dynamic array using std::vector
std::vector<m_Data> m_DynamicData(y);
//..
m_DynamicData[0].m_Size = whatever;
//...



No new, no delete, no problems.

Regards,

Paul McKenzie

jangel
April 11th, 2002, 12:56 PM
It worked! Tnx a lot