C ByteBeat dynamically allocated array not working [closed]

I tried to play ByteBeat in C with a dynamically allocated array, I've been searching for 3 or more days to find an answer but no luck... I tried: malloc, HeapAlloc & HeapFree, new datatype & delete.

Here is my code:

    std::cout << "Enter duration:" << std::endl;
    int duration;
    std::cin >> duration;

    HWAVEOUT hwo = 0;
    WAVEFORMATEX wfx = { WAVE_FORMAT_PCM, 1, 8000, 8000, 1, 8, 0 };
    
    waveOutOpen(&hwo, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL);

    char* buffer = (char*)malloc(8000 * duration * sizeof(char));
    
    for (int t = 0; t < sizeof(buffer); t++) {
        buffer[t] = t * (t >> 7);
    }

    WAVEHDR whdr = { (LPSTR)buffer, sizeof(buffer), 0, 0, 0, 0, 0, 0 };

    waveOutPrepareHeader(hwo, &whdr, sizeof(WAVEHDR));
    waveOutWrite(hwo, &whdr, sizeof(WAVEHDR));
    waveOutUnprepareHeader(hwo, &whdr, sizeof(WAVEHDR));
    waveOutClose(hwo);

    Sleep(1000 * duration);
    free(buffer);

When I try to make it: char buffer[8000 * 3], it works. Yes, I also did #pragma comment(lib, "winmm.lib") Please do not tell me to use constant!


This is a classic. The problem is here:

With char buffer[100], sizeof(buffer) is 100. With char *buffer = ...., sizeof(buffer) is the size of a pointer on your platform (4 or 8).

So just use WAVEHDR whdr = { (LPSTR)buffer, 8000 * duration, 0, 0, 0, 0, 0, 0 } instead of WAVEHDR whdr = { (LPSTR)buffer, sizeof(buffer), 0, 0, 0, 0, 0, 0 }.

You should learn how to use your debugger, you could have found out this yourself in a matter of minutes.