C 指针转换会导致内存访问冲突
C pointer convertion cause memory access violation
我正在尝试使用MSVC 2015 64位编译器将旧项目从Borland BCC 5迁移到Qt 5.6.3 Microsoft。
以下代码是原始作品的模型。它在Visual Studio 2015和Borland BCC 5中按预期工作,但是,当我尝试使用Qt Creator 3.6.1在Qt 5.6.3中运行它时。它抛出一个错误,因为"*hcID"不可访问。
#include <stdlib.h>
typedef void (*ProcFoo) (void *pvData, unsigned long ulBarTag);
static char* gpcID = NULL;
typedef struct FooType
{
ProcFoo pfFoo; ///< pointer to Foo callback function
unsigned long ulBarTag; ///< base id
} FooType;
FooType* pFoo = NULL;
void RegisterAsFoo(unsigned long ulBarTag, ProcFoo pfFoo)
{
pFoo = (FooType*)malloc(sizeof(FooType));
pFoo->ulBarTag = ulBarTag;
pFoo->pfFoo = pfFoo;
}
void GetBarTag(unsigned long *pulBarTag, unsigned long ulBarTag)
{
*pulBarTag = ulBarTag;
}
int main()
{
char s[] = "Some Value";
gpcID = s;
char ** hcID = NULL;
RegisterAsFoo((unsigned long)&gpcID, (ProcFoo)GetBarTag);
pFoo->pfFoo(&hcID, pFoo->ulBarTag);
// after the execution, the hcID is still NULL in Qt 5.6.3, however,
// it is valid in Visual Studio 2015 and Borland BCC 5.
char* result = (*hcID);
return 1;
}
如果您有任何意见或想法,谢谢!
问题出在
(unsigned long)
适用于 32 位的强制转换。 用
(unsigned long long)
相反。您需要到处进行更换。
否则,请摆脱 int 到指针的转换或使用 32 位编译器
相关文章:
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++中的内存分配(引发异常:读取访问冲突)
- 使用加速进程间创建消息队列 - 内存访问冲突
- C 指针转换会导致内存访问冲突
- 为什么创建进程 API 调用会导致内存访问冲突?
- 使用 std::map 作为本地成员的内存访问冲突
- 编译哈希算法时出现内存访问冲突错误
- 使用内存访问冲突错误
- 堆栈引发内存访问冲突
- 使用 Boost::序列化时内存访问冲突
- 调用std::vector::empty()时内存访问冲突读取位置
- 基于 SAPI 的应用程序在枚举令牌时引发内存访问冲突
- 内存访问冲突
- 使用 VC++ 中的 Try 和 catch 块捕获内存访问冲突
- 对象向量中的内存访问冲突
- "detects_simple_anagram"中的致命错误:地址 0x00000000 处的内存访问冲突:故障地址处没有映射
- 内存访问冲突插入结构在树结构c++
- c++内存访问冲突
- MotorBee dll和c++,内存访问冲突
- 调用FindConnectionPoint时写入内存访问冲突