简体   繁体   English

: 0xC0000005: 访问冲突写入位置 0x00000000 C++ 与内联汇编

[英]: 0xC0000005: Access violation writing location 0x00000000 C++ with inline assembly

I've been writing this code to implement the C "strcmp()" function using C/C++ with embedded assembly code like this我一直在编写这段代码来实现 C "strcmp()" function 使用 C/C++ 和嵌入式汇编代码这样

bool myStrCmp(char* mystr1, char* mystr2) {
    if (myStrLen(mystr1) != myStrLen(mystr1)) return false;
    char s1[100], s2[100];
    strcpy_s(s1, mystr1);
    strcpy_s(s2, mystr2);
    int i = 0;
    int flag = 1;
    _asm mov ecx, flag;
    _asm 
    {
        push esi
        mov esi,i
      startCmp:
        mov al,s1[esi]
        mov dl,s2[esi]
        cmp al,NULL
            je endCmp
        cmp al,dl
            jne zeroFlag
        inc [esi]
        jmp startCmp
      zeroFlag:
         mov ecx,0
       endCmp:
            pop esi
    }
    _asm mov flag, ecx

    return flag == 1;

}

However, there is an exception at the exact line of jne zeroFlag saying : 0xC0000005: Access violation writing location 0x00000000但是,在jne zeroFlag的确切行有一个异常说: 0xC0000005: Access violation writing location 0x00000000

this exception happens whenever I enter a similar charecters in the first and second string generally每当我在第一个和第二个字符串中输入类似的字符时,就会发生此异常

I have no idea why does this happen我不知道为什么会这样

It seems your debugger stops at the last instruction before the one where an exception occurred.似乎您的调试器在发生异常的指令之前的最后一条指令处停止。 The error is actually at the next line:错误实际上在下一行:

inc [esi]

That tries to increment a value stored at address esi .这试图增加存储在地址esi的值。 Since esi is 0, incrementing a value at address 0 results in an access violation.由于esi为 0,因此增加地址 0 处的值会导致访问冲突。

To increment esi itself, simply write:要增加esi本身,只需编写:

inc esi

With that said, there's no need to copy C-strings into temporary arrays, you can compare them in-place (and you can optimize the process by comparing in dwords, falling back to a byte compare in the last chunk).话虽如此,没有必要将 C 字符串复制到临时 arrays 中,您可以就地比较它们(并且您可以通过在 dwords 中进行比较来优化过程,回退到最后一个块中的字节比较)。

        cmp al,dl
        jne zeroFlag

So, you fault on the jne instruction with address NULL.因此,您在地址为 NULL 的 jne 指令上出错了。 This is literally impossible.这实际上是不可能的。 Remembering how the processor works, it actually faulted on the previous instruction and IP points to the next one;记住处理器是如何工作的,它实际上在上一条指令上出错,IP 指向下一条; if the debugger doesn't adjust for this it faulted on the cmp instruction, which is equally impossible.如果调试器没有对此进行调整,它会在 cmp 指令上出错,这同样是不可能的。

Only one possibility.只有一种可能。 The code your running is not the code you see in the debugger.您运行的代码不是您在调试器中看到的代码。 Rebuild all, and fix your compilation errors, and try again.全部重建,修复编译错误,然后重试。

You should just replace this with您应该将其替换为

bool myStrCmp(char* mystr1, char* mystr2) {
    return 0 == strcmp(mystr1, mystr2);
}

You can't beat the builtin.你不能打败内置的。

暂无
暂无

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

相关问题 C ++:0xC0000005:访问冲突写入位置0x00000000 - C++: 0xC0000005: Access violation writing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000 IXAudio2-0xC0000005:访问冲突写入位置0x00000000 - IXAudio2 - 0xC0000005: Access violation writing location 0x00000000 复制构造函数问题 C++:“0xC0000005:访问冲突写入位置 0x00000000。” - Copy Constructor Issue C++: "0xC0000005: Access violation writing location 0x00000000." Project4.exe中0x00998876处的首次机会异常:0xC0000005:访问冲突写入位置0x00000000 - First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000 在 Project0_opengl.exe 中的 0x00000000 处抛出异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in Project0_opengl.exe: 0xC0000005: Access violation executing location 0x00000000 在 CobwebDiagram.exe 中的 0x00000000 处引发异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in CobwebDiagram.exe: 0xC0000005: Access violation executing location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM