Voici le code dans lequel j'obtiens une erreur Signal 11 lors de l'allocation de mémoire pour la chaîne que je veux retourner.Erreur signal 11 (SIGSEGV) lors de l'utilisation malloc() et realloc()
J'apprécierai l'aide pour localiser où est l'erreur et comment puis-je la réparer, merci!
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 }
Voici l'erreur que je reçois:
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]
Est-ce c ou C++? Pourquoi l'avez-vous tagué tous les deux? –
Symbled symboles signifie C++, non? Pourquoi le tag C? –
c'est un fichier C++, mais le code est en c (où erreur est) – Raj