简体   繁体   English

字符数组访问冲突

[英]Access Violation on char array

I'm getting an access violation on a char array I just created using new . 我是一个字符数组我只是用创建上获得访问冲突new

DispatchCommand(char* cmdStr)
        {
            // Dispatch
            for(int i = 0; i < sizeof(_lpCommands); i++)
            {
                const int len = strlen(_lpCommands[i].szCommand);
                char* cmdblip = new char[len + 1];
                memcpy(&cmdblip, cmdStr, len);
                cmdblip[len] = '\0';  // Access Violation

                if(strcmp(cmdblip, _lpCommands[i].szCommand) == 0)
                {
                    if(strlen(cmdStr) > strlen(_lpCommands[i].szCommand))
                        (*_lpCommands[i].cbCallback)(&cmdStr[strlen(_lpCommands[i].szCommand)]);
                    else
                        (*_lpCommands[i].cbCallback)("");

                    delete cmdblip;
                    return;
                }

                delete cmdblip;
            }

            // Error and return
            *Out::ServerInfo<<"Command not found!"<<ENDL;
        }

_lpCommands is an array of Command structures: _lpCommands是Command结构的数组:

struct Command
{
    char* szCommand;
    CommandCallback cbCallback;
};

The produced error message is: 产生的错误消息是:

Unhandled exception at 0x012219cf in Program.exe: 0xC0000005: Access violation writing location 0x66647366. Program.exe中0x012219cf的未处理异常:0xC0000005:访问冲突写入位置0x66647366。

This was a rewrite of similar code which was using memcmp , which ended up giving me an access violation as well without be doing a memcpy . 这是对使用memcmp的类似代码的重写,最终也导致了我的访问冲突,而没有执行memcpy

What gives? 是什么赋予了?

Don't pass &cmdblip to memcpy . 不要将&cmdblip传递给memcpy You should pass a pointer to the destination buffer, not a pointer to that pointer. 您应该将指针传递给目标缓冲区,而不是该指针的指针。 Pass cmdblip instead. 改为传递cmdblip

Edit: I agree that in general, std::string should be used in C++. 编辑:我同意,一般来说,std :: string应该在C ++中使用。 Still, the technical reason this code crashes is that memcpy corrupts the cmdblip pointer, making it point on a memory location that is actually made of the first 4 bytes of the copied string. 仍然,此代码崩溃的技术原因是memcpy破坏了cmdblip指针,使其指向实际上由复制字符串的前4个字节组成的内存位置。 Then, cmdblip[len] results in a memory location that is not within the allocated buffer (or any other legally allocated buffer), hence the crash. 然后, cmdblip[len]导致内存位置不在分配的缓冲区(或任何其他合法分配的缓冲区)内,从而导致崩溃。 So, if you want to write better code, use C++ classes. 因此,如果您想编写更好的代码,请使用C ++类。 And if you want to understand why the given code crashed, consider the above. 如果您想了解给定代码崩溃的原因,请考虑以上内容。

The only possible helpful answer to this question is "Use std::string ". 该问题唯一可能的有用答案是“ Use std::string ”。 The specific problem you are having now will simply re-occur, or an identical one, every time you modify this function or write another like it. 每次您修改此功能或编写另一个类似的功能时,您现在遇到的特定问题都将简单地再次出现,或者完全相同。 The only way to solve the problem in the general case is to move to a class-based solution, which is kindly provided for you as Standard. 通常情况下,解决问题的唯一方法是转到基于类的解决方案,该解决方案已作为标准提供给您。 For example, your current code is exception unsafe, on top of whatever is giving you an access violation, not to mention that it's unreadable and begging for a number of other errors, such as off-by-one, not properly NULL terminating, double deletes, and memory leaks. 例如,您的当前代码是异常不安全的,除了给您带来访问冲突的原因外,更不用说它是不可读的,并乞求其他一些错误,例如,一一错误,未正确终止为NULL,两次删除,内存泄漏。 Oh, and UB because you delete what you new[] . 噢,UB,因为您delete了您的new[]

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

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