Archived

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

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.

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 on other sites
A linked list is a better choice for that type of thing. You can find plenty of information on making your own or using the STL''s online.

Share on other sites

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 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 on other sites
You can always use a circular linked list, then use a back pointer that points to the last item in the list.

------

Check a Reseller''s Rating Before You Purchase!

Share on other sites
The url i posted above actually is a circular (double) linked list.

Share on other sites
Whoops!, Sorry bout that.

------

Check a Reseller''s Rating Before You Purchase!

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

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 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 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 codechar **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);}// deinitfor(i=0; i<numFiles; i++){   free(filelist[i]);}free(filelist);// to get the extensionchra *ext = strrchr(filelist[i], ''.'')+1;

hope that helps

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• Forum Statistics

• Total Topics
632910
• Total Posts
3009178
• Who's Online (See full list)

There are no registered users currently online

×