使用指针时出现分段错误:没有这样的文件或目录

时间:2016-04-05 20:24:19

标签: c pointers

我正在使用musly开展音乐相似性项目。我对指针不是很熟悉。下面的代码给出了一个分段错误,GDB说no such file or directory

musly_track_analyze_audiofile(mj,"2.wav",0,track+1);
*(tracks+1)=track+1;

以上部分导致错误。请注意,仅当我尝试分析第二个文件(并且目录中的2.wav )时才会出现此错误。

    Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd5e3e in musly_track_analyze_audiofile (jukebox=0x6211f0, 
    audiofile=0x400cba "2.wav", max_seconds=0, track=0x6029e4)
    at /home/ananthakrishnan/work/musly-0.1/libmusly/lib.cpp:354
354 /home/ananthakrishnan/work/musly-0.1/libmusly/lib.cpp: No such file or directory.

完整的计划如下。我认为错误是由指针造成的。

main()
{
musly_jukebox* mj ;
musly_track* track,track2;
musly_track** tracks;
musly_trackid* trackids;
float* similarities;
//const char *j;
int chk,i; 
track=(musly_track *) malloc(5*sizeof(musly_track));
trackids=(musly_trackid *) malloc(5*sizeof(musly_track));
tracks=(musly_track **)malloc(5*sizeof(track));

musly_debug(4);     
mj = musly_jukebox_poweron(NULL,NULL);

musly_track_analyze_audiofile(mj,"1.wav",0,track);
*(tracks)=track;
musly_track_analyze_audiofile(mj,"2.wav",0,track+1);
*(tracks+1)=track+1;
musly_track_analyze_audiofile(mj,"a.wav",0,track+2);
*(tracks+2)=track+2;
musly_jukebox_addtracks(mj,tracks,trackids,1);

musly_jukebox_setmusicstyle(mj,tracks,0);



musly_jukebox_similarity(mj,tracks[2],3,tracks,trackids,0,similarities);


printf("\n%f",similarities);

//musly_jukekox_poweroff(mj);
printf("\n");
}

2 个答案:

答案 0 :(得分:3)

你不能只是

track=(musly_track *) malloc(5*sizeof(musly_track));

track对象的大小取决于很多因素。请改用musly_track_alloc

答案 1 :(得分:0)

在上述评论和答案的帮助下,我顺利执行了该程序。 以下是相似性检查的完整更正代码。希望这能帮助那些与你一起工作的人。

#include <musly/musly.h>
#include<stdio.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
    main()
    {
    musly_jukebox* mj ;
    musly_track* track;
    musly_track* trac[10];
    musly_track* track2;
    //musly_track** tracks;
    //musly_trackid* trackids;
    int trackids[10];
    float similarities[1000];
    //const char *j;
    int chk,i; 

    musly_debug(4);     
    mj = musly_jukebox_poweron(NULL,NULL);
    track=musly_track_alloc(mj);
    for(i=0;i<5;i++)
        trac[i]=musly_track_alloc(mj);
    musly_track_analyze_audiofile(mj,"1.wav",0,trac[0]);

    musly_track_analyze_audiofile(mj,"2.wav",0,trac[1]);
    musly_track_analyze_audiofile(mj,"a.wav",0,trac[4]);
    musly_track_analyze_audiofile(mj,"b.wav",0,trac[2]);
    musly_track_analyze_audiofile(mj,"1.wav",0,trac[3]);
    //tracks=&trac;
    musly_jukebox_addtracks(mj,trac,trackids,3);
    musly_jukebox_setmusicstyle(mj,trac,3);
    chk = musly_jukebox_similarity(mj,trac[3],3,trac,trackids,5,similarities);
    for(i=0;i<5;i++)
        printf("\n%f",similarities[i]);

    //musly_jukekox_poweroff(mj);
    printf("\nchk=%d\n",chk);
    musly_track_alloc(mj);
    }