Can anyone see anything wrong about this small segment?

1
2
3
4
5
6
7
8
9
10
11
12
 void appendErrorLog(std::string tag, Error *ptr)
{
	static int size = ptr->invalidTags.size();

	ptr->invalidTags.resize(size+1);
	ptr->invalidTags.push_back(tag);
	std::cout << "\n --- " << tag << " --- "; // debugging

	ptr->invalidTagsIndex = ptr->invalidTags.size();

	return;
}


invalidTags is a vector of std::strings
invalidTagsIndex is just an integer

EDIT* Or perhaps a problem I that might be happening because i'm not getting the expected results.
Last edited on
Without the information for your side classes/objects, this diagnosis is very difficult.
Well, for one thing the static is initialized only once, the first time into the function.
Well, for one thing the static is initialized only once, the first time into the function.

What is the problem with that? I thought that was the point of a static int in a funciton like this. To retain its value after returning to the caller.
I'm not sure but the call to size() may execute every time the function runs; I don't remember.
It runs once.

I don't know if it's a problem -- you know the code that uses the function.

Example:

On first call to appendErrorLog, let's say ptr->invalidTags.size() returns 0.
Line 3 sets size to 0.
Line 5 resizes the invalidTags container to 1.
Line 6 adds the tag to the container.
Line 9 sets invalidTagsIndex to 1.

On second call to appendErrorLog, *whatever* ptr->invalidTags.size() is:
Line 3 is not executed;
Line 5 resizes the invalidTags container to 1.
Line 6 adds the tag to the container.
Line 9 sets invalidTagsIndex to 1.

On third call to appendErrorLog, *whatever* ptr->invalidTags.size() is:
Line 3 is not executed;
Line 5 resizes the invalidTags container to 1, which has the effect of dropping the second tag.
Line 6 adds the new tag to the container.
Line 9 sets invalidTagsIndex to 1.

etc.

On the surface it seems wrong to me.
Yes! That is what is happening and it took the step-through of your post to show me why, so thank you.

However, I have a small problem with the new code, the container seems to be increasing in size by 2 when all I need is 1. Take this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void appendErrorLog(std::string tag, Error *ptr)
{
	int static size;

	if(ptr->invalidTags.empty())
		size = 0;
	else
		size = ptr->invalidTags.size();

	ptr->invalidTags.resize(size+1);
	std::cout << "The size is " << ptr->invalidTags.size(); // debugging
	ptr->invalidTags.push_back(tag);
	std::cout << "\n --- " << tag << " --- "; // debugging

	ptr->invalidTagsIndex = ptr->invalidTags.size();

	return;
}


The output I get for this is:

 --- *tag* --- The size is 1
 --- *tag* --- The size is 3
 --- *tag* --- The size is 5
etc...
You are misusing the container.

You do not need to resize() it to make room for a new element that you add via push_back().

resize() to size() + 1 actually adds a default constructed element to the end of the
container.

Your function can be as simple as

1
2
3
4
void appendErrorLog( const std::string& tag, Error& err ) {
    err.invalidTags.push_back( tag );
    err.invalidTagsIndex = err.invalidTags.size(); // Not sure why you need this?
}

Yeah I'm not sure why I've actually put that there... Also, why have you used the object not the pointer?
Topic archived. No new replies allowed.