Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Xces

How to implement dynamic "custom" arrays..?

This topic is 6017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, i want to read a filename (or directory) which contains pictures. Let''s assume i want to read a text file. I want to display these pictures in my OpenGL app. Therefore i want to load the entire text file into a dynamic array. Why dynamic array? First of all i don''t know how many items will be in the text file, and second of all i want to be able to add images while the app is running. a) But i need an array which not only contains the filename, but also contains the extension. So my guess is i must use a struct for this. b) Anybody has some examples of how to initialize such an array and how to add an item, delete an item etc etc? maybe another way of doing this? Greetz Xces (2 weeks c++ experience and growing )

Share this post


Link to post
Share on other sites
Advertisement
Hey there, with your suggestion about linked list, i went on searching for it on Google, read some documents and came to this site:

http://www.cprogramming.com/tutorial/lesson15.html

Very interesting with good explanation of what''s what. But now my next question: As they say on this page, a (singular) linked list can only be pushed and popped;

If my linked list is 15 items big, and i want to delete item 7, so 8 becomes 7, 9 becomes 8 and so on, this cannot be done?

And also.. I read that for me to read item 15, i have to traverse throught items 1..14 is that right? Any work-arrounds, as in, standard linked list classed which support such navigation?

Share this post


Link to post
Share on other sites
quote:
Original post by Xces
If my linked list is 15 items big, and i want to delete item 7, so 8 becomes 7, 9 becomes 8 and so on, this cannot be done?

It can be done, but it requires navigation through the list to get item 6 and 7 (6 since we can''t go backwards, and must link 6 to 8). A doubly linked list helps with ''I have a pointer to node x and want to delete it'' situations (since we don''t have to get x-1, because x has a parent node).
quote:
Original post by Xces
And also.. I read that for me to read item 15, i have to traverse throught items 1..14 is that right? Any work-arrounds, as in, standard linked list classed which support such navigation?

There''s no single workaround, but most of the time it isn''t a problem. The most common use of a linked list is to loop through from beginning to end, in which case you don''t run into the ''I must get x, so I have to loop through x-1 nodes'' issue. If random access is crutial, but appending and deleting items isn''t, then you may actually want to use a dynamic array. The STL''s vector class is a dynamic array wrapper than can hide all of the resizing from you.

Share this post


Link to post
Share on other sites
It''s OK :D
----------------------------------------------------------------
But anywayz...
----------------------------------------------------------------
Is this a good starting point for me?

http://www.cs.smith.edu/~nhowe/teaching/csc112/Tutorials/linklist.html

Basicly i only need to add (e.g. initialize) the linked list while i read in the file, and move through it untill i reach the end and then restart from head()->tail(). When the file changes, i have to "empty" the linked list, so i can refill it again.
So searching in, and deleting @ positions is not needed by me.

Share this post


Link to post
Share on other sites
Pixellate (web-zine for games programmers, find via google) did a great article on the STL which should answer all your questions on list, vectors etc. I recommend giving it a read.

Dan

Share this post


Link to post
Share on other sites
btw, you can add to your text file the number of files that are there, so when reading in the file you know how many char* you need for the file name ( a simple malloc(num*szieof(char*)) allocation). then when reading the filenames place an upper limit like MAX_PATH (which is the windows upper limit and defined in the windows.h header). this is useful becuase you can have a tmp array which you read the file name into then allocate the space in your filelist array.


  
// PSUDEO code

char **filelist = (char**)malloc(numFiles*sizeof(char*));
for(i=0; i<numFiles; i++)
{
char tmp[MAX_PATH]; // like 512 or 1024

getline(tmp);
filelist[i] = (char*)malloc(strlen(tmp));
strcpy(filelist[i], tmp);
}

// deinit

for(i=0; i<numFiles; i++)
{
free(filelist[i]);
}
free(filelist);

// to get the extension

chra *ext = strrchr(filelist[i], ''.'')+1;


hope that helps

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!