Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to concat vectors when I get data from cable in my while loop. I search for
C++
copy
method in C++ array a lot and I saw examples like my code which are supposed to run correct, however I am getting error as in title. I put breakpoints and I observed;
copy(GetIntArrayFromCharArray(vec).begin(), GetIntArrayFromCharArray(vec).end(), it+intData.size());
in this line first time it calls
GetIntArrayFromCharArray
was okay, then it calls it for
.end()
then error shows up. I also watched iterator, it is not empty. I couldn't figure out what should I do?

(PrevFrameFragmentNo and other variables are just to trace frame and segments in UDP packet data.)

What I have tried:

THIS CODE PIECE IS IN WHILE LOOP, TAKES DATA TO BUFFER
//char buf ----> vecor<char> vec
		int n = sizeof(buf) / sizeof(*buf);//number of elements
		vector<char> vec(buf, buf + n);

		 


		if (FrameFragmentNo == 1)// First fragment
		{
			intData = GetIntArrayFromCharArray(vec);
			PrevFrameFragmentNo = FrameFragmentNo;
		}
		else if (FrameFragmentNo <= TotalFragmentCount)  // Other Fragments
		{
			vector<uint16_t> concatAry;
			std::vector<uint16_t>::iterator it;

			
			
			
			concatAry.assign(intData.begin(), intData.end());
			

			it = concatAry.begin();
			//PROBLEMATIC LINE IS HERE
			copy(GetIntArrayFromCharArray(vec).begin(), GetIntArrayFromCharArray(vec).end(), it+intData.size());

			
			intData = concatAry;
			PrevFrameFragmentNo = FrameFragmentNo;
			count++;
		}


THIS CODE IS GetIntArrayFromCharArray() METHOD
vector<uint16_t> GetIntArrayFromCharArray(vector<char> arr)
{
	
	if ((arr.size() % 2) == 1)
		arr.resize(arr.size()+1);
	


	vector<uint16_t> intArray;

	for (int i = 0; i < arr.size(); i += 2)
		intArray.push_back((uint16_t)((arr[i] << 8) | arr[i + 1]));

	return intArray;
}
Posted
Updated 5-Sep-19 22:22pm
v3

1 solution

The function GetIntArrayFromCharArray creates a copy, therefore your calls to begin() and end() refer to two different arrays! This is reflected literally in the error message - all you need to do is read and understand it. ;-)

[..] (cut away revised parts of the solution - see below)

P.[...]S.:
[...]
Sorry, I need to revise my solution a third time:
It seems you are trying to append multiple converted int arrays in a loop: the first time you simply call
GetIntArrayFromCharArray
, and that's fine. Inthe following loop iterations, you convert the char array and try to append it to the existing int array.

As for the solution you're looking for, the first thing you need is a local variable to hold the converted int array, as pointed out above, and then you need append(), like this:
C++
auto myIntArray = GetIntArrayFromCharArray(vec);
concatAry.append(myIntArray.begin(), myIntArray.end());

The copy() function can be used only for the purpose of copying data to a location that is already allocated. Your concatAry however is only large enough to hold it's existing elements, therefore you need to use append() instead. Alternately you could use resize() first, but why use two function calls when you can do it in one? ;-)
 
Share this answer
 
v4
Comments
Danny96 6-Sep-19 4:29am    
you are right, I correct it but it gives "can not seek iterator after end" error now, iterator is not empty, I don't understand why compiler struggles with iterator, do you have an idea? :(
phil.o 6-Sep-19 4:42am    
Most probably, you are trying to copy the source array into destination array, and destination array is too small to host the data you are trying to fit in.
Why don't you use debugging for that kind of issue?
Danny96 6-Sep-19 4:46am    
I am already on debug mode and source is bigger than destination array I already know but they are vectors, it shouldn't be a problem I thought
phil.o 6-Sep-19 4:51am    
It is not a problem when you use the right push_back method to add elements at the end of the vector.
Is is a problem, though, when you deal directly with iterators. Iterators are pointers; they point to a memory address in application's space. If you manipulate the pointer arithmetically, you may end up referencing a memory address which is beyond the area assigned to the vector. This is a typical buffer overflow.
Stefan_Lang 6-Sep-19 6:11am    
Iterators are not pointers, although they behave very similarly. But that is not the problem here anyway. I've modified my solution with further insights and advice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900