General Container Issues

I've got a class that for all intents and purposes is:
1
2
3
4
5
6
7
8
9
class Entry[
public:
  Entry(){};
  ~Entry(){
    delete [] word;
  }  
  unsigned int letters;
  unsigned char * word;
};


I'm a little unsure of how to contain it (array, vector, list)

Still simplified, say the file is series of integers followed by a string of that length. In fact, lets say the file starts with an integer defining the amount of words.

I need a container of Entry. It is simple enough to make the dynamic array of entries, but this is a step away from the whole point of the program.

I've tried making std::list<Entry> and std::list<Entry*>, but these both give me memory problems.

Can a list have dynamically allocated memory in it? I was having similar problems with Vectors. I must not be using something correctly
Last edited on
Sure they can. Every container should be able to accomodate both Entry and Entry* members, but it's usually better to use pointers because dynamically allocated members have "infinite" scope. So deciding which container to use should be determined by usage (e.g. lots of random access = vector, lots of insertion and deletion = list, etc.) Creating your container shouldn't be that hard. Try it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
// create your container
std::vector<Entry*> vec;

// add some elements
vec.push_back(new Entry);
vec.push_back(new Entry);

// don't forget to deallocate all of your memory
for (std::vector<Entry*>::iterator iter = vec.begin();
	 iter != vec.end(); ++iter) {
	delete *iter;
	*iter = NULL;
}


Also, you're going to need to out some code in your constructor to dynamically allocate word or your deconstructor will cause the program to crash :/
Last edited on
Thanks for the help :)
Topic archived. No new replies allowed.