为什么当我更改此代码时会给我"segmentation fault"错误?



我可以不用任何错误编译此代码:

#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>

espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;

char Voice[] = {"English"};

char text[30] = {"this is a english test"};
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;


int main(int argc, char* argv[] ) 
{
    output = AUDIO_OUTPUT_PLAYBACK;
    int I, Run = 1, L;    
    espeak_Initialize(output, Buflength, path, Options ); 
    espeak_SetVoiceByName(Voice);
    const char *langNativeString = "en"; //Default to US English
    espeak_VOICE voice;
    memset(&voice, 0, sizeof(espeak_VOICE)); // Zero out the voice first
    voice.languages = langNativeString;
    voice.name = "US";
    voice.variant = 2;
    voice.gender = 1;
//  espeak_SetVoiceByProperties(&voice);
    Size = strlen(text)+1;    
    espeak_Synth( text, Size, position, position_type, end_position, flags,
    unique_identifier, user_data );
    espeak_Synchronize( );
    return 0;
}

尝试编译此代码时,我会遇到segmentation fault错误(将所有代码放入man()

   #include <string.h>
    #include <malloc.h>
    #include <espeak/speak_lib.h>
    int main(){
    espeak_POSITION_TYPE position_type;
    espeak_AUDIO_OUTPUT output;
    char *path=NULL;
    int Buflength = 500, Options=0;
    void* user_data;
    t_espeak_callback *SynthCallback;
    espeak_PARAMETER Parm;
    char Voice[] = {"English"};
    unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
        output = AUDIO_OUTPUT_PLAYBACK;
        int I, Run = 1, L;    
        espeak_Initialize(output, Buflength, path, Options ); 
        espeak_SetVoiceByName(Voice);
        const char *langNativeString = "en"; //Default to US English
        espeak_VOICE voice;
            memset(&voice, 0, sizeof(espeak_VOICE)); // Zero out the voice first
            voice.languages = langNativeString;
            voice.name = "US";
            voice.variant = 2;
            voice.gender = 1;
            //espeak_SetVoiceByProperties(&voice);
        char tx[]="hi there, my name is Eliyaas, what's your name?";
        espeak_Synth( tx, strlen(tx)+1, position, position_type, end_position, flags,unique_identifier, user_data );
        espeak_Synchronize( );

    return 0;}

有什么区别,哪一行导致此错误?是否可以将所有这些程序都放入main()功能中?如何?

(添加更多文本以通过添加更多发布错误所需的信息)(添加 更多文本可以添加发布错误所需的更多信息)(添加更多信息 文本要添加发布错误所需的更多信息)(添加更多文本 通过添加发布错误所需的更多信息)

全局变量在C 中初始化为零,局部变量不是,并且不确定的行为可以从中读取。例如,您的代码中有很多它们:

espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;

您需要编辑代码,并确保所有变量均已正确初始化。例如,如果memset是POD,则与voice相同的方式。

相关内容

  • 没有找到相关文章

最新更新