global variables

Started by
13 comments, last by Antheus 15 years, 8 months ago
Actually I load the bulk of the file info into a dynamic 2D array. The maps vary in size so when a new map loads I deallocate the array, read the dimensions of the new map, and then load info about every block. I only use the file after that point to grab extra stuff like the name of a side link or a cave link or something. Is there any real reason to load that stuff into memory too? What's wrong with just reading the link info from the file?

Actually I used to read EVERYTHING straight from the file, it was awesome. I had like 100 bad guys on the map at once and every one of them would seekg the file to see if they could walk left or up or down or wherever. It actually worked plenty fast enough for the type of game I'm making. But these days I load that stuff into memory.
Advertisement
Quote:Original post by icecubeflower
Actually I load the bulk of the file info into a dynamic 2D array. The maps vary in size so when a new map loads I deallocate the array, read the dimensions of the new map, and then load info about every block. I only use the file after that point to grab extra stuff like the name of a side link or a cave link or something. Is there any real reason to load that stuff into memory too? What's wrong with just reading the link info from the file?

Actually I used to read EVERYTHING straight from the file, it was awesome. I had like 100 bad guys on the map at once and every one of them would seekg the file to see if they could walk left or up or down or wherever. It actually worked plenty fast enough for the type of game I'm making. But these days I load that stuff into memory.


Well, reading/writing to/from a harddrive is SLOW, if the file isn't too large it's not too bad to load it into memory. Memory is relatively quick (CPU memory / registers are even faster, but thats not relevant now).
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
I'm not so sure reading from a file is necessarily reading from the hard drive. I realize the way I used to rely completely on seekg was the dumb way to do it, I'm not arguing about that. But after I loaded all the info into memory and read it from a 2D array I didn't notice any speed difference.

Those files were only about 40K, I think fstream must keep a big chunk of the file in a buffer or something because it worked plenty fast. I'm not exactly programming Quake 5 here, I could probably get away with never loading anything into memory and it would work just fine. I'm just trying to get rid of my bad habits.

I loaded the map info into memory because that stuff is accessed in all the loops over and over and over. The link information is accessed only when you reach the end of a map or go into a house or something. It just reads the file to grab the name of the next file to open. Having that in memory instead of in a file wouldn't make any difference at all. Unless it loaded a new map a fraction of a nanosecond sooner.
Something like might help you (taking no position in what I personally think you should program like (i.e not using globals, using globals etc...))

I know some of these have been posted but I'll add them anyway (see similar examples above in another post)

struct myStruct
{
myType aName,
anotherType aName,
.
.
.
};

returnType funcName(const struct * myStruct)
{
static myType localPersistentData;
myType localInpersistentData;

...Doing my Jazz...

return returnType;
}

Note that I have not cleared this as compilable or fully correct, there are things you need to do, however this should give you some idea on how to do it.

The notion here is that it is better to use structs as they can alter without breaking code for other functions should you add something later (unless you remove something of course). Also if you use local data you can do it in two ways, persistent or non-persistent. That way you can "store" data in your function but then you also need to be aware that the data *might* be different next time you enter the function. And by using "normal" non-persistent data you can be assured that the data will be the same each time you enter the function and also have the relevant values always within the function.

Note that you should name such values (that stay the same) to a name like *myAmmo* = 5 or something like that. Then you can stay away from "magic numbers" and not having to guess what they mean (the numbers that is).

But as noted in other posts, I would strongly suggest you pass structs and make them *const* (if at all possible) to help you stabilize the code.

Just keep in mind that it is most important that you code in a style and way that you are most comfortable with. Change is always good but do it in your own pace and time. Don't change because others tell you to. You need to understand why first, and then when you do only if you agree with them.
No no no no! :)
Quote:Original post by icecubeflower

Those files were only about 40K, I think fstream must keep a big chunk of the file in a buffer or something because it worked plenty fast.


The OS is primary cache. Even if you don't do anything in your application, most of disk access will be cached.

There's a performance counter Memory/Cache Bytes, which shows you current size of cache. It will be in tens of megabytes.

40kb is really small in this respect.

This topic is closed to new replies.

Advertisement