Results 1 to 18 of 18

Thread: Arrays of structs with unspecified size

  1. #1
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Arrays of structs with unspecified size

    Say I had a 2d map and I had a struct and and array of that type like so:

    Code:
    struct sMAP{
    	int tiletype;
    	bool walkable;
    };
    Now I want to put a 2 dimensional array in the program without specifying the size of it, as I will load the size into the array from a binary file... whenever I just leave the values blank, it seems to give me a compile error and when I just define it as an external struct in a seperate function, then it seems to complain about there not being any constant dimensions in it...

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 00
    Location
    Under my rock
    Posts
    2,336

    Re: Arrays of structs with unspecified size

    I dont think you can declare an array without also specifying it's size. Umm, well unless you use Vector I think, but I have never used Vector so I am not sure.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 02
    Posts
    1,037

    Re: Arrays of structs with unspecified size

    sMap sMapPtr=new sMap[runtime number does not have to be constant];
    or
    vector<sMap> v;
    v.pushback(sMap variable);

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594

    Re: Arrays of structs with unspecified size

    Two-dimensional and dynamic is hard. You have three options:
    Use one-dimensional and do manual address translation. Advantage: continuous storage. Disadvantage: Manual address translation, manual memory management.

    Use two nested vectors. Advantage: No address translation, no memory management. Disadvantage: Discontinuous storage.

    Use a wrapper class for option 1. Advantage: Continuous storage, no address translation, no memory management. Disadvantage: You have to write it or get an external libary, such as Boost.MultiArray.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    Code:
    sMap sMapPtr=new sMap[runtime number does not have to be constant];
    I get an error with this method:

    error C2440: 'initializing' : cannot convert from 'struct sMAP (*)[100]' to 'struct sMAP'
    No constructor could take the source type, or constructor overload resolution was ambiguous


    Is it really so hard to open a binary file, dimension an array of sMAP structs to the width and height variables in the file?

    in VB, the code would be like this:

    VB Code:
    1. Open "\hello.MAP" for Binary as #FF
    2.    
    3.     Get #FF, ,MapWidth
    4.     Get #FF, ,MapHeight
    5.  
    6.     ReDim tiles(MapWidth, MapHeight)
    7.  
    8.     Get #FF, ,tiles
    9. Close #FF

    I just wanted that code in C, and I've already found out the opening and the put and get syntax in C, but the redimensioning of such an array is a bit tricky for me...

  6. #6
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    I wonder what game developers did when they needed their map systems... just hardcoded them into the EXE?

  7. #7
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 02
    Posts
    1,037

    Re: Arrays of structs with unspecified size

    Sorry, my mistake, forgot the *.
    sMap* sMapPtr=new sMap[runtime number does not have to be constant];

    I don't know VB though so I can't help you converting snippet.

  8. #8
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    Code:
    	sMAP* sMapPtr = new sMAP[mapwidth][mapheight];
    my compiler still whines about how the array bound is not constant


    and I get a different error too:

    error C2440: 'initializing' : cannot convert from 'struct sMAP (*)[1]' to 'struct sMAP *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

  9. #9
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 02
    Posts
    1,037

    Re: Arrays of structs with unspecified size

    Don't try to make a 2 dimentional array. Treat it like a bitmap and do manual address translation like CB said.
    sMAP* sMapPtr = new sMAP[mapwidth*mapheight];

  10. #10
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    Hey, thanks!
    When CornedBee spoke of manual address translation, I didn't think that he meant that, so thanks to the both of you!

  11. #11
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    I find this quite strange... This method works very well when the compiler is set to C++, but when it is set to just C, it doesn't like what it sees... Is that because this is only a C++ method of doing that?

  12. #12
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 02
    Posts
    1,037

    Re: Arrays of structs with unspecified size

    new and delete are c++,
    malloc and free are c and c++.

  13. #13
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    I'm also having trouble with something else.. the scope of the pointer. Say I load the map, but in the same function I declare that pointer, as I get the mapwidth and mapheight variables from the file... I can't use that same pointer in a different function, like the Render function...

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594

    Re: Arrays of structs with unspecified size

    No. A pointer has the same scope as any other variable.

    The pointed-to object (pointee) is a different story, of course.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    Would you happen to know how I'd get the pointer to array of structs to be able to be used in another function as well, making it public, without having to pass it?

    Say I have a function... LoadMap... there I get the variables from the file and create the pointer...

    I need to use that pointer in the render function,and, throughout the entire program...

    If I declare that pointer outside of a function, then I can't dimension the array that it is pointing to to a specified length

    Another question:

    I'm using that manual addressing.. so map[mapwidth * mapheight].... If I were to loop through the X and Y tiles in the array, would I do a nested X loop in the Y loop and get the position with : map[x * y]?

    Code:
    for (int x = 0; x <= MapWidth; x++)
    {
    	for(int y = 0; y <= MapHeight; y++)
    	{
    		map[x * y].TileType = 2;
    	}
    }
    Is that code above the same as:

    Code:
    for (int x = 0; x <= MapWidth; x++)
    {
    	for(int y = 0; y <= MapHeight; y++)
    	{
    		map[x][y].TileType = 2;
    	}
    }

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594

    Re: Arrays of structs with unspecified size

    y*width+x is the correct 2d addressing formula.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Hyperactive Member
    Join Date
    Jan 03
    Location
    The Netherlands
    Posts
    386

    Re: Arrays of structs with unspecified size

    So if my tiles are 32x32 in an array of 100x100 tiles I'd do:

    Code:
    for (int x = 0; x < MapWidth; x++)
    {
    	for (int y = 0; y < MapHeight; y++)
    	{
    		map[y * 32 + x].TileType = 1;
    	}
    }
    That doesn't really seem to work?

    EDIT:

    Ohhh, now I see what you mean by 'width', thanks, it works
    Last edited by Venom555; May 6th, 2005 at 05:32 PM.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594

    Re: Arrays of structs with unspecified size

    No, you'd do
    map[y * MapWidth + x]

    And you should do the vertical loop as the outer one. This is the usual way to traverse 2d arrays: the way you read a book. Line by line, each line from left to right.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •