c++ - 更改此代码后,为什么会给我 “segmentation fault”错误?

标签 c++ error-handling 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()函数中?怎么样?

(Add more text to pass add more information needed to post error) (Add more text to pass add more information needed to post error) (Add more text to pass add more information needed to post error) (Add more text to pass add more information needed to post error)

最佳答案

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

espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;

您需要编辑代码,并确保所有变量均已正确初始化。例如,如果它们是POD,则使用memset,与对voice的处理方法相同。

关于c++ - 更改此代码后,为什么会给我 “segmentation fault”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47555523/

相关文章:

powershell - 如何从 Remove-Item 中捕获错误并发出警告?

c - 不可能的事情发生了!这是什么意思?

c - 使用 MPI_Bcast 时出现 MPI 段错误

c++ - 电话拨号程序将字母转换为卡片套装而不是数字

validation - paper-input : different error-messages (w and w/o auto-validate)

c++ - 从字符串类型到类类型的隐式转换

string - 将字母放入字符串时为什么会出错?

c - 在 c 中释放 char 指针会导致段错误

c++ - 生成随机字符串并不总是返回正确的长度

c++ - 指向 vector 元素的指针