Declaring dynamic arrays inside structures

Started by
10 comments, last by Aardvajk 17 years, 6 months ago
Please, what is the correct way to declare a dynamic array inside a structure (in true, inside an array of structures)? I was thinking in something like this:

struct MY_STRUCT
{
   ...
   DWORD *array;
   ...
};

MY_STRUCT *var = new MY_STRUCT[N];
var->array = new DWORD[M];
Thanks in advance
Advertisement
Quote:Original post by Mari_p

struct MY_STRUCT
{
...
DWORD *array;
...
};

MY_STRUCT *var = new MY_STRUCT[N];
var->array = new DWORD[M];
That's correct (except that there seems to be some confusion as to whether or not var is supposed to be an array).

However, unless you're restricted to C for some reason, you'd be better off using std::vector.
Quote:Original post by Mari_p
Please, what is the correct way to declare a dynamic array inside a structure (in true, inside an array of structures)?
I was thinking in something like this:

struct MY_STRUCT{   ...   DWORD *array;   ...};MY_STRUCT *var = new MY_STRUCT[N];var->array = new DWORD[M];


Thanks in advance


#include <vector>struct MY_STRUCT {    ...    std::vector< DWORD > array;    ...};std::vector< MY_STRUCT > var( N );var[0].array.resize( M );


Unlike your posted example, this will not leak :).

@jyk: new isn't legal C :).
Quote:Original post by MaulingMonkey
@jyk: new isn't legal C :).
Hehe, well I do know that :-) But obviously somewhere between reading the original post and writing my reply I forgot that the OP had used new [Edit: as well as C++-style struct syntax].

How embarrassing :-|
Quote:Original post by jyk
Quote:Original post by MaulingMonkey
@jyk: new isn't legal C :).
Hehe, well I do know that :-) But obviously somewhere between reading the original post and writing my reply I forgot that the OP had used new. How embarrassing :-|


Hardly embarrassing, the code originally posted stinks badly of C. Non-RAIIed pointers, non-containered arrays, getting within 3 miles of the Win32 API (DWORD), etc. - I just hawk around for the most subtle indicators that allow me to assert that since C++ is being used, you should/must/will use the standard library instead of pretending it's C :P.

(EDIT to reply to EDIT: "struct ____ { ... };" is legal C, you'll just need to use "struct _____" every time you want to use it. Typedefs just eliminate the extranious keyword, although I guess this is another point against the legality of the MY_STRUCT *var = ...; line as C code :P)
Quote:Original post by MaulingMonkey
(EDIT to reply to EDIT: "struct ____ { ... };" is legal C, you'll just need to use "struct _____" every time you want to use it. Typedefs just eliminate the extranious keyword, although I guess this is another point against the legality of the MY_STRUCT *var = ...; line as C code :P)
Yes, my 'edit' comment was simply in reference to the fact that in the given context, 'MY_STRUCT *var = ...;' is not legal C.
Thank you much, guys.

I implemented dynamic arrays using the two ways (new and vector)... but I am getting an annoying CXX0030 error. :(

The relevant code is:
struct M_FILE_DATA_EX{	DWORD tempo;	std::vector<BYTE> roboID;		std::vector<FLOAT> x;		std::vector<FLOAT> y;		std::vector<FLOAT> theta;		std::vector<BOOL> carregado;	};std::vector<M_FILE_DATA_EX> ArrayDadosEx;DWORD nroLinhas = 10869;BYTE nroRobos  = 5;VOID AlimentaArrayDadosEx(){   ArrayDadosEx.resize(nroLinhas);   ArrayDadosEx[0].carregado.resize(nroRobos);   ArrayDadosEx[0].roboID.resize(nroRobos);   ArrayDadosEx[0].x.resize(nroRobos);   ArrayDadosEx[0].y.resize(nroRobos);   ArrayDadosEx[0].theta.resize(nroRobos);   ...   for ( DWORD i = 0 ; i < nroLinhas ; i++ )   {         ...         for ( BYTE rob = 0 ; rob < nroRobos ; rob++ )      {         if ( i > 0 )         {            ArrayDadosEx.roboID[rob]    = (ArrayDadosEx[i-1].roboID[rob]);            ArrayDadosEx.x[rob]         = ArrayDadosEx[i-1].x[rob];            ArrayDadosEx.y[rob]         = ArrayDadosEx[i-1].y[rob];            ArrayDadosEx.theta[rob]     = ArrayDadosEx[i-1].theta[rob];            ArrayDadosEx.carregado[rob] = ArrayDadosEx[i-1].carregado[rob];                  }      }         ...   }   ...}


I got the error when i = 4279. Please, would anybody have some idea on how to solve that?

Thanks again.
please use .at(int) instead of the [] operators for std::vectors. if you exceed the vector's limits, at() will throw at least an exception, so you know what the problem is.
------------------------------------------------------------Jawohl, Herr Oberst!
After a lot of attempts I decided to restart my machine and the code simply worked. But I'm now getting another error when I close the application. The error is:

DAMAGE: after Normal Block (#389) at 0x00B63A60

When I debug it, I notice the error is exclusively related to std::vector. Any ideas?
You are only resizing the vector members of ArrayDadosEx[0]. You are then attempting to access 0-5 of the vector members all the other elements of ArrayDadosEx.

You need to move your resize code into the loop and resize ArrayDadosEx.members for all of i.

VOID AlimentaArrayDadosEx(){   ArrayDadosEx.resize(nroLinhas);   ...   for ( DWORD i = 0 ; i < nroLinhas ; i++ )   {      ArrayDadosEx.carregado.resize(nroRobos);   ArrayDadosEx.roboID.resize(nroRobos);   ArrayDadosEx.x.resize(nroRobos);   ArrayDadosEx.y.resize(nroRobos);   ArrayDadosEx.theta.resize(nroRobos);      ...         for ( BYTE rob = 0 ; rob < nroRobos ; rob++ )      {         if ( i > 0 )         {            ArrayDadosEx.roboID[rob]    = (ArrayDadosEx[i-1].roboID[rob]);            ArrayDadosEx.x[rob]         = ArrayDadosEx[i-1].x[rob];            ArrayDadosEx.y[rob]         = ArrayDadosEx[i-1].y[rob];            ArrayDadosEx.theta[rob]     = ArrayDadosEx[i-1].theta[rob];            ArrayDadosEx.carregado[rob] = ArrayDadosEx[i-1].carregado[rob];                  }      }         ...   }


I'm not sure what your assignments in the inner loop are trying to do. I assume you are assigning values to ArrayDadosEx[0]'s members somewhere in your ...'s. Given that your inner loop then seems to propegate these values up through the entire array, I'd suspect it would be clearer to just assign to the whole array in a loop when you assign to [0], although I don't know what else is going in in the ...'s, so I may be off track.

You can also just assign one vector to another if they are the same type, so:

      for ( BYTE rob = 0 ; rob < nroRobos ; rob++ )      {            ...            ArrayDadosEx.roboID[rob]    = (ArrayDadosEx[i-1].roboID[rob]);            ...      }


can be expressed more easily as:

ArrayDadosEx.roboID=ArrayDadosEx[i-1].roboID;


But equally, since all the members of M_FILE_DATA_EX can be handled by default copying, you could just do:

ArrayDadosEx=ArrayDadosEx[i-1];


as far as I can see.

Or just set up the M_FILE_DATA_EX once, and do:

M_FILE_DATA_EX Ex;// set up ExArrayDadosEx.resize(nroLinhas,Ex);


unless I'm confused about what your code is trying to do. Apologies if so.

This topic is closed to new replies.

Advertisement