Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array of pointers to a structure

Started by Bad Sector Jul 31, 2002 at 7:08 PM 3 replies 600+ views
Original Post
Bad Sector
Bad Sector
I was curious about the advantages of using an array of pointers to a structure. Let''s say I create a large structure and then declare an array of pointers to that structure and use malloc() to dynamically allocate each instance. Would this be of any advantage over just declaring an array of the structure type, or perhaps a number of individual variables of the structure type? I''m somewhat of a newbie to C/C++ and am trying to get a solid picture of the uses of pointers. Thanks in advance for any insight you can offer.
Crispy
Crispy
quote:
Original post by Bad Sector
I was curious about the advantages of using an array of pointers to a structure.


It totally depends on the properties of the structure (size, purpose, the size of the array). If you create an array of 600 containing 600 structures, each 5kb in size, you'd be stuck with 600*600*5 kB of memory. In such a case dynamic allocation would probably be very welcome since it is highly unlikely that you'll ever need those 600 structure instances at the same time.

quote:
Original post by Bad Sector
...and use malloc() to dynamically allocate each instance.



Use new and delete instead:


  
struct a {
int c;
//add a little constructor to make it less boring

a() {}
a(int s) { c = s }
};

a* structs[100];

structs[index] = new a(); //or...

structs[index] = new a(15);
//////

delete structs[index];

delete[] structs;


quote:
Original post by Bad Sector
I'm somewhat of a newbie to C/C++ and am trying to get a solid picture of the uses of pointers.



We've all been there and pointers we can't do without...

Hope this helps,
Crispy

edit: evil /source tag

[edited by - crispy on July 31, 2002 8:23:59 PM]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Zipster
Zipster
An array of pointers is good if the structure itself is very big, and you won''t be using all the elements at once. This allows you to save on memory. The downfall is that you then have to manage all those pointers by hand.
Bad Sector
Bad Sector
Ok, I think I''m getting a clearer picture of the processes. So when I allocate (for example, those 600*600*5Kb structs) they are all being thrown on the heap. Then I need to use pointers to access each instance of the struct on the heap, whereas if they were not allocated then I could just use variables instead of pointers. However, if they were not placed on the heap then they would fill up a large part of the available space on the code segment (of say, the small memory model). Do I have this right? I suppose I''m more concerned with how memory works and the purposes of dynamic allocation. But doesn''t Windows use a virtual memory model in which the DOS memory models don''t have any relevancy? I was reading that the virtual memory model consists of several gigs. That being the case, why even bother with dynamic allocation? I can''t imagine writing an array of structures that would equate to several gigs.

Sincerely intrigued,
Bad Sector

Crispy
Crispy
quote:
Original post by Bad Sector
I was reading that the virtual memory model consists of several gigs. That being the case, why even bother with dynamic allocation? I can''t imagine writing an array of structures that would equate to several gigs.



You''ve got to have those several Gigabytes of space in order to use it. Not everyone does. Basically, virtual memory is the slow portion of RAM that is borrowed on the expense of your hard drive space. If you''re using up, say, 300 Megabytes of RAM and your computer only has 128 - Windows writes stuff on the hard drive, causing swapping and other annoying things to appear. The bottom line is, if you fall back on virtual memory, you''re slowing down your program. IMO this is only the right thing to do when you have huge libraries that need to be loaded, but are never used all at once; example: Unreal will run on 32 Mb of RAM (was it 32 or 64?), but it normally requires somewhere around 150-200 to fully load itself because of the size and amount of its resources (music, texture libs, etc). Since one level can have textures from many libraries, and the purpose of those libraries is that a certain amount of textures can be loaded at a time, you may end up with a lot of RAM used up. Note that, I''m by no means an Unreal expert, so if I''m getting this wrong, please correct me.

Dynamic memory cannot be used in this case, at least not without CPU overhead which would probably slow down gameplay as well. If you have some internally defined data structures, however, that you won''t need to address all at once (better yet, if most of the time those structures are not needed), why deliberately torture the user''s computer? Above and beyond, if you''re writing a game, speed is probably what you''re after and that''s what dynamic allocation can give you a lot. Imagine a game consisting of 50 level, each having 50 monsters. If those levels are all connected (you can go back and forth from one level to another), would you rather load all those 50 levels and 2500 ghoulies statically, or would you load only the one level that the player is in at any given time and those 50 monsters, at a time?

Crispy

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.