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

时间:2021-07-16 04:14:29

Contextualizing, after compiling my code I'm receiving a SEGV signal, which is related to unauthorized access of memory. Given the source and that the code was working before recent changes. Why am I receiving this signal?

在编译我的代码后,我正在接收SEGV信号,这与未经授权的内存访问有关。鉴于源代码和代码在最近的更改之前工作。我为什么收到这个信号?

Note: Motif's type return explained

注意:Motif的类型返回说明

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).

返回指向TextField小部件的字符串值的字符指针。此返回值是XmNvalue资源值的副本。如果TextField小部件的字符串的长度为0(零),则返回空字符串。

Declaration:

char *str, *str1, *str2;

Previous initialization:

str2 = XmTextFieldGetString( WIDGET_WITH_STRING ); 

Current initialization:

str2[0] = "Group Definition";

Exception:

(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";

2 个解决方案

#1


4  

str2 is not initialized at all in your code, so it can have any random value. And de-referencing it may cause segfault.

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

Also str2[0] is char while you are assigning it a 'char *' "Group Definition", which is not correct. You want str2 = "Group Definition"; ?

当你为它指定一个'char *'“Group Definition”时,str2 [0]也是char,这是不正确的。你想要str2 =“组定义”; ?

#2


1  

With str2 declared as char *str2; you just need: str2 = "Group Definition"

将str2声明为char * str2;你只需要:str2 =“组定义”

#1


4  

str2 is not initialized at all in your code, so it can have any random value. And de-referencing it may cause segfault.

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

Also str2[0] is char while you are assigning it a 'char *' "Group Definition", which is not correct. You want str2 = "Group Definition"; ?

当你为它指定一个'char *'“Group Definition”时,str2 [0]也是char,这是不正确的。你想要str2 =“组定义”; ?

#2


1  

With str2 declared as char *str2; you just need: str2 = "Group Definition"

将str2声明为char * str2;你只需要:str2 =“组定义”