简体   繁体   English

调试错误:变量'cardDesc'周围的堆栈已损坏

[英]Debug error: stack around the variable 'cardDesc' was corrupted

TITLE. 标题。 The function i'm using is this: 我正在使用的功能是这样的:

void GetVideoCardInfo(int* memoryVar, char* cardName)
{
    strcpy_s(cardName, 128, m_videoCardDescription);
    *memoryVar = m_videoCardMemory;
    return;
}

m_videoCardDescription is a '128 character long' character array that has the description of my video card in it. m_videoCardDescription是一个“ 128个字符长”的字符数组,其中包含我的视频卡的描述。 Here is where I am calling the function: 这是我在调用函数的地方:

bool writeGPUnameDesc() {
    char cardDesc;
    int cardMem;
    m_D3D->GetVideoCardInfo(&cardMem, &cardDesc);

    std::ofstream myfile;
    myfile.open("gpuNameAndDesc.txt");
    myfile << "Graphics card name: " << cardDesc;
    myfile << " - Graphics card memory: " << cardMem;
    myfile.close();

    return true;
}

When I run the program a message box pops up that says Runtime-check failure #2 and the title. 当我运行程序时,会弹出一个消息框,提示运行时检查失败#2和标题。 If anyone can help thanks in advance. 如果有人可以帮助提前表示感谢。

You are copying 128 characters into char cardDesc , which represents only 1 character. 您正在将128个字符复制到char cardDesc ,该字符仅代表1个字符。

You should change the type of cardDesc to a char-array: 您应该将cardDesc的类型更改为char数组:

char cardDesc[128];
// ...
m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
//                                ^ no &

TL;DR TL; DR

std::string GetVideoCardInfo(int & memoryVar)
{
    memoryVar = m_videoCardMemory;
    return m_videoCardDescription;;
}


bool writeGPUnameDesc() {
    int cardMem;
    std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem); 

    std::ofstream myfile;
    myfile.open("gpuNameAndDesc.txt");
    myfile << "Graphics card name: " << cardDesc;
    myfile << " - Graphics card memory: " << cardMem;
    myfile.close();

    return true;
}

Explanation 说明

strcpy_s(cardName, 128, m_videoCardDescription);

is a blatant lie. 是一个公然的谎言。 The size of cardName is exactly one character. cardName的大小cardName是一个字符。 If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you. 如果您撒谎对strcpy_s进行额外的检查以确保您不会溢出缓冲区, strcpy_s它将无济于事。

Inferior solutions 次等解决方案

Replace 更换

char cardDesc;   

with

char cardDesc[129]; 

The better approach gets rid of the magic numbers entirely. 更好的方法完全摆脱了魔幻数字

Up near the top of the file 在文件顶部附近

namespace // annonymous namespace. Contents will not leak out of the file
{
    constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
}

and then 接着

void GetVideoCardInfo(int* memoryVar, char* cardName)
{
    strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
    *memoryVar = m_videoCardMemory;
    return;
}

and

bool writeGPUnameDesc() {
    char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
    int cardMem;
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

    std::ofstream myfile;
    myfile.open("gpuNameAndDesc.txt");
    myfile << "Graphics card name: " << cardDesc;
    myfile << " - Graphics card memory: " << cardMem;
    myfile.close();

    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM