简体   繁体   中英

Signal 11 (SIGSEGV) error while using malloc() and realloc()

Following is the piece of code where I am getting Signal 11 error while allocation of memory for the string which I want to return.

I will appreciate the help to pin-point where is the error and how do I fix it, thanks!

1265 static string encode_func(a_type_ptr ptr)
1266 {
1267   char *my_str=(char *)malloc(512);
//some variable declarations
 1275   int no_of_array_elements;
1276     while (field_ptr && field_ptr->type) {
1277         field_offset=field_ptr->offset*targ_char_bit;
1278         if (field_offset > current_offset) {
1279             if(sizeof(my_str) < (field_offset-current_offset)) {
1280                 my_str = (char*)realloc(my_str, (field_offset-current_offset+1));
1281             }
1282             for(int i = 0; i < (field_offset-current_offset); i ++) {
1283                 strcat(my_str, "X");
1284             }
1285         }
    //some condition check not relevant to memalloc'ed variables
1299         field_bit_size=get_field_bit_size(field_ptr);
1300         no_of_array_elements=field_bit_size/base_size;
1301         if (uptr != NULL) {
1302             string tmp = encode_func(uptr);
1303             char *sub_my_str = new char[tmp.size()];
1304             strncpy(sub_my_str, tmp.c_str(), sizeof(tmp));
1305             if(strlen(my_str) < strlen(sub_my_str)) {
1306                 char *tmp= (char *)realloc(my_str,((strlen(my_str)+no_of_array_elements*strlen(sub_my_str))+1));
1307                 if(tmp != NULL)
1308                     my_str = tmp;
1309             }
1310             strncat(my_str, sub_my_str, sizeof(sub_my_str));
1311             for(int i = 1; i < no_of_array_elements; i++) {
1312                 strncat(my_str, sub_my_str,sizeof(sub_my_str));
1313             }
1314         } else {
1315             char str[25];
1316             sprintf(str, "%ldo",  base_size);
1317             if(strlen(my_str) < (no_of_array_elements*strlen(str)))
1318             {
1319                 char *tmp = (char *)realloc(my_str,strlen(my_str)+no_of_array_elements*strlen(str)+1);
1320                 if(tmp!=NULL)
1321                     my_str =  tmp;
1322             }
1323             strncat(my_str,   str,strlen(str));
1324             for(int i = 1;  i < no_of_array_elements;   i++)
1325             {
1326                 strncat(my_str, str,strlen(str));
1327             }
1328         }
1329         current_offset=field_offset+field_bit_size;
1330         field_ptr=field_ptr->next;
1331     }
1332     struct_size=ptr->size*targ_char_bit;
1333     if(struct_size > current_offset) {
1334         if(strlen(my_str) < (struct_size-current_offset)) {
1335             char *tmp = (char *)realloc(my_str, (struct_size-current_offset+1));
1336             if(tmp != NULL)
1337                 my_str = tmp;
1338         }
1339         for(int i=0; i<(struct_size-current_offset); i++) {
1340             strcat(my_str, "X");
1341         }
1342     }
1343     my_str[strlen(my_str)] = '\0';
1344     string str_enc = string(my_str);
1345     return str_enc;
1346 }

Here is the error I am getting:

Signal 11, code 1 (address not mapped to object)
(0)  0x4000000003039b00  term_on_signal + 0xa90 at host_envir.c:2129[./test]
(1)  0xe00000010d0028e0  ---- Signal 11 (SIGSEGV) delivered ----
(2)  0xc00000000023db70  real_malloc + 0x670 at ../../../../../core/libs/libc/shared_em_64/../core/gen/malloc.c:2748 [/usr/lib/hpux64/+++libc.so.1]
(3)  0xc00000000023cc80  _malloc + 0x140 at ../../../../../core/libs/libc/shared_em_64/../core/gen/malloc.c:1863 [/usr/lib/hpux64/libc+++.so.1]
(4)  0xc0000000002484b0  malloc + 0x140 at ../../../../../core/libs/libc/shared_em_64/../core/gen/malloc.c:5069 [/usr/lib/hpux64/libc.+++so.1]
(5)  0x40000000037de6d0  _Z11encode_funcP6a_type + 0x70 at test.C:1267 [./test]
(6)  0x40000000037de9e0  _Z11encode_funcP6a_type + 0x380 at test.C:1303 [./test]
(7)  0x40000000037de9e0  _Z11encode_funcP6a_type + 0x380 at test.C:1303 [./test]

该程序的解决方案是使用valgrind工具。

As bmargulies suggested, use Valgrind. If you are on OS which does not support valgrind, I would suggest Purify, I don't believe there are any free solutions for non-Linux platforms. If you can reproduce same issue on Windows, sprinkle your code with calls to _CrtCheckMemory, that will narrow down the problem - this isn't as good as Purify on Windows, but it's free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM