c - 函数 main 从子例程接收到不正确的值

标签 c

此代码不起作用 - 将数据从子例程传递到主例程并分配内存时出现问题。

子例程内的计算是正确的,但主程序接收到的值不正确 - 主程序中的变量具有随机值,例如 sRates。

#include <stdio.h>
#include <malloc.h>
#include "sndfile.h"

int main(int argc, char *argv[])
{
    int sRates , sRatem , ret;
    long    nSamples=0, nSamplem;   
    float   *datas, *datam;


    printf("Read Test\n");
    if (argc != 3) {
        fprintf(stderr, "Expecting two wav file as argument\n");
        return 1;
    }


    ret =  readWav(argv[1], nSamples, sRates, &datas );
    if (ret != 0) {
    printf("Error\n");
    }
    // Output Info
    printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
        nSamples, argv[1], sRates, (float)nSamples/sRates);
    printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
        nSamples, argv[1], sRates, (float)nSamples/sRates);

//  free(datas);

    return 0;
}

int  readWav(char *fname, long *numFrames, int *sRate, float **buffer  ) 
{

    // Open sound file
    SF_INFO sndInfo;
    SNDFILE *sndFile = sf_open(fname, SFM_READ, &sndInfo);
    if (sndFile == NULL) {
        fprintf(stderr, "Error reading source file '%s': %s\n", fname, sf_strerror(sndFile));
        return 1;
    }


    printf("1Format of the audio file = %i\n", sndInfo.format);
    printf("2Number of channels = %i\n", sndInfo.channels);
    printf("3Sample Rate  = %d\n", sndInfo.samplerate);
    printf("4 Sample count  = %ld\n", (long)sndInfo.frames);
    sRate= sndInfo.samplerate;

    // Allocate memory
    buffer = (float *)malloc(sndInfo.frames * sndInfo.channels * sizeof(float));
    if (buffer == NULL) {
        fprintf(stderr, "Could not allocate memory for file\n");
        sf_close(sndFile);
        return 1;
    }

      // Load data
    numFrames = sf_readf_float(sndFile, buffer, sndInfo.frames);

    // Check correct number of samples loaded
    if (numFrames != sndInfo.frames) {
        fprintf(stderr, "Did not read enough frames for source\n");
        sf_close(sndFile);
        free(buffer);
//      return 1;
    }
    else {
        printf("Successfully read file\n");
        numFrames = sndInfo.frames;
    }
    // Output Info
    printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
//      numFrames, fname, sndInfo.samplerate, (float)numFrames/sndInfo.samplerate);
        numFrames, fname, sRate, (float)numFrames/sndInfo.samplerate);


    sf_close(sndFile);
//  return(buffer);
    return(0);

}

最佳答案

在 C 中,所有参数都是按值传递的,因此如果您想要类似 by-ref 的参数,则必须传递指针。由于您想返回一个 float*,因此您需要传递一个 float**

实际上您正在传递它,但您没有正确使用它(请使用 -Wall 或等效的编译器来启用警告)。

代码应该或多或少像这样:

int  readWav(const char *fname, long *numFrames, int *sRate, float **buffer) 
{
    *buffer = malloc(...);
    //if you do not feel comfortable writing `*buffer` everywhere:
    float *data = *buffer;
    ///....
    *numFrames = sf_readf_float(...);
    ///....
    *sRate = sndInfo.samplerate;
    ///....
}

int main()
{
    long nSamples;
    int sRates;
    float *datas;
    ret =  readWav(argv[1], &nSamples, &sRates, &datas);
    //...
}

关于c - 函数 main 从子例程接收到不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205315/

相关文章:

c - 释放 char 指针时获取 SIGABRT

c - 从标准输入读取任意行数

c - 提取由空格分隔的数字的最佳方法?在 C

c - printf的错误打印

C编程: allocating memory for char array using malloc in C89

c - 高级 Unix 编程 - 第 1 章 shell 代码错误

使用集合列表创建数组

c - 编译器可以删除局部变量以支持多个内存访问吗?

c - 如何在 ubuntu 11.10 的 eclipse IDE 中编程和编译 C?我需要安装哪个插件?

c - 返回一个指针数组,返回类型?