4
Orionss
7y

It segfaulted at malloc. Fml.

Comments
  • 1
    Must be a really large "char" ;)
  • 1
    @juneeighteen It's a string with a variable size ^^
  • 0
    Why is it segfaulting in malloc()?

    What does the stack trace say?

    And how big is the string?
  • 1
    @spl0 The bug was linked to the printf in fact, I was wrong
  • 2
    @Orionss As in forgot the \n so printf didn't flush? The correct way to do that is to check if result is NULL, then use perror() and exit if it is.
  • 1
    @Gauthier I'm not allowed to useperror
  • 1
    @Orionss That's a shame, why not? You can always fprintf(stderr, "Error: malloc failed to allocate memory for variable result\n"); better than nothing.

    But always check the return value of malloc (actually, of any function that is kind enough to tell you it failed). It feels like your code is very verbose, but crashing as close to what is causing the error as possible is invaluable!
  • 1
    @Orionss you haven't paid the license fee for perror()?
  • 1
    @Gauthier I'm not allowed to use any of the basic function of the standard library except for malloc, write, and read :p
  • 1
    @spl0 Question of school ^^
  • 1
    @Orionss Seriously wtf?? If this is a school assignment I'd leave a sarcastic comment about not being allowed to use the best tool for the job (perror)
  • 0
    Haha. Use write() instead of printf.... :)
  • 2
    @Gauthier We have to recode some functions of the standard library to make ours ^^
  • 1
    @spl0 Yep, that's to log ^^
  • 1
    I suppose it's just an academic exercise. Either way as our friend has already advised always check the return value of malloc (and everything) - even if your not allowed to use perror you still need to recognise and handle the failure.
  • 1
    @Orionss you can still use the var errno in your error message...
  • 0
    @Gauthier Yes and I did it sometimes
  • 1
    Although to be honest... you know if malloc returns NULL, it only means one thing....
  • 0
Add Comment