Confused with array-ness

Member
Posts: 20
Joined: 2007.02
Post: #1
For a level editor for my game, I save in a file the entities I have. I want to add all this data to an array, so I can use it.
My problem is that I need this array to be accessible from any method, so I need this to be global (create it outside a method), then set the size once I've loaded the file (so I know how much I need to store). But the former can only be done outside a method, whilst the latter must be done inside a method.
So any ideas how to do it? Dynamic array size or something?
I'm not sure how better to explain it, but if you don't understand what I said, I'll have another go at explaining it.

Tobs
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
If you're using C++ just use a std::vector< foo >. If you're using ObjC/Cocoa, use an NSMutableArray. And, hell, if you're using plain old C, just define your vector as a pointer of type <whatever> and keep another global variable which says how long that vector is.

Either way you go, you can resize the vector in your reading method.

That being said, the std::vector and NSMutableArray approaches both will let you add to the vector as you parse your file, which doesn't require you to know how long to make it in advance.

All that being said, I'd not use globals if I could avoid it. You almost certainly have some sort of 'app' instance. You can generally keep around one global which points to your app, and then let your app keep all the stuff you'd normally treat as global.
Quote this message in a reply
Realization
Unregistered
 
Post: #3
Once you create the array, are you going to change the size after that? If you aren't going to modify it's dimensions after allocating it the first time, then all you need to do is something like this:

Code:
int nData = NUMBER_OF_ENTRIES;
datatype *list = malloc(sizeof(datatype) * nData);

// accessing
int i;
for (i = 0; i < nData; i++)
  data[i] = 0;

// when you're done with the data
free(list);

...or if you're in C++, use this to allocate and free the data instead:
Code:
// allocating
datatype *list = new datatype[nData];
// freeing
delete [] list;

Where datatype is whatever kind of entities you are saving, such as int or double, and NUMBER_OF_ENTRIES is the size of the array. Look for a tutorial on memory allocation in the programming language you're using, it'll really come in handy.
Quote this message in a reply
Member
Posts: 20
Joined: 2007.02
Post: #4
Thanks guys, I shall try these and read up on memory allocation =]

Tobs
Quote this message in a reply
Member
Posts: 45
Joined: 2006.11
Post: #5
If you are using C++, there are very few reasons for using naked arrays for your own internal code (unless perhaps if you are using a library that requires them). Use std::vector for pretty much everything else. Here's a short example:

Code:
std::vector<int> v;
v.reserve(20); // ensures space for 20 things at least
for(int i = 0; i < 21; i++)
    v.push_back(i); // vector is {0, 1, 2, ..., 20}
// notice that there are currently 21 ints in the vector now.
// push_back reserves more space automatically
// For best performance, reserve as much space as
// you'll think you'll need
cout << "v.size() : " << v.size() << endl;
cout << "v.capacity() : " << v.capacity() << endl;

// alternatively if you knew you were going to add
// exactly 20 items to the array, you could do this:
v.resize(20);
for(int i = 0; i < 20; i++)
    v[i] = i;

cout << "v.size() : " << v.size() << endl;
cout << "v.capacity() : " << v.capacity() << endl;
// capacity should be the same here as it was above

// this is faster since the [] operator doesn't do any
// bounds checking like push_back does, and is
// therefor faster. When inlining is turned on,
// some implementations of the [] operator are
// exactly as fast as naked C array accesses
// (at least that's what I've been told. I haven't
//  benchmarked it myself)

-JeroMiya
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Confused as to array behaviour kendric 3 2,224 Mar 14, 2009 05:26 PM
Last Post: kendric
  Endian-ness ia3n_g 11 3,968 Sep 20, 2006 11:47 PM
Last Post: OneSadCookie
  memcpy(stuct array pointer struct array point) unknown 22 10,255 Sep 29, 2005 03:16 PM
Last Post: unknown
  Confused While Debugging Nick 2 3,194 Feb 24, 2005 10:04 AM
Last Post: Nick