Note that there are some explanatory texts on larger screens.

plurals
  1. POUnknown crash when using dynamic unsigned char array
    text
    copied!<p>Could someone explain to me why this:</p> <pre><code>unsigned char * buf; buf = new unsigned char[dataSize]; </code></pre> <p>is crashing my c++ program? It's not giving me any error's so I'm really lost as to the reason why my program is crashing due to these lines of code. Thank you in advance!</p> <p>EDIT: Here's the code for the project I'm currently working on, it's using OpenAL so you'll need it if you want to recompile the code.</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;Windows.h&gt; #include &lt;al.h&gt; #include &lt;alc.h&gt; #include &lt;vector&gt; 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 &lt;&lt; msg &lt;&lt; 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(&amp;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(&amp;chunkSize, sizeof(DWORD), 1, fp); fread(&amp;formatType, sizeof(short), 1, fp); fread(&amp;channels, sizeof(short), 1, fp); fread(&amp;sampleRate, sizeof(DWORD), 1, fp); fread(&amp;avgBytesPerSec, sizeof(DWORD), 1, fp); fread(&amp;bytesPerSample, sizeof(short), 1, fp); fread(&amp;bitsPerSample, sizeof(short), 1, fp); cout &lt;&lt; "Chuck size: " &lt;&lt; chunkSize &lt;&lt; endl; cout &lt;&lt; "Format type: " &lt;&lt; formatType &lt;&lt; endl; cout &lt;&lt; "Channels: " &lt;&lt; channels &lt;&lt; endl; cout &lt;&lt; "Sample rate: " &lt;&lt; sampleRate &lt;&lt; endl; cout &lt;&lt; "Avg Bytes per sec: " &lt;&lt; avgBytesPerSec &lt;&lt; endl; cout &lt;&lt; "Bytes per sample: " &lt;&lt; bytesPerSample &lt;&lt; endl; cout &lt;&lt; "Bits per sample: " &lt;&lt; bitsPerSample &lt;&lt; endl; fread(type, sizeof(char), 4, fp); if(!strcmp(type, "data")) endWithError("Error: No data"); fread(&amp;dataSize, sizeof(DWORD), 1, fp); unsigned char * buf; buf = new unsigned char[dataSize]; fread(buf, sizeof(BYTE), dataSize, fp); alGenBuffers(1, &amp;s.buffer); alGenSources(1, &amp;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, &amp;sound.Source); alDeleteBuffers(1, &amp;sound.buffer); Context = alcGetCurrentContext(); Device = alcGetContextsDevice(Context); alcMakeContextCurrent(NULL); alcDestroyContext(Context); alcCloseDevice(Device); cout &lt;&lt; "OpenAL sound closed!" &lt;&lt; endl; } int main() { string result; if(initSound()) { cout &lt;&lt; "Sound Context and Device up!" &lt;&lt; endl; result = loadSound(sound); cout &lt;&lt; result.c_str() &lt;&lt; endl; alSourcePlay(sound.Source); system("PAUSE"); } else { { cout &lt;&lt; "Sound Context and Device not made.." &lt;&lt; endl; system("PAUSE"); } } closeSound(); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload