title: Signal 11 (SIGSEGV) error while using malloc() and realloc()
body: <p>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. </p>
  
  <p>I will appreciate the help to pin-point where is the error and how do I fix it, thanks!</p>
  
  <pre><code>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 &amp;&amp; field_ptr-&gt;type) {
  1277         field_offset=field_ptr-&gt;offset*targ_char_bit;
  1278         if (field_offset &gt; current_offset) {
  1279             if(sizeof(my_str) &lt; (field_offset-current_offset)) {
  1280                 my_str = (char*)realloc(my_str, (field_offset-current_offset+1));
  1281             }
  1282             for(int i = 0; i &lt; (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) &lt; 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 &lt; 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) &lt; (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 &lt; 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-&gt;next;
  1331     }
  1332     struct_size=ptr-&gt;size*targ_char_bit;
  1333     if(struct_size &gt; current_offset) {
  1334         if(strlen(my_str) &lt; (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&lt;(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 }
  </code></pre>
  
  <p>Here is the error I am getting:</p>
  
  <pre><code>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]
  </code></pre>