我可以编译此代码而不会出现任何错误:
#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()
函数中?怎么样?
(添加更多要传递的文字,添加发布错误所需的更多信息)(添加 更多要传递的文字添加发布错误所需的更多信息)(添加更多 要传递的文本添加发布错误所需的更多信息)(添加更多文本 传递添加发布错误所需的更多信息)
答案 0 :(得分:2)
全局变量在C ++中初始化为零,局部变量不是,并且从它们读取是未定义的行为。您的代码中有很多,例如:
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
您需要编辑代码并确保所有变量都已正确初始化。例如memset
如果它们是POD,则与voice
的方式相同。