ConsoleApplication2.exe中的首次机会异常:0xC0000005:写入访问冲突,c++
First-chance exception in ConsoleApplication2.exe: 0xC0000005: Access violation writing, c++
我正在玩一些代码,并试图使其工作,但似乎我错过了一些东西。。。有人能告诉我我错过了什么或做错了什么吗?程序在中断*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
以下是完整的代码:我正在visual studio 2012中编译,如果这与它有关的话…
#include <iostream>
using namespace std;
/*
struct StubData{
char * pFileBuffer;
long long FileSize;
char * pKey;
long KeySize;
};*/
class StubData{
public:
char *pFileBuffer;
long long FileSize;
char *pKey;
long KeySize;
StubData(){}
};
void Decrypt(StubData * sData){
int i=0,j=0;
for(i;i<sData->FileSize;i++){
*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
j++;
if (j>=sData->KeySize)j=0;
}
}
void Encrypt(StubData * sData){
int i,j;
sData->pKey="mysecretpassword";
sData->KeySize=strlen(sData->pKey);
j=0;
printf("[*]Encodingn");
for(i=0;i<sData->FileSize;i++)
{
*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
j++;
if (j>=sData->KeySize)j=0;
}
}
void main(){
//StubData S;
StubData *S = (StubData *)malloc(sizeof(StubData));
new (S) StubData;
S->pFileBuffer="MARKO";
S->FileSize=strlen(S->pFileBuffer);
Encrypt(S);
cout<<"nencn"<<S->pFileBuffer;
Decrypt(S);
cout<<"ndecn"<<S->pFileBuffer;
}
您为S->pFileBuffer
分配一个字符字符串文字
S->pFileBuffer="MARKO";
这些字符串文字是不可变的(通常编译为.rodata
)。如果你想要一个可变的字符串,你应该把它分配到某个地方。
你可以做一些类似的事情
char str[] = "MARKO"; //be careful, if this goes out of scope before S, then S has a dangling pointer
S->pFileBuffer=str;
这不是很像C++。但是您的代码的其余部分看起来也不太像C++。
相关文章:
- 在类 12.exe 中0x7B37FF80 (ucrtbased.dll) 引发异常: 0xC0000005:访问冲突读
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- (此函数用于计算 SMA).在 FA.exe 中0x00509159引发异常: 0xC0000005:访问冲突读取位置0
- 0x0F50DF58:0xC0000005:访问冲突读取位置0x0047CA04时未处理的异常
- 0xC0000005:访问冲突读取位置0x0000000000000008
- 实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取
- 0xC0000005:访问冲突读取位置0x0000000000000000.C++
- 在 DirectXGame.exe 中0x00B84CD6出现未经处理的异常:0xC0000005:访问冲突读取位置0x
- IXAudio2 - 0xC0000005:访问冲突写入位置0x00000000
- 0xC0000005:访问冲突读取位置0x00000000哈希函数
- 引擎.exe 0x00000000时未处理的异常: 0xC0000005:访问冲突
- 运行时错误0xC0000005 - 访问冲突
- 获取IDropTarget与HWND抛出0xC0000005访问冲突
- 程序.exe中0x010F2F1C时未处理的异常: 0xC0000005:访问冲突读取位置0xCCCCCCD0
- 模拟时钟.exe 0x00BF57F9时未处理的异常: 0xC0000005:访问冲突读取位置0x00000000
- 0xC0000005:访问冲突读取位置0x00000000 c++
- 0xC0000005:访问冲突读取位置0x00000008
- 0xC0000005:访问冲突执行位置0x00000000.(OpenGL)
- 0xC0000005:访问冲突读取位置0x00000004