Click here to Skip to main content
15,033,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my project, I am using the wcscpy_s function. I went to the msdn website and they say that the second argument is the size of the destination buffer and when I build the project, crash.

When I equal the second argument to the lenght of the source string, the programa runs without any error.

Here is the function where occurs the error.

C++
void remove_space(wchar_t *(&equation), int &len)
{
	wchar_t equation2[MAX_EQUATION];
	int eq_index = 0;
	for(int i = 0 ; i < len ; i++)
	{
		if(equation[i] != ((wchar_t)(32))/*space*/ && equation[i] != ((wchar_t)(9))/*tab*/)
		{
			equation2[eq_index] = equation[i];
			eq_index++;
		}
	}
	equation2[eq_index] = NULL;
	wcscpy_s(equation, wcslen(equation2)+1, equation2);//?
        wcscpy_s(equation, 512, equation2);//?
	len = eq_index;
}


Thanks in advance.
Filipe
Posted

It seems that you are just started programming C/C++, because your code shows some misunderstandings and will probably result in buffer overflows. But everybody was a beginner once and made such or similar mistakes.

A proper implememtation of your code would be:
C++
size_t remove_space(wchar_t *equation)
{
    // length of input string, buffer size is at least len+1
    size_t len = wcslen(equation);
    // allocate buffer that always fits
    wchar_t *equation2 = new wchar_t[len+1];
    size_t eq_index = 0;
    for (size_t i = 0; i <= len; i++)
    {
        if (equation[i] != L' ' && equation[i] != L'\t')
            equation2[eq_index++] = equation[i];
    }
    wcscpy(equation, len+1, equation2);
    delete [] equation2;
    return --eq_index; // return length of string
}


However, removing chars from a string may be also done without using a help string. Another option is to use a string class providing a remove function (e.g. CString and Remove).
   
Comments
Richard MacCutchan 11-Jan-12 8:58am
   
Alternatively you could return len;
Filipe Marques 11-Jan-12 14:29pm
   
Thanks Jochen,
works fine now.
However, I have a question: why you use

size_t remove_space(wchar_t *equation)

instead

size_t remove_space(wchar_t *(&equation))

?

Because I want the string back.

Filipe
Jochen Arndt 11-Jan-12 14:36pm
   
The calling context still owns the string and has full control on it. So there is no need to return it in any way (the content is modified in the original string memory location).
Michael Haephrati 9-Jan-15 17:00pm
   
It is advicedd to use wcscpy_s instead of wcscpy while your solution is using wcscpy
You say this works but which version? Are you sure that equation is 512 words long? Also you could make your code clearer by using proper character constants like this:
C++
if(equation[i] != L' ' && equation[i] != L'\t')
   
Comments
Filipe Marques 11-Jan-12 14:23pm
   
I use normally char instead of wchar_t and I don't noticed this mistake.
thanks,

Filipe
Richard MacCutchan 11-Jan-12 17:42pm
   
It's much better to use the proper types rather than using casts, as the compiler is more likely to spot your mistakes.
Filipe Marques 12-Jan-12 5:16am
   
i thought that for the compiler was the same.
And I use a lot of cast in code, and I always used.
Thanks
Filipe
Richard MacCutchan 12-Jan-12 5:25am
   
No, for the compiler it's different. If you are using a lot of casts then I would suggest you are creating the potential for a lot of problems. Using a cast bypasses the compiler's type and error checking so you should only use it where you are certain that it is the only way. Using it in the way you showed in your original code is not a good idea.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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