C/C++语言-指针和算术..异常代码:c0000005
C/C++ language - POINTERS and ARITHMETIC... Exception Code: c0000005
下面的代码编译正确,但在执行时,控制台显示以下错误。。。异常代码:c0000005。错误发生在以下行:
*cptr++ = hextbl[((tval >> 4) & 0x0F)];
此错误是由于对内存的访问不正确。这样,我相信这个错误可能是我仍然在做的事情不正确理解指针和算术。。。
#include <stdio.h>
// function prototypes
int main(int argc, const char *argv[]);
char *put_hexbyte(char *cptr, char tval);
// main routine
int main(int argc, const char *argv[]) // variables to get arguments
{
char val = 65; // 0x41 >>> I need 2 bytes 0x34 and 0x31,
// they are ASCII from 0x41 (0x34 = "4" and 0x31 = "1")
char *bufASCII; // pointer to store these ASCII
bufASCII = put_hexbyte(bufASCII, val);
return 0;
}
// Put a byte as hex ASCII, return pointer to next location.
char *put_hexbyte(char *cptr, char tval)
{
static char hextbl[16] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
printf("at this point, all is OK!!!n"); // <<< OK!
*cptr++ = hextbl[((tval >> 4) & 0x0F)]; // <<< memory violation error! (Exception Code: c0000005)
*cptr++ = hextbl[tval & 0x0F];
return(cptr);
}
谢谢你的帮助!:)
您的指针:
char *bufASCII;
未初始化。然后你写信给它:
*cptr++ = x;
您需要先初始化它,否则使用它是未定义的行为。例如:
char *bufASCII = new char[2];
尽管即使在那时,这:
bufASCII = put_hexbyte(bufASCII, val);
会丢失对原始指针的跟踪。如果您想要缓冲区末尾的返回值,您应该存储该分隔符l:
char* eob = put_hexbyte(bufASCII, val);
这:
char *bufASCII; // pointer to store these ASCII
是一个指向字符的指针,但它实际上还没有指向任何东西,因此您不能写入该指针。
相关文章:
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- VisualStudios 会抛出异常,而代码块不会 [C++]
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 为什么代码段会引发浮点异常?
- 我应该C++自定义异常代码放在哪里?
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- C++中的奇怪异常代码,当用户输入为 23 时,退出值 1,073,740,940(代码:0xc0000374)
- 如何在Visual Studio(2015)中增加堆栈尺寸的堆栈尺寸?异常代码:C00000FD
- 从覆盖范围报告中删除自动生成的异常代码
- 防火墙异常代码仅适用于传出连接
- 需要对异常代码进行解释
- C/C++语言-指针和算术..异常代码:c0000005
- 异常代码"EXC_I386_GPFLT"的含义是什么?
- 单元测试失败,异常代码为c0000005
- 如何使异常代码DRY
- 访问计算机指针时发生访问冲突(c系统异常代码:c0000005),导致应用程序崩溃
- 如何在Windows上诊断异常代码0xc0000409
- 如何在iOS Objective-C中捕获C++异常代码