gpt4 book ai didi

c - 我的代码有什么问题?我得到一个 "Bus Error"

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:22 26 4
gpt4 key购买 nike

我正在尝试读取包含原始音频的文件并使用 FLAC 对其进行编码.当我运行该程序时,出现“总线错误”。有什么问题吗?
我正在使用以下行在 OS X 10.6.8 上进行编译:

gcc nsFlacEncoder.c -I/opt/local/include -lflac -m32 -o flac_enc

#include "FLAC/stream_encoder.h"
#define READSIZE 40000
char buffer[READSIZE];
FLAC__int32 pcm[READSIZE/2];

FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
{
FILE * fp;
fp = fopen("rec.flac","rw");
fwrite(buffer, 1, bytes, fp);
fclose(fp);
return 0;
}

int rawToFlac()
{
FLAC__bool ok = true;
FLAC__StreamEncoder *encoder = 0;
FLAC__StreamEncoderInitStatus init_status;
unsigned sample_rate = 16000;
unsigned channels = 1;
unsigned bps = 16;

if((encoder=FLAC__stream_encoder_new()) == NULL){
printf("Error!");
return 1;
}

ok &= FLAC__stream_encoder_set_verify(encoder, true);
ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
ok &= FLAC__stream_encoder_set_channels(encoder, channels);
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, READSIZE);

if(ok){
init_status = FLAC__stream_encoder_init_stream(encoder, &write_callback, NULL, NULL, NULL, /*client_data=*/NULL);
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK){
printf("Encoder not initiated");
return 1;
}
}

if(ok){
while(ok)
{
/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
size_t i;
for(i = 0; i < 20000; i++) {
/* inefficient but simple and works on big- or little-endian machines */
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
}
/* feed samples to encoder */
ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, 20000);
}
}

ok &= FLAC__stream_encoder_finish(encoder);
printf("Finished.");

FLAC__stream_encoder_delete(encoder);
return 0;
}

int main()
{

FILE *file;
file = fopen("recording","rb");
fread(buffer,2, 20000, file);
rawToFlac();
fclose(file);
return 0;
}

运行 gdb flac_enc 给我这个:

warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitmath.o" - no debug information available for "bitmath.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitreader.o" - no debug information available for "bitreader.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitwriter.o" - no debug information available for "bitwriter.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/cpu.o" - no debug information available for "cpu.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/crc.o" - no debug information available for "crc.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/fixed.o" - no debug information available for "fixed.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/format.o" - no debug information available for "format.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/lpc.o" - no debug information available for "lpc.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/md5.o" - no debug information available for "md5.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/memory.o" - no debug information available for "memory.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/metadata_iterators.o" - no debug information available for "metadata_iterators.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/metadata_object.o" - no debug information available for "metadata_object.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_decoder.o" - no debug information available for "stream_decoder.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_encoder.o" - no debug information available for "stream_encoder.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_encoder_framing.o" - no debug information available for "stream_encoder_framing.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/window.o" - no debug information available for "window.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_decoder_aspect.o" - no debug information available for "ogg_decoder_aspect.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_encoder_aspect.o" - no debug information available for "ogg_encoder_aspect.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_helper.o" - no debug information available for "ogg_helper.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_mapping.o" - no debug information available for "ogg_mapping.c".

这很奇怪,因为我的系统上没有用户“benski”。但我确定 FLAC 库已正确安装为 example programs完美运行。

最佳答案

main() 中,您不检查文件是否已成功打开。问题可能在于您在 fread() 操作中使用了空指针。同样,在 write_callback() 函数中,您的代码显示了无敌假设。 (此外,如果您的回调被调用不止一次,则第二次调用会覆盖第一次调用产生的数据。但是,这是一个不同的问题。)

您不检查 fread() 读取了多少个 2 字节单元。您也不检查 fwrite() 写入了多少数据 - 它是否成功?

您应该能够使用 gdb 或类似的调试器来查看错误发生的位置。

您也可以使用 valgrind 来发现问题。

您不需要同时使用 ifwhile:

if (ok)
{
while (ok)
{
...
}
}

只有循环就足够了;如果 ok 在第一个循环中为 false,它将执行零次。如果在 while 循环之后和 if 结束之前有一个语句,那么两者都是必需的。

通常,当您尝试访问未对齐的数据对象时,RISC 芯片上会发生 SIGBUS(总线错误)。目前尚不清楚哪一行可能导致此代码中的问题。尽管之前有关于“空指针”的评论,但通常以 SIGSEGV(分段违规)而不是 SIGBUS 结束。

关于c - 我的代码有什么问题?我得到一个 "Bus Error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6642150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com