gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:11:35 27 4
gpt4 key购买 nike

我可以编译此代码而不会出现任何错误:

#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/

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