为什么当我更改此代码时会给我"segmentation fault"错误?
Why it gives me "segmentation fault" error when I change this code?
我可以不用任何错误编译此代码:
#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
相同的方式。
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 导致Segmentation错误的结构中的矢量
- OpenGL "Segmentation fault"错误
- 如何修复访问动态数组中结构中的字符串变量时"segmentation fault (core dumped)"错误
- 如何修复 C++ 中的'Segmentation fault'错误
- 尝试读取包含 ints 并存储在 int vector 中的文件,但我不断收到错误"Segmentation fault (core dumped)"
- 为什么当我更改此代码时会给我"segmentation fault"错误?
- 尝试将文本文件加载到动态分配的 2D 数组中时出现'Segmentation fault'错误
- 由于while循环中的条件,导致Segmentation错误
- 取消引用会导致Segmentation错误
- 调用构造函数定义中的函数后收到Segmentation Fault 11错误
- 为什么我在运行程序时收到错误"segmentation fault"?
- 当我尝试从结构列表中访问内存时出现错误:" Program received signal SIGSEGV, Segmentation fault."
- 当g++静态链接pthread时,导致Segmentation错误,原因何在
- 为什么我的程序在运行后给出"segmentation fault"(核心转储)错误
- 在Qt错误显示:"Fault Module Name: Qt5Cored.dll"在win 7和调试时:"segmentation fault"在CentOS Linux和Win 7中
- 引用字符串参数导致Segmentation错误
- 为什么 g++ 在将节点指向数据字符时返回'Segmentation fault'错误?
- JNI从c调用java会出现Segmentation错误(核心转储)