0

A website said that malloc doesn't set the value returned to zero.

So I decided to test it using this code:

#include<stdlib.h>
#include<stdio.h>
int main() {
    size_t is = sizeof(int);
    unsigned int *l = malloc(is);
    while((*l)==0) {
        free(l);
        l=malloc(is);
    }
    return 0;
}

Will this ever return or run forever.

I waited a while for it to stop but it hasn't.

  • 3
    (1) malloc returns a valid pointer in this case. You seem to be writing "return" when you actually refer to the contents of the space allocated by malloc, these are two different things. (2) your program causes undefined behaviour by reading that space without initializing it, so anything can happen – M.M Mar 11 '20 at 03:46
  • Check [man](https://linux.die.net/man/3/malloc): "The malloc() and calloc() functions return a pointer to the allocated memory that is suitably aligned for any kind of variable. On error, these functions return NULL". – ggorlen Mar 11 '20 at 03:46
  • 3
    The generic answer is "because you got lucky". If you want to know why it happens to be the case for your program on your computer at this moment in time, you'll have to dig into the details of how your standard library is implemented, which is probably not a very productive use of your time. But you certainly cannot rely on it being either zero or nonzero. – Nate Eldredge Mar 11 '20 at 03:49
  • 1
    On top of everything else you're likely reallocating the same memory every time. Try allocating a few million zeros and see if they're all zero – Mad Physicist Mar 11 '20 at 03:51
  • 1
    "A website said that malloc doesn't set the value returned to zero." They should have written more carefully and said that it does not *necessarily* set the value to zero. It can set it to zero, or to 0xdeadbeef, or to random numbers, or to your mom's phone number. Any of the above are legal as far as the C standard is concerned. – Nate Eldredge Mar 11 '20 at 03:52
  • @Nate. My understanding is that the memory can contain anything, but that malloc will very specifically not alter what's already there. – Mad Physicist Mar 11 '20 at 03:53
  • 2
    Most implementations don't, because they don't want to waste the CPU cycles, but it would be legal if they did. For instance, there are implementations which fill all allocated memory with particular values to help you with debugging. Anyway, from the point of view of the C standard, you have no way of knowing what was "already there", so you can't really even know whether the contents were "already there" or if malloc wrote something of its own there. – Nate Eldredge Mar 11 '20 at 03:57
  • Relevant: https://stackoverflow.com/questions/8029584/why-does-malloc-initialize-the-values-to-0-in-gcc – M.M Mar 11 '20 at 03:57

3 Answers3

3

You're not checking the value returned by malloc. In fact, malloc almost surely didn't return zero/NULL since that would result in a SIGSEGV on most systems.

You're checking what's at the address pointed by the pointer returned by malloc. That's not guaranteed to be zero or anything else, and it's Undefined Behaviour to read uninitialized memory. There's no point in discussing what happens when one invokes Undefined Behaviour.

Use calloc if you want initialized memory.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/209481/discussion-on-answer-by-ikegami-why-does-malloc-seem-to-always-return-0). – Samuel Liew Mar 11 '20 at 23:30
1

Generally, malloc does not set the memory it provides to zero. However, it may be zero for other reasons, such as that the operating system set it to zero when giving it to the process, and the process has not used it for anything else.

Therefore, you cannot expect that the memory will not be zero (and you cannot expect that it will be zero).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

As the manual says, malloc() return a void *, so a void-type pointer. depending on the case, the returned value change:

  • If malloc() is called with a size of 0, it will return NULL.
  • In case of any error, malloc() will return NULL.
  • If no problems are encountered, malloc will return a pointer to the allocated memory.

So when calling to malloc(), you will get an allocated space, randomly in your memory. And that's all. The malloc() function does not set values, or change data already written in memory; it will just return an address to a free space.

If you want to initialize values in an array, you should use the memset() function (man 3 memset).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nathan
  • 493
  • 1
  • 3
  • 13
  • 1
    `malloc(0)` could either return null pointer, or some other valid pointer. Also the call may or may not set values, and if you want it initialized you should use `calloc` (not malloc+memset) – M.M Mar 11 '20 at 04:09
  • 1
    well to be honest i actually never used `calloc` because i always set my variables after allocate them ^^' but according to the man, that's literally the solution – Nathan Mar 11 '20 at 04:13
  • "_If `malloc()` is called with a size of 0, it will return `NULL`_" is not right: [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.](http://port70.net/~nsz/c/c11/n1570.html#7.22.3p1) – ad absurdum Mar 11 '20 at 06:01