|
-
Apr 14th, 2005, 05:55 PM
#1
Thread Starter
Hyperactive Member
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...
-
Apr 14th, 2005, 06:22 PM
#2
PowerPoster
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.

-
Apr 14th, 2005, 08:59 PM
#3
Frenzied Member
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);
-
Apr 15th, 2005, 09:20 AM
#4
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.
-
Apr 19th, 2005, 05:28 PM
#5
Thread Starter
Hyperactive Member
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...
-
Apr 19th, 2005, 06:37 PM
#6
Thread Starter
Hyperactive Member
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?
-
Apr 19th, 2005, 06:59 PM
#7
Frenzied Member
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.
-
Apr 19th, 2005, 07:05 PM
#8
Thread Starter
Hyperactive Member
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
-
Apr 19th, 2005, 08:19 PM
#9
Frenzied Member
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];
-
Apr 20th, 2005, 06:47 AM
#10
Thread Starter
Hyperactive Member
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!
-
May 4th, 2005, 01:28 PM
#11
Thread Starter
Hyperactive Member
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?
-
May 4th, 2005, 01:59 PM
#12
Frenzied Member
Re: Arrays of structs with unspecified size
new and delete are c++,
malloc and free are c and c++.
-
May 4th, 2005, 06:53 PM
#13
Thread Starter
Hyperactive Member
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...
-
May 5th, 2005, 04:45 AM
#14
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.
-
May 5th, 2005, 08:47 AM
#15
Thread Starter
Hyperactive Member
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;
}
}
-
May 5th, 2005, 11:06 AM
#16
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.
-
May 6th, 2005, 05:23 PM
#17
Thread Starter
Hyperactive Member
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.
-
May 6th, 2005, 05:30 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|