1

So allocating zero bytes is ill-defined so I want to treat 0 bytes as a fail. Would this code do the trick

#include <stdio.h>
#incude "xmalloc.h"
void *malloc_or exit(size_t nbytes, const char *file, int line){
void *x; //declarea void pointer
if ((x = malloc(nbytes)) == NULL){
fprintf(stderr. " %s: line %d: malloc(%zu) bytes failed", file , line, nbytes);
exit(EXIT_FAILURE);
} else
return x;
}
Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
Josh
  • 113
  • 6

4 Answers4

5

The C standard (the link is to the N1570 draft) says:

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

So if your malloc_or_exit() function is called with an argument of 0, it may either terminate your program (if malloc(0) returns NULL), or it may return a non-null pointer that may not be dereferenced (dereferencing such a pointer would cause undefined behavior).

If you want to treat a zero-sized allocation as an error, you can modify your wrapper function (untested):

void *malloc_or_exit(size_t size) {
    void *result;
    if (size == 0) {
        exit(EXIT_FAILURE);
    }
    else if ((result = malloc(size)) == NULL) {
        exit(EXIT_FAILURE);
    }
    else {
        return result;
    }
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Hey #include #include int main() { int *p=(int*)malloc(0); p[0]=3; printf("%d",*p); return 0; } I tried this code and found no error which could be there due to dereferencing the pointer returned by malloc having 0 size as an argument. – Abhishek Jaiswal May 10 '21 at 08:08
  • @AbhishekJaiswal Your code has undefined behavior. If it didn't show a visible error, that just means you have a bug that's difficult to detect. – Keith Thompson May 10 '21 at 19:12
  • actually, something is missing in the code, the dereferencing operator (star) is removed when I posted the code. I don't know why did it happen. – Abhishek Jaiswal May 11 '21 at 02:37
  • Please visit the below link and explain why is this running well(without any error)? https://www.writeurl.com/text/pez4xzobzbo0l8zz7wdw/ar2eb1rh7zwpwziesyzq – Abhishek Jaiswal May 11 '21 at 02:37
  • @AbhishekJaiswal Comments user markdown formatting. Asterisks are used for emphasis *like this*, double asterisks for bold **like this**. Use backticks to format text as code `like this`. – Keith Thompson May 11 '21 at 03:25
  • @AbhishekJaiswal The link isn't working for me, but as I said the code in your original comment (once corrected for formatting errors) has undefined behavior. Behaving as you expect is one of the infinitely many possible consequences of undefined behavior (and arguably the worst). You're accessing an `int` object in unallocated memory. Remember that "undefined behavior" doesn't mean "this will crash". It means that the C standard says nothing about how it will behave. – Keith Thompson May 11 '21 at 03:25
  • Okay thanks for your suggestion, but I tried this code with `-pedantic` flag. Still can't figure out any warning. Is there still any way through which compiler would raise a warning or just notify me about the undefined behaviour? – Abhishek Jaiswal May 11 '21 at 03:33
  • 1
    @AbhishekJaiswal https://stackoverflow.com/a/4105123/827263 – Keith Thompson May 11 '21 at 04:11
1

If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.

ref:malloc

simon_xia
  • 2,394
  • 1
  • 20
  • 32
  • The "shall" in that is perhaps not helpful to the OP, who may not realize that it comes from the C standard. Essentially it says that the program should not try to use the return value as a pointer because it is not guaranteed to be valid (or useful). – Thomas Dickey Feb 14 '15 at 13:27
1
int i = 4;
void *x;
if( ( i != 0 ) && ( ( x = malloc(i) ) != NULL ) )
{
    // malloc successfull
}
else
{
    // not successfull
}
return 0;
Sridhar Nagarajan
  • 1,085
  • 6
  • 14
0

No - according to whats-the-point-in-malloc0, a non-null return value still may not be useful for your application.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105