第一個值之後,我越來越段故障。並且buffer
能夠正確傳遞到main()
,我能夠使用datas[i]
檢查main
中的值。我只是將printf
複製到main()
中,並且必須用*buffer[i]
替換datas[i]
,然後編譯好。它打印readWav()
中的第一個值,然後發生SegFault。ç分段錯誤,當我嘗試當我嘗試在<code>readWav()</code>功能上<code>*buffer[i]</code>使用<code>printf</code>給printf只是打印功能裏面
如何做到這一點的readWav()
- 什麼是錯的*buffer[i])
- 換句話說,如果datas[i]
是指實際值main()
那會是在readWav()
在buffer
方面是什麼?
更新: main()
將通過datas
其他功能仍需添加。這就是爲什麼我試圖在readWav()
中打印它,認爲它將如何傳遞給其他功能非常相似 - 請糾正我,如果我錯了?
#include <stdio.h>
#include "sndfile.h"
int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer);
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");
return 1;
}
// Output Info
printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
nSamples, argv[1], sRates, (float)nSamples/sRates);
for (i=0; i < nSamples ; i++) {
printf("%d\t %f\n", i, datas[i]);
}
free(datas);
return 0;
//Cleanup etc:
}
int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer)
{
// Open sound file
SF_INFO sndInfo;
if ((sRate == NULL) || (numFrames == NULL) || (buffer == NULL)) {
fprintf(stderr, "Invalid arguments passed to readWav()\n");
return 1;
}
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;
}
// Allocate memory
*buffer = malloc(sndInfo.frames * sndInfo.channels * sizeof(float));
if (*buffer == NULL) {
fprintf(stderr, "Could not allocate memory for file\n");
sf_close(sndFile);
return 1;
}
*sRate = sndInfo.samplerate;
// 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);
}
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, *sRate, (float)*numFrames/sndInfo.samplerate);
for (i=0; i < sndInfo.frames ; i++) {
printf("%d\t %f\n", i, *buffer[i]);
}
sf_close(sndFile);
return(0);
}
你必須首先解除引用:'(* buffer)[i]' – 2015-01-31 19:13:25