c++ - 0xC0000005 : Access violation writing in loaction

标签 c++ c visual-studio-2008

如果我不包含 HeapAlloc,我的程序会出错。我无法理解到底发生了什么。

int iIndex=0;

enum EDataType
{
    kINT,
    kFLOAT,
    kUINT
};
typedef  struct logstr
{
    EDataType sData_Type;
    string    sComment;
    bool      bStatus;
    float     fDump_Value;
    int       iDump_Value;
    UINT32    uDump_Value;
}slog,*StrLog;

Str_Dump[iIndex]->sData_Type=EDataType(0);//i get a error on this line exception0xC0000005: Access violation writing in location0X00000;

但是当我包含这一行时

Str_Dump[iIndex]=(StrLog)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(slog));

然后再次运行程序运行成功。

那条线到底在做什么?看不懂。

我已将 strdump 声明为

StrLog Str_Dump[100];

最佳答案

第二行为Str_Dump[iIndex]分配内存。 HeapAllocGetProcessHeap 是 WinApi 方法,您可以在 MSDN 网站上找到它们的文档。

第一行崩溃是因为您正在访问未分配的指针 - Str_Dump[iIndex]

关于c++ - 0xC0000005 : Access violation writing in loaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10123344/

相关文章:

c++ - 在某些情况下允许从 constexpr 调用非 constexpr 函数

c - 使用指针类型转换时值发生莫名其妙的变化

c++ - Cmake 中的可选目标

.net - 为什么在我的面向 .net framework 3.5 的 .net 项目中 System.dll 的版本是 2.x?

c++ - 我如何判断一个元组在 C++ 中是否为空

c++ - "const char(&a)[N]"是什么意思?

c# - 如何设置程序的CPU亲和性?

c++ - 使用 Visual Studio 2008 从 x86 编译 x64 dll

c++ - Visual Studio 2012 中的单元测试 C++ 控制台应用程序

c - 为什么 `strchr` 似乎适用于多字节字符,尽管有手册页免责声明?