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...
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.
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);
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.
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:
Open "\hello.MAP" for Binary as #FF
Get #FF, ,MapWidth
Get #FF, ,MapHeight
ReDim tiles(MapWidth, MapHeight)
Get #FF, ,tiles
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...
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?
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.
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
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];
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! :)
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?
Re: Arrays of structs with unspecified size
new and delete are c++,
malloc and free are c and c++.
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...
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.
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;
}
}
Re: Arrays of structs with unspecified size
y*width+x is the correct 2d addressing formula.
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 :D
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.