Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
spl020408yWhy is it segfaulting in malloc()?
What does the stack trace say?
And how big is the string? -
@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.
-
@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! -
Orionss28868y@Gauthier I'm not allowed to use any of the basic function of the standard library except for malloc, write, and read :p
-
@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)
-
spl020408yI 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.
Related Rants
-
cdrice105"You gave us bad code! We ran it and now production is DOWN! Join this bridgeline now and help us fix this!" ...
-
MoboTheHobo36My Friend: Dude our Linux Server is not working anymore! Me: What? What did you do? My friend: Nothing I swe...
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
It segfaulted at malloc. Fml.
undefined
malloc
c
fml
segfault