1

Why allocating a 0 size char block works in this case? But if I write char *string = NULL; it won't work.

I'm using Visual Studio.

int main()
{   
    char *string = (char *)malloc(0);
    string[0] = 'a';
    string[1] = 'b';
    string[2] = 'c';
    string[3] = 'd';
    string[4] = '\0';

    printf("%s\n",string);
    return 0;
}
6
  • 4
    It's not "working"; it at best pretends to be working. Undefined behavior is called "undefined" and not "guaranteed crash" for a very good reason. Commented Aug 6, 2015 at 12:20
  • What did you expect to happen and why? Commented Aug 6, 2015 at 12:20
  • This is not at all a duplicate. Commented Aug 6, 2015 at 12:21
  • 7
    Please don't cast the result of malloc(). Commented Aug 6, 2015 at 12:28
  • possible duplicate of Array index out of bound in C Commented Aug 6, 2015 at 14:05

3 Answers 3

10

First let me state, as per the man page of malloc()

The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

a call like malloc(0) is valid itself, but then, we need to check the validity of the returned pointer. It can either

  • Return NULL
  • Return a pointer which can be passed to free().

but anyways, dereferencing that pointer is not allowed. It will cause out-of-bound memory access and cause undefined behaviour.

That said, two important things to mention,

  1. Please see why not to cast the return value of malloc() and family in C.

  2. Please check the return value of malloc() before using the returned pointer.

So, to answer your question,

Difference between initializing a string with (char *)malloc(0) and NULL

Do not use malloc(0) in this case, as a NULL check on the pointer may fail, giving the wrong impression of a valid allocation of the memory to the pointer. Always use NULL for initialization.

Sign up to request clarification or add additional context in comments.

Comments

4

The above code invokes undefined behavior. You have allocated insufficient memory and you are accessing invalid addresses.

According to the specifications, malloc(0) will return either "a null pointer or a unique pointer that can be successfully passed to free()".

4 Comments

s/random/invalid. A memory allocator's behavior is pretty un-random, it would make a terrible PRNG.
@Quentin but this is a Q&A site for programmers, and if one gives an answer, one is supposed to phrase it correctly. I could as well decide that from now on, I use the word "string" to denote "function", and then I complain every time someone points out that one can't call a string that I just use "string" with "function" as its meaning. Some people are trying to use memory allocation addresses as the source of random numbers. Let's not encourage spreading the infection (what if these "random" numbers end up encrypting your HTTPS traffic? would you like that?)
@Quentin grow up. seriously, if you don't see the value in formulating correctly what one says, I don't even want to argue with you further.
@TheParamagneticCroissant I'm not even sure why you're arguing in the first place. Of course "invalid" is better than "random" for an indeterminate address. It's just that the thought of someone jumping on the mere presence of the word "random" and trying to make a PRNG out of it made me chuckle. There's no reason to be upset.
1

malloc definition:

Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

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.

Taken from here and found this related question.

1 Comment

calling malloc(0) does not result in undefined behavior, and the returned address isn't particularly random either. (in particular, it is required to be unique.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.