Run-Time Check Failure #2 - Stack around the variable was corrupted.

Hi Guys

Is there anyone around who is good with C++?
I keep getting an error when trying to copy a char array to a char pointer e.g

strncpy_s(buffer, sizeof(temp), temp, _TRUNCATE);

I get a "Stack around the variable 'temp' was corrupted"

Please let me know if more information is needed.

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
34
35
36
37
38
39
  void fixupstr(char * buffer)
{
	char     temp[1024] = { 0 };
	char *   pos = buffer;
	char *   destpos = temp;
	char     lastchar = 0;

	while (*pos != 0)
	{
		if (((*pos == '{') ||
			(*pos == '}') ||
			(*pos == ';') ||
			(*pos == ',') ||
			(*pos == '=')) && (lastchar != ' ') || (lastchar == ',') || (lastchar == '='))
		{
			*destpos++ = ' ';
			*destpos++ = *pos;
		}
		else
		{
			*destpos++ = *pos;
		}
		lastchar = *pos++;
	}
	*destpos = 0;
	if (*(destpos - 1) == '\n')
	{
		*(destpos - 2) = 0;
	}
	
	strncpy_s(buffer, sizeof(buffer), temp, _TRUNCATE);
}

int main()
{
      char buffer[1024] = {0};
      fgets(buffer, sizeof(buffer), *inFile); //inFile is the input file
      fixupstr(buffer);
} 
Is there anyone around who is good with C++
Hello gothnerd,

With a run time error of "Stack around the variable 'temp' was corrupted" usually means that you tried to access an array outside the size or boundary of an array.

Hope that helps,

Andy
Last edited on
If I were in the bunch of the good C++ programmers, I would try to give you an answer; since I'm not, I can only point out that your code does not compile and you did not provide input data to test. That makes your question more suitable for clairvoyants than programmers.
However, these instructions of yours are really dangerous:
1
2
3
4
if (*(destpos - 1) == '\n')
{
    *(destpos - 2) = 0;
}

Are you sure to know what’s in that memory area?
For what I can see in your code, "destpos" could be incremented only by one 'step', but you could try to access the memory two 'steps' lower.
Last edited on
Topic archived. No new replies allowed.