dynamic memory allocation
-
If I have a class called MyClass and three variables (var1, var2, var3), how do a create a multidimensional array of MyClass with dimensions the size of the variables. Here is an example:
class MyClass { int blablabla; //some stuff }; int main() { int i = 2; int var1 = ++i; // 2 int var2 = ++i; // 3 int var3 = ++i; // 4 MyClass ***table; table = new table[var1][var2][var3]; //I get an error here //VS says only the first dimension can be non-constant ... }
I've gotten so used to C# that I forgot how to do easy things like this.:eek: I would greatly appreciate any help. Thanks, Steve -
If I have a class called MyClass and three variables (var1, var2, var3), how do a create a multidimensional array of MyClass with dimensions the size of the variables. Here is an example:
class MyClass { int blablabla; //some stuff }; int main() { int i = 2; int var1 = ++i; // 2 int var2 = ++i; // 3 int var3 = ++i; // 4 MyClass ***table; table = new table[var1][var2][var3]; //I get an error here //VS says only the first dimension can be non-constant ... }
I've gotten so used to C# that I forgot how to do easy things like this.:eek: I would greatly appreciate any help. Thanks, SteveUnfortunately allocating multidimensional arrays in C++ is not that easy as in your example ;) See these articles: http://www.geocities.com/SPUR4444/prog/multidimensional.html[^] http://cpptips.hyperformix.com/cpptips/alloc_multi_dim[^] SHaroz wrote: int i = 2; int var1 = ++i; // 2 int var2 = ++i; // 3 int var3 = ++i; // 4 MyClass ***table; The comments are wrong. You are using ++ before the variable what means that the variable is incremented before it is used. So these comments would be correct:
int i = 2;
int var1 = ++i; // 3
int var2 = ++i; // 4
int var3 = ++i; // 5
MyClass ***table;:-D -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) -
Unfortunately allocating multidimensional arrays in C++ is not that easy as in your example ;) See these articles: http://www.geocities.com/SPUR4444/prog/multidimensional.html[^] http://cpptips.hyperformix.com/cpptips/alloc_multi_dim[^] SHaroz wrote: int i = 2; int var1 = ++i; // 2 int var2 = ++i; // 3 int var3 = ++i; // 4 MyClass ***table; The comments are wrong. You are using ++ before the variable what means that the variable is incremented before it is used. So these comments would be correct:
int i = 2;
int var1 = ++i; // 3
int var2 = ++i; // 4
int var3 = ++i; // 5
MyClass ***table;:-D -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;)