Debug Assertion Failed Buffer!= NULL

时间:2014-08-14 05:24:08

标签: c++ c visual-studio-2013 wav

您好,我有一个大学项目,可以阅读任何语言的Wav文件,所以我选择了C / C ++我认为我做对了一件事我只是做了一件事 c项目我总是得到这个msg“Buffer!= NULL”

完整代码:

int main (){
    //char id[10];
    FILE *wav;
    wav=fopen("sound.wav","rb");

    if(wav){
        BYTE wav_id[5], *sound_buffer;      
        DWORD size;

        short format_tag, channels, block_align, bits_per_sample;
        DWORD format_length, sample_rate, avg_bytes_sec,datasize,i;

        fread(&wav_id,sizeof(BYTE),4,wav);
        wav_id[sizeof(BYTE)+3]=0;   

        if (!memcmp(&wav_id,"RIFF",4)) {
            fread(&size,sizeof(DWORD),1,wav);
            fread(&wav_id,sizeof(BYTE),4,wav); 
            if (!memcmp(&wav_id,"WAVE",4)) { //this is probably a wave file since it contained "WAVE" 

                fread(&wav_id,sizeof(BYTE),4,wav); //read in 4 bytes "fmt "; 
                fread(&format_length,sizeof(DWORD),1,wav);
                fread(&format_tag, sizeof(short), 1, wav); 
                fread(&channels, sizeof(short),1,wav); 
                fread(&sample_rate, sizeof(DWORD), 1, wav); 
                fread(&avg_bytes_sec, sizeof(short), 1, wav); 
                fread(&block_align, sizeof(short), 1, wav); 
                fread(&bits_per_sample, sizeof(short), 1, wav); 
                fread(&wav_id, sizeof(BYTE), 4, wav); //read in 'data' 
                fread(&datasize, sizeof(DWORD), 1, wav); //how many bytes of sound data we 
                sound_buffer=(BYTE *)malloc(sizeof(BYTE) * datasize);
                fread(sound_buffer, sizeof(BYTE), datasize, wav);  // i trace the problem to this line the whole above code seems to run smooth 
            }
            cout << "\n The file is Riff but it's not a wav file"<<endl;
        }
        else {
            cout <<"\nNOT a Wav File\n"<<endl;}

        }
    else {
        cout <<"\nFile wasn't opened\n"<<endl;
    }

    system("pause");
}

3 个答案:

答案 0 :(得分:1)

如您的错误显示 -

Error 2 error C2664: 'strcmp' : cannot convert parameter 1 from 'BYTE [5]' to 'const char *'

您无法将BYTE [5]数据类型传递给字符串比较功能。它对原型无效。语法是 -

int strcmp(const char *s1, const char *s2);

你的两个参数都必须是字符串!

但您将wav_id传递给strcmp。我认为这不是一个字符串 -

strcmp(wav_id,"RIFF") // problem 
strcmp(wav_id,"WAVE") // problem

否则,您可以使用memcmp来比较内存位置。原型是 -

int memcmp(const void *s1, const void *s2, size_t n);

两个参数都是void *,所以你可以传递任何类型的指针这个函数!

尝试以下更改 -

if (!memcmp(&wav_id,"RIFF",4)) {
    fread(&size,sizeof(DWORD),1,wav);
    fread(&wav_id,sizeof(BYTE),4,wav); 
    if (!memcmp(&wav_id,"WAVE",4)) 
... }

答案 1 :(得分:0)

您可以使用memcmp

但是对于c ++,我会使用这样的

 template <char a, char b, char c, char d>
    struct  NUMBER { 
        enum {
            value = a +
            (static_cast<uint32_t> (b) << 8) +
            (static_cast<uint32_t> (c) << 16) +
            (static_cast<uint32_t> (d) << 24)        };
    };

...

        uint32_t  data;
        fread(&data,sizeof(uint32_t),1,wav)

        if ( data !=  NUMBER<'R', 'I', 'F', 'F'>::value) //  "RIFF" 
            return -1; //Not RIFF 

答案 2 :(得分:0)

将WAVE音频ID视为整数更容易。说uint32_t。然后您可以查阅ASCII表来定义一些常量:

const uint32_t RIFF_ID = 0x46464952;

记住WAVE音频是一种小端格式。

然后,您可以直接阅读uint32_t个变量,并与==进行简单比较。


关于另一个问题,fread中的断言失败,很可能datasize是一个非常大的值,对malloc的调用失败,返回NULL。当您将NULL作为fread的缓冲区传递时,CRT的调试版本会检测到该问题。添加一些错误检查。如果不检查错误的返回值,则调用malloc是错误的。