Subset of list of objects

I have defined my own object type myClass, and in another class I am storing a list of instances of these objects. I have written the method below to return a subset of the list, based on some property of the object. However, if I modify one of the objects in the returned sublist, it is not reflected by the same object in the main list. Any advice on what I am doing wrong and how to resolve this problem would be greatly appreciated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
myClass* myList::getTaggedNames(size_t *listSize)
{
	size_t taggedCapacity = 10;
	size_t taggedCount = 0;
	myClass* taggedList = new myClass[taggedCapacity];
	for(size_t i = 0; i < numNames; i++)  // numNames is global var count of names in main list
	{
		if(names[i].isTagged())  // names is the global list
		{
			if(taggedCount >= taggedCapacity)
			{ // expand local list if necessary
				myClass* largerList;
				largerList = new myClass[taggedCapacity+5];
				copy(taggedList, taggedList+taggedCount, largerList);
				delete [] taggedList;
				taggedList = largerList;
				taggedCapacity += 5;
			}
			taggedList[taggedCount] = names[i];
			taggedCount ++;
		}
	}
	*listSize = taggedCount;
	return taggedList;
}
Hello kamikazeUnicorn,

Since 'taggedList' and 'names' are addressing different memory blocks you can copy (like you do with taggedList[taggedCount] = names[i];) but the're still independent and changes to one does not effect the other
I tried using an array of pointers here to try and get the 'taggedList' to contain pointers to the objects in 'names', but I was getting a segmentation fault when I tried to run it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
myClass* myLists::getTaggedNames_ALT(size_t *listSize)
{
	size_t taggedCapacity = 10;
	size_t taggedCount = 0;
	myClass** taggedList;
	*taggedList = new myClass[taggedCapacity];
	for(size_t i = 0; i < numNames; i++)
	{
		if(names[i].isTagged())
		{
			if(taggedCount >= taggedCapacity)
			{
				myClass** largerList;
				*largerList = new myClass[taggedCapacity+5];
				copy(taggedList, taggedList+taggedCount, largerList);
				delete [] taggedList;
				taggedList = largerList;
				taggedCapacity += 5;
			}
			taggedList[taggedCount] = &names[i];
			taggedCount ++;
		}
	}
	*listSize = taggedCount;
	return *taggedList;
}

Is this something that can be resolved, or is there essentially no straightforward way to achieve the desired results?
Last edited on
well i'd suggest that you use std::vector which does all the dynamic growing magic for you.

this is wrong:
1
2
  myClass** taggedList;
  *taggedList = new myClass[taggedCapacity];


it has to be
1
2
3
4
5
  myClass** taggedList = new myClass*[taggedCapacity]; // creating an array of pointer
  for(int i = 0; i < taggedCapacity; ++i) // fill the array of pointer
  {
    taggedList[i] = new myClass;
  }
the same applies to largerList
Oooh, thank you! That seems to work now. I did try to switch over to using a vector at one point, but then my code got completely messed up so I reverted back to this. I will look into using vectors for the future.
Thanks!
Topic archived. No new replies allowed.