Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C/C++ Major Flaw?

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
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

  • SAckSAck Member Posts: 3
    [b][red]This message was edited by SAck at 2002-9-16 12:54:35[/red][/b][hr]
    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]
    :



  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : [b][red]This message was edited by SAck at 2002-9-16 12:54:35[/red][/b][hr]
    : 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]

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    OK, although I can now properly allocate memory to a 2D array, I don't think it's working properly. It keeps causing page faults in itself.
    [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]

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    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]

  • pingpongpingpong Member Posts: 937
    Seph, what's up buddy? How about the Panthers?! 2-0 so far.. That's more wins than the whole of last season!

    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]
    :
    :

  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    _________ \ http://forcoder.org \ free ebooks and video tutorials about { Delphi Ruby Perl Objective-C JavaScript C# Visual Basic .NET C++ R C PL/SQL MATLAB PHP Java Swift Scratch Visual Basic Go Python Assembly Lua Julia Kotlin F# Bash Transact-SQL COBOL Hack Erlang Scheme SAS LabVIEW VBScript Awk Lisp Scala Rust D Dart Ada ML FoxPro Prolog Alice ABAP Fortran Crystal Apex Logo Clojure } _______

Sign In or Register to comment.