Click here to Skip to main content
15,887,083 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
k505420-Dec-22 7:52
mvek505420-Dec-22 7:52 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
jmaida20-Dec-22 10:13
jmaida20-Dec-22 10:13 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
Bruno van Dooren20-Dec-22 11:08
mvaBruno van Dooren20-Dec-22 11:08 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
jmaida20-Dec-22 12:00
jmaida20-Dec-22 12:00 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
megaadam20-Dec-22 0:09
professionalmegaadam20-Dec-22 0:09 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
Daniel Pfeffer20-Dec-22 6:37
professionalDaniel Pfeffer20-Dec-22 6:37 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
megaadam20-Dec-22 6:55
professionalmegaadam20-Dec-22 6:55 
GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
englebart21-Dec-22 5:15
professionalenglebart21-Dec-22 5:15 
We like to create wrappers for most standard APIs. Mostly to make it easier to swap implementations without impacting application code.

Consider a higher level allocation concept where you

MyThing* next = allocMyThing(…);

You could have a static (or dynamic) MyThing that covers the case correctly and cleanly where the malloc(0) case crops up.

Different types will need different implementations to correctly avoid malloc(0).

IF you stick with allowing malloc(0), try some more band aids…

If running in test:
it might be best to assert and crash the program. What condition is trying to malloc(0)? Fix it.

For non-test:
It seems like your wrapper method could check for 0 size and return a (global?) static pointer to a zeroed block of memory.

You would get a safe pointer back, but like other posters have pointed out, you should never really dereference a pointer to nothing. If this is cast to char*, you would have a in effect a zero length string.

If you use a global static, the caller could check against the returned value for an exact pointer match, but again it seems it would be best to never call malloc(0).

If it is new code, avoid the malloc(0) calls.

If this legacy code, good luck!

Edit
If you use the static, your wrapper for free() will need to check for a match and skip the actual call to free().

If you wrap malloc, you need to wrap the inverse API as well!

GeneralRe: malloc and calloc revisited - oh hum but pay attention when using them Pin
jmaida21-Dec-22 14:06
jmaida21-Dec-22 14:06 
GeneralWordle 549 Pin
StarNamer@work19-Dec-22 13:37
professionalStarNamer@work19-Dec-22 13:37 
GeneralRe: Wordle 549 Pin
Amarnath S19-Dec-22 13:44
professionalAmarnath S19-Dec-22 13:44 
GeneralRe: Wordle 549 Pin
Shane010319-Dec-22 16:48
Shane010319-Dec-22 16:48 
GeneralRe: Wordle 549 Pin
Peter_in_278019-Dec-22 18:04
professionalPeter_in_278019-Dec-22 18:04 
GeneralRe: Wordle 549 Pin
Sander Rossel19-Dec-22 19:32
professionalSander Rossel19-Dec-22 19:32 
GeneralRe: Wordle 549 Pin
den2k8819-Dec-22 21:02
professionalden2k8819-Dec-22 21:02 
GeneralRe: Wordle 549 2/6 Pin
jmaida20-Dec-22 12:32
jmaida20-Dec-22 12:32 
GeneralRe: Wordle 549 Pin
OriginalGriff19-Dec-22 21:07
mveOriginalGriff19-Dec-22 21:07 
GeneralRe: Wordle 549 Pin
pkfox19-Dec-22 21:30
professionalpkfox19-Dec-22 21:30 
GeneralRe: Wordle 549 Pin
OriginalGriff19-Dec-22 21:48
mveOriginalGriff19-Dec-22 21:48 
GeneralRe: Wordle 549 Pin
pkfox19-Dec-22 21:58
professionalpkfox19-Dec-22 21:58 
GeneralRe: Wordle 549 Pin
pkfox19-Dec-22 21:29
professionalpkfox19-Dec-22 21:29 
GeneralRe: Wordle 549 Pin
ChandraRam19-Dec-22 21:53
ChandraRam19-Dec-22 21:53 
GeneralRe: Wordle 549 (3/6) Pin
musefan20-Dec-22 4:00
musefan20-Dec-22 4:00 
GeneralRe: Wordle 549 Pin
Sandeep Mewara20-Dec-22 5:49
mveSandeep Mewara20-Dec-22 5:49 
GeneralRe: Wordle 549 Pin
Cp-Coder20-Dec-22 8:04
Cp-Coder20-Dec-22 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.