为什么我会收到 SEGV 信号 (C)?

Why am I receiving a SEGV signal (C)?

上下文化,在编译我的代码后,我收到了一个 SEGV 信号,这与未经授权的内存访问有关。考虑到源代码并且代码在最近的更改之前可以正常工作。为什么我会收到这个信号?

Note: Motif's type return explained

Returns a character pointer to the string value of the TextField widget. This returned value is a copy of the value of the XmNvalue resource. Returns an empty string if the length of the TextField widget’s string is 0 (zero).

声明:

char *str, *str1, *str2;

之前的初始化:

str2 = XmTextFieldGetString( WIDGET_WITH_STRING ); 

当前初始化:

str2[0] = "Group Definition";

异常:

(dbx) cont      
dbx: warning: Resuming only t@1 to satisfy events that require
automatic single-stepping

trace:    814     if (!add_button_sensitive)
trace:    817     str = XmTextFieldGetString( region_code_text );
trace:    818     str1 = XmTextFieldGetString( region_name_text );
trace:    823     str2[0] = "Group Definition";
t@1 (l@1) signal SEGV (access to address exceeded protections) in      
region_add_mod_cb at line 823 in file "region_groups.c"
  823     str2[0] = "Group Definition";

str2 在您的代码中根本没有初始化,因此它可以有任何随机值。取消引用它可能会导致段错误。

此外,str2[0]char,而您将其分配为 'char *' "Group Definition",这是不正确的。你想要 str2 = "Group Definition"; ?

str2 声明为 字符*str2; 您只需: str2 = "Group Definition"