anyone know how to dinamically allocate an array in VC++?
I need to dinamically allocate an two dimensional array in VC++, but i have no idea how to do it.
Anyone can help, please ?
As far as I know, that is impossible. One workaround is the following:
char *DynamicArray = NULL;
void main()
{
DynamicArray = new char[x * y];
DynamicArray[x][y] = ''!'';
}
You can do something like that. It will not actually be a 2d array, but you can access it as one. x and y are not variables, just placeholder data to show you the example
--------------------
You are not a real programmer until you end all your sentences with semicolons;
char *DynamicArray = NULL;
void main()
{
DynamicArray = new char[x * y];
DynamicArray[x][y] = ''!'';
}
You can do something like that. It will not actually be a 2d array, but you can access it as one. x and y are not variables, just placeholder data to show you the example
--------------------
You are not a real programmer until you end all your sentences with semicolons;
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
That won't work. I don't even think that will compile. Since this question keeps coming up, please go read:
http://www.cerfnet.com/~mpcline/c++-faq-lite/freestore-mgmt.html#[16.15]
And read the follow up question if you so desire.
-Brian
Edited by - osmanb on 3/29/00 11:38:04 AM
http://www.cerfnet.com/~mpcline/c++-faq-lite/freestore-mgmt.html#[16.15]
And read the follow up question if you so desire.
-Brian
Edited by - osmanb on 3/29/00 11:38:04 AM
I also should have mentioned that I'd rather just use a vector of vectors, which makes them resizable:
-Brian
Edited by - osmanb on 3/29/00 11:37:00 AM
#include <vector>using namespace std;int main(){ vector<double> TempRow(100); // One row w/100 things vector<vector<double> > Matrix(100,TempRow); // 100 copies of TempRow... now a 100x100 matrix ...}
-Brian
Edited by - osmanb on 3/29/00 11:37:00 AM
One way to do that is as follows:
char** 2D_Array;
...
for (int i = 0; i < 10; i++)
2D_Array = new char[10];
something like that should work just fine.. haven''t tested that, and haven''t programmed in C/C++ in a few months so I don''t remember exactly if that would compile. (I think it will)
..-=ViKtOr=-..
char** 2D_Array;
...
for (int i = 0; i < 10; i++)
2D_Array = new char[10];
something like that should work just fine.. haven''t tested that, and haven''t programmed in C/C++ in a few months so I don''t remember exactly if that would compile. (I think it will)
..-=ViKtOr=-..
oppps..
2D_Array = new char[10];
should read
2D_Array = new char[10];
basically the code allocates 10x10 array of chars..
good luck!
..-=ViKtOr=-..
2D_Array = new char[10];
should read
2D_Array = new char[10];
basically the code allocates 10x10 array of chars..
good luck!
..-=ViKtOr=-..
god dammit... i thought i forgot to type it but it seems like the message board removes the brackets
what you need is this... 2D_Array
<br><br>hope this shows up..<br><br>you need rectangular brackets (symbolizing an array) and an "i" between the brackets.. you place that right after 2D_Array on the second line of code.. i hope you understrand what i''ve tried to show you..<br><br>l8r<br><br>..-=ViKtOr=-.. </i>
what you need is this... 2D_Array
<br><br>hope this shows up..<br><br>you need rectangular brackets (symbolizing an array) and an "i" between the brackets.. you place that right after 2D_Array on the second line of code.. i hope you understrand what i''ve tried to show you..<br><br>l8r<br><br>..-=ViKtOr=-.. </i>
Gladiator:
You still need to allocate the base indirection layer.
typedef char * pchar;
char ** CharArray = new pchar[X_DIM];
for (int j = 0; j < X_DIM; j++) {
CharArray[j] = new char[Y_DIM];
}
To deallocate:
for (int j = 0; j < X_DIM; j++) {
delete [] (CharArray[j]);
}
delete [] CharArray;
You still need to allocate the base indirection layer.
typedef char * pchar;
char ** CharArray = new pchar[X_DIM];
for (int j = 0; j < X_DIM; j++) {
CharArray[j] = new char[Y_DIM];
}
To deallocate:
for (int j = 0; j < X_DIM; j++) {
delete [] (CharArray[j]);
}
delete [] CharArray;
criminy, why all the crazy code. All you need to do is allocate a single dimension array of the size you need for a double dimension array and then index the array using offsets.
I.E.
char* array = new char[x*y];
array[x+y*width] = ''new value here'';
all you actually have to know ahead of time is the width, which can be calculated when you create the array.
The first x is the width, and the y the height.
The second x is the column you want, and the y the row.
Bworks.Com
I.E.
char* array = new char[x*y];
array[x+y*width] = ''new value here'';
all you actually have to know ahead of time is the width, which can be calculated when you create the array.
The first x is the width, and the y the height.
The second x is the column you want, and the y the row.
Bworks.Com
quote: criminy, why all the crazy code.
Many library routines require that matrices be given to them in standard double-derefernced two-d arrays rather than one-d arrays that use funky indexing. It also aids clarity of code. e.g. array[3][4] makes a lot more sense and looks cleaner than array[3 * 7 + 4].
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement