What is causing this access violation?

Hello, I am trying to write a function using cstdarg. My function will return true if a number of terms are found in order. E.G:searchTermsInOrder("hello everybody how are you all today", 3, "hello", "you", "today") would return true. Here is my code:
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
27
28
29
bool searchTermsInOrder(string s_string, int i_numberOfTerms, ...)
{
	int i_termsSearched = 0;
	int i_termScanPosition = 0;

	va_list v_parameters;
	va_start(v_parameters, i_numberOfTerms);

	string s_currentSearchTerm = va_arg(v_parameters, string);

	for(int i_iterator = 0; i_iterator < s_string.length(); i_iterator ++)
	{
		if(s_currentSearchTerm[i_termScanPosition] == s_string[i_iterator])
			i_termScanPosition ++;
		else
			i_termScanPosition = 0;

		if(i_termScanPosition == s_currentSearchTerm.length())
		{
			i_termScanPosition = 0;
			i_termsSearched ++;

			s_currentSearchTerm = va_arg(v_parameters, string);
		}
	}
	va_end(v_parameters); 
	if(i_termScanPosition == i_numberOfTerms) return true;
	else return false;
}
My issue is that every time I run my code I get a strange access violation error. Does anybody know whats causing this, and if so, what I should do to fix it? Thankyou
Last edited on
Argument "hello" has type const char *. You are trying to access it as if it has type string.
Last edited on
line 9:
va_arg(v_parameters, string);

That is why.

You are not putting a string in the va list, you are putting a literal char*.

Change this to va_arg(v_parameters, const char*);

You're also doing the same thing on line 23.
Okay. If I set them to const char* I get an assertion error regarding an invalid null pointer, and if I keep them as strings and pass three string objects as parameters I get another access violation.
Last edited on
The function you wrote shows that you are not using GCC compiler ( in gcc it wud not have managed to compile ) ... anyways here is the code corrected :

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
27
28
29
30
31
32
33
bool searchTermsInOrder(string s_string, int i_numberOfTerms, ...)
{
	int i_termsSearched = 0;
	int i_termScanPosition = 0;

	va_list v_parameters;
	va_start(v_parameters, i_numberOfTerms);

	string s_currentSearchTerm = va_arg(v_parameters, char*);

	for(int i_iterator = 0; i_iterator < s_string.length() ; i_iterator ++)
	{
		if(s_currentSearchTerm[i_termScanPosition] == s_string[i_iterator])
			i_termScanPosition ++;
		else
			i_termScanPosition = 0;
		
		if(i_termScanPosition == s_currentSearchTerm.length())
		{
			i_termScanPosition = 0;
			i_termsSearched ++;

			if (i_numberOfTerms == i_termsSearched)  /* this will stop this for loop when your search is completed */
				{
                                   va_end(v_parameters); 
                                   return true;
                                 }
			s_currentSearchTerm = va_arg(v_parameters, char*);
		}
	}
	va_end(v_parameters); 
	 return false;
}


access violation error are occurred when you try to get a argument (via va_arg ) which was never passed ( in this case it was because you were trying to get fourth variable argument )

Also check out that last if statement you are using in your function
1
2
if(i_termScanPosition == i_numberOfTerms) return true;
	else return false;
do you think it correct ??
Last edited on
Thankyou very much :) Its working perfectly now
Topic archived. No new replies allowed.