gpt4 book ai didi

c++ - 使用动态无符号字符数组时发生未知崩溃

转载 作者:行者123 更新时间:2023-11-28 07:59:08 25 4
gpt4 key购买 nike

谁能给我解释一下这是为什么:

unsigned char * buf;
buf = new unsigned char[dataSize];

我的 C++ 程序崩溃了吗?它没有给我任何错误,所以我真的不知道我的程序由于这些代码行而崩溃的原因。提前致谢!

编辑:这是我目前正在处理的项目的代码,它使用的是 OpenAL,因此如果您想重新编译代码,您将需要它。

#include <cstdlib>
#include <iostream>
#include <Windows.h>
#include <al.h>
#include <alc.h>
#include <vector>

using namespace std;

class SoundSource
{
public:
ALuint Source;
ALuint buffer;
ALuint frequency;
ALenum format;

//position
ALfloat sourcePos[3];
ALfloat sourceVel[3];
};

ALCcontext * Context;
ALCdevice * Device;
SoundSource sound;

int endWithError(char * msg, int error = 0)
{
cout << msg << endl;
while(cin.get() != 10);

return error;
}

bool initSound()
{
Device = alcOpenDevice((ALCchar*)"DirectSound3D");
if(Device == NULL)
return false;
else
{
Context = alcCreateContext(Device,NULL);
alcMakeContextCurrent(Context);
alGetError();
return true;
}
return false;
}

string loadSound(SoundSource s)
{
char type[4];
DWORD size, chunkSize;
short formatType, channels;
DWORD sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample;
DWORD dataSize;

FILE * fp = NULL;

fp = fopen("test.wav","rb");

fread(type, sizeof(char), 4, fp);
if(!strcmp(type, "RIFF"))
endWithError("Error: Not RIFF format");

fread(&size, sizeof(DWORD), 1, fp);

fread(type, sizeof(char), 4, fp);
if(!strcmp(type, "WAVE"))
endWithError("Error: Not WAVE format");

fread(type, sizeof(char), 4, fp);
if(!strcmp(type, "fmt "))
endWithError("Error: Not fmt format");

fread(&chunkSize, sizeof(DWORD), 1, fp);
fread(&formatType, sizeof(short), 1, fp);
fread(&channels, sizeof(short), 1, fp);
fread(&sampleRate, sizeof(DWORD), 1, fp);
fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
fread(&bytesPerSample, sizeof(short), 1, fp);
fread(&bitsPerSample, sizeof(short), 1, fp);

cout << "Chuck size: " << chunkSize << endl;
cout << "Format type: " << formatType << endl;
cout << "Channels: " << channels << endl;
cout << "Sample rate: " << sampleRate << endl;
cout << "Avg Bytes per sec: " << avgBytesPerSec << endl;
cout << "Bytes per sample: " << bytesPerSample << endl;
cout << "Bits per sample: " << bitsPerSample << endl;

fread(type, sizeof(char), 4, fp);
if(!strcmp(type, "data"))
endWithError("Error: No data");

fread(&dataSize, sizeof(DWORD), 1, fp);

unsigned char * buf;
buf = new unsigned char[dataSize];
fread(buf, sizeof(BYTE), dataSize, fp);

alGenBuffers(1, &s.buffer);
alGenSources(1, &s.Source);

switch(bitsPerSample)
{
//8 bit
case 8:
{
switch(channels)
{
case 1: s.format = AL_FORMAT_MONO8; break;
case 2: s.format = AL_FORMAT_STEREO8; break;
}
}
//16 bit
case 16:
{
switch(channels)
{
case 1: s.format = AL_FORMAT_MONO16; break;
case 2: s.format = AL_FORMAT_STEREO16; break;
}
}
}

alBufferData(s.buffer, s.format, (ALvoid *)buf, dataSize, s.frequency);
s.sourcePos[0] = 0.0;
s.sourcePos[1] = 0.0;
s.sourcePos[2] = 0.0;
s.sourceVel[0] = 0.0;
s.sourceVel[1] = 0.0;
s.sourceVel[2] = 0.0;

fclose(fp);
//delete[] buf;

return "WAVE successfully loaded!";
}

void closeSound()
{
alDeleteSources(1, &sound.Source);
alDeleteBuffers(1, &sound.buffer);
Context = alcGetCurrentContext();
Device = alcGetContextsDevice(Context);
alcMakeContextCurrent(NULL);
alcDestroyContext(Context);
alcCloseDevice(Device);
cout << "OpenAL sound closed!" << endl;
}

int main()
{
string result;

if(initSound())
{
cout << "Sound Context and Device up!" << endl;
result = loadSound(sound);
cout << result.c_str() << endl;
alSourcePlay(sound.Source);
system("PAUSE");
}
else
{
{
cout << "Sound Context and Device not made.." << endl;
system("PAUSE");
}
}

closeSound();
return 0;
}

最佳答案

// replace this 
if(!strcmp(type, "XXXX"))
endWithError("Error: Not RIFF format");
// with this
if(!memcmp(type, "XXXX", 4))
endWithError("Error: Not RIFF format");

因为类型没有\0终止

fread(&chunkSize, sizeof(DWORD), 1, fp);

提示:使用 sizeof(变量名) 而不是 sizeof(type)例如 fread(&chunkSize, sizeof(chunkSize), 1, fp);

如果您以后出于某种原因需要更改变量类型,它可能不会在您面前爆炸

fread(&dataSize, sizeof(DWORD), 1, fp);
unsigned char * buf;
buf = new unsigned char[dataSize];
fread(buf, sizeof(BYTE), dataSize, fp);

永远不要假设您读入的内容是正确的,而是在分配之前检查 dataSize 是多少。

避免使用全局变量也是一个好主意,您可以轻松地创建一个包含所需变量的结构并从函数中返回它

struct context_t // or whatever u want to call it
{
ALCcontext * Context;
ALCdevice * Device;

};

bool initSound(context_t & c) {}
void closeSound(context_t & c) {}

int main()
{
context_t context;

if (initSound(context))
{
...
}
..
closeSound(context);

关于c++ - 使用动态无符号字符数组时发生未知崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com