变量周围的另一个“堆栈已损坏 [英] Another 'Stack around the variable was corrupted

查看:164
本文介绍了变量周围的另一个“堆栈已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习,因此可以原谅另一个菜鸟问题.
我会将代码简化到让我感到悲伤的地方.

我要做的就是得到一个字符串,在其末尾加一个数字,然后显示出来.字符串按计划到达屏幕,但是在函数末尾出现堆栈损坏,因为它将控制权返回给WndProc.

I''m still learning so forgive another noob problem.
I''ll abreviate the code to around the area that has given me grief.

All I''m trying to do is get a string, slap a number onto the end of it, then display it. The string arrives on the screen just as planned, but I get the Stack Corruption at the end of the function as it returns control to WndProc.

#include <windows.h>
#include <stdlib.h>

void FailingFunction(HWND hwnd)
{
   HDC hdc = GetDC(hwnd);
   RECT TextRect1;

   int aNumber = 10;
   wchar_T ident[] = L"A string";
   wchar_T Out[50];

   TextRect1.top = 15;
   TextRect1.left = 15;
   TextRect1.bottom = 100;
   TextRect1.right = 100;

   PrepTextOut(ident, Out, aNumber);

   DrawText(hdc, Out, -1, &TextRect1, DT_SINGLELINE | DT_CENTER | DT_VCENTER)

   ReleaseDC(hwnd,hdc);
}

void PrepTextOut(wchar_t* str, wchar_t* dest, int value)
{
   wchar_t cNum[10]; //Wide character array to hold integer
   size_t cNumSize = 10; //size of ''wchar_t cNum'' 
   size_t destSize = wcslen(dest); //size of dest wide character array
   _itow_s(value, cNum, cNumSize, 10); //Convert cNum integer to wchar_t
   wcscpy_s(dest, destSize, str); //Copy str into dest
   wcscat_s(dest, destSize, cNum); //Contracate cNum onto dest
}</stdlib.h></windows.h>

我需要执行此步骤数十次,并希望缩短代码.我已经包括了所有使其运行的必需品.我的测试整数是1.测试字符串是"Strength".

我们非常感谢您的帮助.

I need to do this step dozens of times and was looking to shorten the code. I''ve included all of the necessities to make it run. My test integer was 1. Test string was ''Strength''.

Any help is greatly appreciated.

推荐答案

由于没有空格可以终止空字符,因此10位整数值可能会导致堆栈损坏. wcslen(dest);调用可能会导致未初始化的目标缓冲区上的访问冲突,同时寻找终止null来确定字符串的结尾.

我的建议已在下面的评论栏中陈述.
Stack corruption may occur by a 10 digit integer value because there is no space for terminating null character. wcslen(dest); call may cause access violation on uninitialized dest buffer while looking for a terminating null to determine the end of string.

My recommendations have been stated as comment lines below.
//void PrepTextOut(wchar_t* str, wchar_t* dest, int value)
void PrepTextOut(wchar_t* str, wchar_t* dest, size_t destSize, int value)
// needs one more parameter for destsize
{
  //wchar_t cNum[10]; //Wide character array to hold integer
  // 10 is not enough (together with terminator null and posible minus sign,
  // should be at least 12 for 32 bit integers)
  wchar_t cNum[16]; // guarantee

  // size_t cNumSize = 10; // size of ''wchar_t cNum''
  size_t cNumSize = sizeof(cNum) / sizeof(cNum[0]);

  // size_t destSize = wcslen(dest); //size of dest wide character array
  // dest may points uninitalized buffer so you shouldn''t do above

  _itow_s(value, cNum, cNumSize, 10); //Convert cNum integer to wchar_t

  wcscpy_s(dest, destSize, str); //Copy str into dest
  wcscat_s(dest, destSize, cNum); //Contracate cNum onto dest
}


可能是这样:

size_t destSize = wcslen(dest); //size of dest wide character array

您稍后将使用"destSize",但我认为此值未正确确定.从"FailingFunction"中,您将"wchar_T out [50]"作为目标"传递.但这只是装满了50 wchar的堆栈垃圾,无论当时发生了什么.因此size_t destSize = wcslen(dest)的结果不确定.

也许尝试如下初始化:

wchar_T Out[50] = L"";

或者尝试传递"destSize"(在这种情况下为50)作为参数,而不是尝试通过wcslen确定它.

-鲍勃
Could it be this:

size_t destSize = wcslen(dest); //size of dest wide character array

You''re using "destSize" later, but I don''t think this value is being determined correctly. From "FailingFunction", you''re passing "wchar_T out[50]" as "dest". But it''s just filled with 50 wchar''s of stack junk, whatever happens to be there at the time. So the result of size_t destSize = wcslen(dest) is undetermined.

Maybe try initializing as follows:

wchar_T Out[50] = L"";

Or try passing "destSize" (in this case 50) as a parameter instead of trying to determine it via wcslen.

- Bob


马上设置为50就可以了

wcscpy_s(dest,50,str); //将库复制到dest
wcscat_s(dest,50,cNum); //将cNum限制到dest

我会记得先获取数组的大小并将该值传递给函数.

感谢您对cNum的建议,为了安全起见,我将其设置为cNum [32]或可能的cNum [64].

这是我的第二个问题,我的第一个问题是真的,真的是noob(将sizeof与字面翻译"size of"相混淆).我快速学习是一件好事.
Setting to 50 right off the bat worked

wcscpy_s(dest, 50, str); //Copy base into dest
wcscat_s(dest, 50, cNum); //Contracate cNum onto dest

I''ll remember to get the size of the array first and passing that value to the function.

Thanks for the advice for cNum, I''ll set that to cNum[32] or possibly cNum[64] just to be safe.

And this is my second question, my first was really, really noob (confused sizeof with the literal translation ''size of''). It''s a good thing that I''m a quick study.


这篇关于变量周围的另一个“堆栈已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆