A piece of C code was creating core dump.
When i simply added a few fprintf statement and printed the value of variales used in code to a file, surprisingly the code ran successfully.
I tried the scenario number of times and the result was same "when fprintf was present no core dump and when absent core dump"
Please note that the input was same in both case.

What could be the reason?
Can it be due to compiler optimization?
I am using gcc compiler and the platform is linux.

could you post that code so we may see

you reference wrong address, this causes core dump yes ?
you add fprintf and maybe initialise some memmory then reference it by mistake, this could be answer m ? check address you reference, and addresses you allocate with memmory

Note that printing is often a slower process compared to in-memory computations. It is entirely possible that you have a race condition somewhere that is avoided simply by printing. Are you using threads?
However, without any example of your code it is hard to make any sound judgement. So, as zeroliken suggests, please post some relevant section of your code.

Actually what happens is we get error in realloc function written in gfunc_Realloc Function in line 174
And below the clled function is the caller function where above function is called from line number 511.
Here I checked save_msg is allocated with gfunc_memAlloc function first time and then second time onwards it is being reallocated through below called function.
So don’t see any mistake.
Also please note that when I added few fprinf to the caller function and printed the value of "n", "k","inputMsgStr","message","save_msg" in a file the core dump didn't come. I tried the scenario number of times and result was same.

// **Called Function**

161 void *gfunc_Realloc (void *i_oldObject_p, size_t i_objectSize)
162 {
163 /* Local variable *\
164 \* -------------- */
165
166 void *newObject_p;
167
168 /* Code *\
169 \* ---- */
170
171     if(NULL == i_oldObject_p) {
172                 newObject_p = gfunc_memAlloc(i_objectSize);
173     } else {
174           if (NULL == (newObject_p =
175                       realloc (i_oldObject_p,i_objectSize))) {
176         printf("memory reallocation error\n");
177         exit(1);
178         }
179     }
180     return newObject_p;
181 }

// Caller Fuction 
static char *gfunc_buildMessage(int   i_line,
425                               char  *i_field,
426                               int   i_sev,
427                               char  *i_codeName,
428                               int   format_message_flag,
429                               char  *i_string_p)
430 {
431
432 /* Local Variables *\
433 \*-----------------*/
434
435 int start=1;
436 int n=0;
437 int k;
438 char *inputMsgStr;
439 char message[FIELD_MESSAGE_SIZE+1];
440 char *save_msg = NULL;
441 char sev;
442
443 /* Code *\
444 \*------*/
445
446    switch (i_sev) {
447       case MSG_WARNING :    sev = 'W';
448           break;
449       case MSG_FATAL   :
450       case MSG_ERROR   :     sev = 'S';
451           break;
452       case MSG_INFORMATION : sev = 'I';
453           break;
454    }
455
456    inputMsgStr = (char *)gfunc_memAlloc(strlen(i_string_p)+1);
457    strcpy(inputMsgStr,i_string_p);
458
459    for (k=0;k<strlen(inputMsgStr);k++)  {
460       /* replace all '\n' by blanks */
461
462       if (inputMsgStr[k]=='\n')
463          inputMsgStr[k]=' ';
464    }
465    ppreg_STR_compressBlanks(inputMsgStr,inputMsgStr);
466
467    while (strlen(inputMsgStr+n) && rbe_notBlankStr(inputMsgStr+n))  {
468
469       for (;*(inputMsgStr+n)== ' ';n++);
470
471       if (strlen(inputMsgStr+n) > FIELD_MESSAGE_SIZE) {
472
473          for (k=FIELD_MESSAGE_SIZE; k>0 &&
474               *(inputMsgStr+n+k) != ' ' &&
475               *(inputMsgStr+n+k) != ',' &&
476               *(inputMsgStr+n+k) != ';' &&
477               *(inputMsgStr+n+k) != '(' &&
478               *(inputMsgStr+n+k) != ')' &&
479               *(inputMsgStr+n+k) != ':';k--);
480
481
482          if (!k)  {
483             k=FIELD_MESSAGE_SIZE;
484          } else if (k<FIELD_MESSAGE_SIZE){
485             k++;
486          }
487
488       } else  {
489          k=strlen(inputMsgStr+n);
490       }
491
492       strncpy(message,inputMsgStr+n,k);
493       message[k]='\0';
494       n+=k;
495
496       if (start)  {
497
498          save_msg = (char *)gfunc_memAlloc(80+1+1);
499
500          sprintf(save_msg,
501                  "%-*d %-*.*s  %c  %-*.*s  %-*.*s\n",
502                  LineLen,i_line,
503                  FieldLen,FieldLen,(format_message_flag?"":i_field),
504                  sev,
505                  CodeLen,CodeLen,(i_codeName?i_codeName:""),
506                  FIELD_MESSAGE_SIZE,FIELD_MESSAGE_SIZE,message);
507
508          start = 0;
509       } else  {
510
511          save_msg = (char *)gfunc_Realloc(save_msg,
512                                               strlen(save_msg)+80+1);
513
514          sprintf(save_msg + strlen(save_msg),
515                  "%-*.*s%-*.*s\n",
516                  80-FIELD_MESSAGE_SIZE,80-FIELD_MESSAGE_SIZE,"",
517                  FIELD_MESSAGE_SIZE,FIELD_MESSAGE_SIZE,message);
518
519       }
520    }
521    free(inputMsgStr);
522
523    return(save_msg);
524 }

I missed 1 more point that the above code works fine on HP platform and fails only on Linux.

Does gfunc_Realloc() work correctly elsewhere? Have you tried writing a simple test program to check if the gfunc_Realloc() function behaves correctly in general (that is to say, when given some test data, it works correctly)? For that matter, are the gfunc_Realloc() and gfunc_memAlloc() in a separate compiled source file, or is it all once piece? Testing it would be much easier if they were already separate, but it should be easy enough to just copy-and-paste the necessary functions into the test program.

Also, have you checked to see if replacing gfunc_Realloc() with a direct call to realloc() works or crashes? If it crashes, does adding the fprintf() calls correct it again?

Yes i tried it elsewhere and it worked fine.
Even in case of core dump it worked fine for 1st three call to it in while loop where the size passed was 162,243,324 respectively. But on 4th try when size is 405 it failed.
Please note that i tried it by providing enough of memory space... still it failed.
But worked with fprinf without changing any part of code.

the problem is that your code is impossible to copy cause of line numbers.
try to use malloc

#include "stdio.h"
#include "stdlib.h"
 void *gfunc_Realloc (void *i_oldObject_p, unsigned long i_objectSize)
 {
 /* Local variable *\
 \* -------------- */

 void *newObject_p;

 /* Code *\
 \* ---- */

 if(NULL == i_oldObject_p) {
 newObject_p = malloc(i_objectSize);
 } else {
 if (NULL == (newObject_p =
 realloc (i_oldObject_p,i_objectSize))) {
 printf("memory reallocation error\n");
 exit(1);
 }
 }
 return newObject_p;
}

int main()
{
   char *pcBuffer = NULL;
   while(1)
   gfunc_Realloc(pcBuffer, 500);
   return 0;
}

Thanks Sokurneko... But i wanted the root cause to be found.
Hence put it here. :)

Actually what happens is we get error in realloc function written in gfunc_Realloc Function in line 174

As a proof of concept, replace realloc() with the corresponding free(), malloc(), and memcpy() process. I'm willing to bet the error you get is happening in free(), which means you've corrupted the memory somehow. This corruption typically occurs by writing beyond the bounds of requested memory from a previous allocation. Very often these areas are specially allocated by the memory manager and hold bookkeeping information that will be used by free().

That might point out the problem, but it certainly doesn't locate the root cause. For that you need to do some serious delving into your code to see where the pointer is being used inappropriately.

Thanks deceptikon....
But no Luck yet...
It appears to me as well a memory corruption issue. But all shorts of activity is leading to a dead end. Heisenbug might be an option but we don't have any evidence.:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.