Advertisement

Variable organisation (structs no good?)

Started by November 21, 2000 04:33 PM
16 comments, last by paulcoz 24 years, 1 month ago
Can someone suggest to me a good way of organising variables so that they are in a logical grouping? If I do it like this: typedef struct { int size; int colour; int width; int length; } gridprofile; gridprofile grid; I can then refer to gridsize and the other variables by saying: grid.size = 1; OR grid.length = 20; and I don't have to use long variable names that take ages to type (and these struct ones are easy to remember). But, the problem with this is that I will only ever have once instance of the grid so I really shouldn't be using a struct (or class) to begin with, and also that if I include other structures within the top one, and other structures within those (about 8 levels down), and then start having huge arrays of some of these structures, I start to use too much memory (I got some image file size too big message during compile - it said I needed a 750289374MB image ). Any other clever ways of putting variables in a logical order, or should I just go back to this? int gridlength = 1; int gridwidth = 20; Thanks in advance, Paulcoz. Edited by - paulcoz on 11/22/00 1:33:17 AM
Why should you not be using a class or struct to begin with if you are only going to have one instance of it? What is the reasoning behind that? I quite often make use of singleton classes to keep related items logically grouped together.

Your comment about including other stuctures inside is interesting. Why include the other structures if you are not going to require them? If you require them, then what is the problem? It is not clear as to what the actual problem is.

There is no penalty for using a single struct or class.


Steve ''Sly'' Williams
Tools Developer
Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Advertisement
Sly,

I think structures are a great way of grouping variables together - that's why I have written my program the way I have.

However, I am getting this weird image file size error on compile, and I am guessing that the structures are causing this because I didn't have nearly as many of them in the last revision of my program which worked fine (the structures are the main thing I have modified), and I really do have a ridiculous number of them now (about 30) with lots of them imbedded within the others and using arrays like so:

map.objs.obj[0].polys.poly[0].vts.vtx[0].x

This one's about eight levels down, and is just ONE example.

Does this imbedded structure look really memory-intensive (keeping in mind that there will be hundreds of objects, and hundreds of polys, 100x100 = 10000 record array, and most likely hundreds of vertices as well) plus all of the other structures I am storing, or could the compile error be caused by something completely un-related to the structures?

Paulcoz.

Edited by - paulcoz on November 22, 2000 1:51:41 AM
I''m not sure what a image file size error is, so I cannot pinpoint what might be the cause of it. It looks like your structures are statically allocated, eg:

Map map;

Have you tried dynamically allocating the structure?

Map *map = new Map;



Steve ''Sly'' Williams
Tools Developer
Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
I haven't tried dynamically allocating the structure. What would that do?

Unless somebody can tell me with some certainty that my imbedded structures are the source of the problem I am inclined to change the subject of this post:

warning LNK4084: total image size -1722265600 exceeds max (268435456); image may not run

The VC6++ compiler help says that the error means that my program exceeds the "256MB size limit for applications" - god knows why its 1.7GB in size??? If I try to run the program it crashes with a "close other applications and try again" type message of course.

I don't really know what is causing this - I've tried decreasing the size of the arrays I have, right down to 10, but the image size doesn't decrease at all. I've tried deleting the project and adding my .cpp files again. I've checked for duplicate includes / unnecessary files in headers. I've checked for typos, right down to the last semi-colon / bracket.

It's o.k. to redefine a structure in another file isn't it? - I do that once or twice, otherwise I get those 'undeclared' error messages in the files which can't see the first definition of the structure.

As soon as I go back to the previous revision of my program, that version works fine, and yet the only thing I've done is add more structures, and move some of my procedures to other files (I have 5 .cpp files and 4 .h files now).

Any ideas?
Paulcoz.

Edited by - paulcoz on November 22, 2000 5:51:41 PM
Any data declared statically in an application forms part of the final executable. You are declaring your map structure statically, therefore the compiler is including enough space in the image to hold that data.

If you dynamically allocate the memory required for map at run-time, then it is not included as part of the image.

That''s how I believe it happens anyway.


Steve ''Sly'' Williams
Tools Developer
Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Advertisement
That sounds a good explanation to me. At least it would explain why the image size during compile is so big.

I'll have to go through my program and work out which variables should be part of the .exe (probably most of them) and which should be allocated at run-time (the map).

Just to make sure I'm not coding a complete turkey with ridiculous system requirements - does 1.7GB sound like a normal amount of memory to use for this sort of application. I expect that once the memory used to hold my maps is allocated, a fair chunk of my 128MB of RAM will be used, and my program will try and make some 1.5GB swap file (which will make it access the hard drive all the time and be really slow and crappy, as well as forcing the user to have 1.5GB free disk space). Is this likely to happen?

Paulcoz.

Edited by - paulcoz on November 22, 2000 7:20:56 PM
You haven''t really told us what type of program you''re doing but...
Unless you''re doing some enormous scientific application, that''s a downright absurd amount of memory to need during runtime. You''re implying that the runtime requirements for your program would take up 3 cds worth of information. First, I would be looking at how much of that truly needs to be stored all the time, how much of it should be calculated on the fly (which is still a lot faster than disk swapping), and how much is really just unnecesary detail.
If you''re really trying to store some *enormous* map or some such, with millions of polygons, perhaps you should be breaking it down into chunks instead and loading more bits of it dynamically. This way your program will not require nearly as much memory and performance will be more reasonable. Do you really need to work with the entire thing at one time? I would imagine something like 256 mb overall is more reasonable, but even that assumes most of it is only loaded ahead of time and only used infrequently. Hard drive swapping is soooo much slower than ram, avoid it as much as possible.
If you''ve got your map structure arranged like this
struct {  struct {    struct {      struct {        struct {          struct {            struct {              float x, y, z;            } vtx[1000];          } vts;        } poly[1000];      } polys;    } obj[1000];  } objs;} map; 

(or a similar notation using typedef with mapprofile, objsprofile, etc.), the compile will reserve
1000 objects, each containing 1000 polys, each containing 1000 vertexes, each containing 3 floats (4 bytes), that is already 12.000.000.000 bytes of memory you''re using up, even dynamically allocated, I don''t think you should make your game require 12 GB RAM so early, maybe in some years, but not now.

You could write it like this:
// MG = My Game ;-)typedef struct MGObject {  MGModel  *Model;      // Model contaning polys  MGXForm  *Position;   // In-world position  MGMap    *Map;        // Owner};typedef struct MGMap {  MGObject *Objects;  int       NumObjects;} MGMap;MGMap *MGMap_Create(void) {  MGMap *newmap;  newmap = malloc(sizeof(MGMap));  newmap->Objects = NULL;  newmap->NumObjects = 0;  return newmap;}void MGMap_AddObject(MGMap *map, MGObject *object) {  assert(map);  assert(object);  map->Objects = realloc(map->Objects, sizeof(*map->Objects) * map->NumObjects + 1);  map->Objects[NumObjects] = object;  map->NumObjects++;  return;} 


Just take this as an idea on how it is done, I haven''t written this code as example but as a tech demonstration.
Your method would permanently allocate (ant throw-away) as much memory as may ever be needed if you had 1000 objects (you''re perhaps going to have more), each of them would have the full 1000 polys etc.

Seems like you wrote some simple programs and tried jumpstart into a full game without having the needed knowledge.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Allocate-on-demand is the best system to use. Then the structures only use as much memory as is required at that time to hold to data.


Steve ''Sly'' Williams
Tools Developer
Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

This topic is closed to new replies.

Advertisement