誰かが私になぜこれを説明できますか:
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;
}