OK, I now have two applications that MUST dynamically allocate a 2D array. Each is a string array. I can easily declare a huge one and wast ram, but that doens't work too well on low-end machines. Using "new" I have found to possibly be in-capable of creating such a simple array. Here's an example:
[code]
char blah[8][32]; //Wasteful
char *blah2;
blah2 = new char[itemcount][32]; //ERROR
[/code]
Now how the !)(%&#
@#_)( am I supposed to dynamically do this? I got a reply to this mater one time that stated something about making an alias or algorithm to make a normal string long enough for all the data and then parsing it, but my engine [b]***NEEDS***[/b] a 2D array. Am I trying to do something that is impossible to do without re-writing the Windows/DOS/Unix platforms?
-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Comments
Is the second number always 32 (e.g. char *buffer[x][32])? In your example blah2 should be a char **, not a char *. You can allocate it like this:
int eight = 8; //does not need to be constant
char **blah = (char **) new char[eight][32];
This ONLY works if the second number (32) is a constant.
You can then access the array elements as you expect.
There is no "flaw" in C++.
: OK, I now have two applications that MUST dynamically allocate a 2D array. Each is a string array. I can easily declare a huge one and wast ram, but that doens't work too well on low-end machines. Using "new" I have found to possibly be in-capable of creating such a simple array. Here's an example:
: [code]
: char blah[8][32]; //Wasteful
: char *blah2;
:
: blah2 = new char[itemcount][32]; //ERROR
: [/code]
: Now how the !)(%&#@#_)( am I supposed to dynamically do this? I got a reply to this mater one time that stated something about making an alias or algorithm to make a normal string long enough for all the data and then parsing it, but my engine [b]***NEEDS***[/b] a 2D array. Am I trying to do something that is impossible to do without re-writing the Windows/DOS/Unix platforms?
:
: -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
:
: Is the second number always 32 (e.g. char *buffer[x][32])? In your example blah2 should be a char **, not a char *. You can allocate it like this:
: int eight = 8; //does not need to be constant
: char **blah = (char **) new char[eight][32];
: This ONLY works if the second number (32) is a constant.
: You can then access the array elements as you expect.
:
: There is no "flaw" in C++.
Thanks a MILLION man. I'll put you in the credits on the app I am on right now (Infiltration Loadout Editor). I have been trying to figure this out for over two months. I didn't know that there was a pointer that used two asteriks. I'd have thought that was a pointer to a pointer or something. Now if I have a variable size array (both digits need to be assigned random sizes), would I use this the same way or some other way? I don't need to, but I am curious.
-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
[code]
void ParseConfig()
{
char temp[512];
unsigned char Position;
short int Loop;
ItemCount = 0;
for(Loop = 0; Loop < strlen(LoadoutString); Loop++)
{
if(LoadoutString[Loop] == ',')
ItemCount++;
}
Items = (char**)new char[ItemCount][64];
Loop = 0;
while(LoadoutString[Loop - 1] != '/' && LoadoutString[Loop] != '/')
{
LoadoutName[Loop] = LoadoutString[Loop];
Loop++;
}
//It messes up somewhere in here...
for(unsigned char Parse = 0; Parse < ItemCount - 1; Parse++)
{
Loop++;
Position = 0;
while(LoadoutString[Loop] != ',')
{
Items[Parse][Position] = LoadoutString[Loop];
Loop++;
Position++;
}
}
//Somewhere above here...
delete [] Items;
MessageBox(NULL, temp, "Debug", MB_OK);
return;
}
[/code]
The file I read into "LoadoutString" is in this format:
[code]
Loadout_Name//Gun-1,#Gun-1Addon,*Gun-1Ammo,*Gun-2,#Gun-2Addon,*Gun-2Ammo,
[/code]
I need to first parse out the name, which is working fine. Then I need to load each gun and ammo into a section of "Items[y][x]". I can NOT figure out why this thing is page faulting becuase I have allocated a TON of string space for LoadoutString to be parsed into. Can you see anything??
-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Anyways, while I agree that C/C++ have major flaws, dynmaic 2d arrays is not one of them. Check this one out:
http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=130831&Setting=A9996F0004
I replied with the classic solution 1, null and void came up with classic solution 2.
: Ugh, found the error. It's the Items array. I used the method I was shown, yet if you try writing to that buffer, it faults and exits. I am begining to think that you can NOT dynamically create a 2D array with C/C++. I have no problem doing it in QuickBasic, but I've now tried four methods of doing it in C/C++ and each one fails or won't even compile. IS there any way I can do this?
:
: -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
:
: