2

I saw a lot forum with this ask, but in all answers it was because they don't verify the returned pointer. But in my case, I'm working on embedded system (so no Valgrind or Gdb), and I debug with printf… So there where my program make a segfault :

void myfunction(…)
{
    CAM_t *n = NULL;
    /* Some code */
    printf("before calloc\n");
    n = calloc(1, sizeof *n);
    printf("calloced\n");
    /* Rest of code */
}

When I run it I've got :

before calloc
Segmentation fault

Does anyone have a suggestion in why I've this fault ?

Thanks for help !

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 8
    Probably because something you did earlier in the program corrupted the heap. – Sneftel Apr 01 '15 at 08:38
  • 4
    Does your embedded system support `calloc`? Does a minimal program like `int main() { free(calloc(1, 1)); }` compile and work as expected? – Kerrek SB Apr 01 '15 at 08:38
  • How have you done your definition for `CAM_t`? – ha9u63a7 Apr 01 '15 at 08:40
  • Thanks for the reponse ! I've tried the simple main you give, and it compile and run without errors. – Bourdy Emilien Apr 01 '15 at 08:44
  • But ! your answer make me try something : just make a `calloc(1, 1)`… And it works ! that make no sense to me because after I need some "sub-structures" of the CAM_t and he make no segmentation fault and all my CAMs have the parameters filled… Bref ! Thanks a lot for your answers. (And yes my CAM_t have his definition, it was made by asn1c ;-) ) – Bourdy Emilien Apr 01 '15 at 08:48
  • Can you please show the definition for `CAM_t`? Hope it's not a `typedef` of a pointer. – Sourav Ghosh Apr 01 '15 at 08:56
  • @BourdyEmilien Please do not put `[SOLVED]` or like in question title. – Sourav Ghosh Apr 01 '15 at 09:00
  • Try to debug your program by porting most of it to Linux and use `valgrind` – Basile Starynkevitch Apr 01 '15 at 09:02
  • There's the CAM_t definition : `typedef struct CAM{ ItsPduHeader_t header; CoopAwareness_t cam; asn_struct_ctx_t _asn_ctx;} CAM_t;`. And of course `header` and `cam` are made of lot of structures of structures, with some pointers to these (when they are optional in the ASN.1). – Bourdy Emilien Apr 01 '15 at 09:47
  • If debugging with printf, ensure you disabled buffering, i.e. issue a call to `setbuf(stdout, NULL)`. I doubt that it's the calloc call that segfaults, more likely something after it. You just don't see the last printf since it's still buffered... – grasbueschel Apr 01 '15 at 10:36
  • Add `fflush(stdout);` after each call to `printf()` to ensure the message is displayed synchronously, before execution continues. The crash may occur after you return from `myfunction` and the message may still linger in the `stdout` buffer. On your system `stdout` might not be line buffered. – chqrlie Apr 01 '15 at 13:43
  • So ! After some strange manipulation (commenting some parts etc.), I finaly found an error with a strcpy (that I don't need anymore) after delete it it works fine. Thanks for all your reply ! – Bourdy Emilien Apr 01 '15 at 15:07

0 Answers0