C language---what result will malloc(0) produce, is it really a null pointer?

foreword

(1) I saw someone in a communication group a few days ago saying that it is really disgusting to ask what happens to malloc(0) in an interview.
(2) This suddenly piqued my curiosity. Can you still malloc(0)? !
(3) After testing, it was found to be feasible. After searching on the Internet, Brother Ken's exchange group and his own understanding, I sorted it out into this blog.
(4) Homepage of Kenge Blog: Architect Li Ken ;
(5) Feeling, the big guys in the group deserve to have many years of development experience. Look at the problem sharply. I found the problem right away, but I was still too superficial. By the way, I heard that the book C trap also explains this part of the knowledge. Although I have heard of the name for a long time, I have never bought it.

malloc (0) operation results

(1) I use printf to print out the area pointed by the pointer. Then use the malloc_usable_size() function to detect the requested memory size. By the way, use the strcpy() function to detect whether the area has incoming data.
(2) It was found that the program can run normally. There is no error, not a null pointer. Judging from the result returned by the malloc_usable_size() function, a 24-byte space has been applied for.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int main()
{
    
    
	char* p=malloc(0);
	printf("p = %p \r\n",p);
	strcpy(p,"abc");	
	printf("malloc_usable_size(p) = %ld \r\n",malloc_usable_size(p));
	free(p);
	p = NULL;
	return 0;
}

insert image description here

code analysis

(1) After consulting all kinds of information on the Internet, there are too many terms, and I really can't understand them. So I decided to ask in Brother Ken's exchange group. I thought I asked a high-quality question. Unexpectedly, what a clown, hahahaha.
(2) The group nickname is Master Xu, a 9-year-old boss, immediately throw me a link ; this is directly the official definition of the C language, which clearly shows that if malloc is passed in 0, the result will be produced.
(3) Official explanation:
If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage, but has to be passed to std:: free).
Translation: If size is zero, the behavior is implementation-defined (may return a null pointer, or may return some non-null pointer which may not be used to access storage but must be passed to std::free).
(4) What do you mean?
<1> That is to say, if the value passed in by malloc is 0, then this behavior can be allowed to pass.
<2>However, the final result of this behavior is a returned null pointer, or some memory that may not be accessible. (Note: It may not be accessed here. The result of my above operation shows that it can be accessed, and there is no conflict.)
<3>Finally, it also shows that the value returned by malloc needs to be released through the free() function.
(5) Then there is another question, why does my malloc(0) return a 24-byte space? Is this all 24 bytes?
No, according to the information on the Internet, the space returned by malloc(0) may be different in different environments.

Whether malloc(-1) is allowed

(1) Since malloc(0) is allowed, what about malloc(-1)? Judging from the running results, there will be errors.
(3) Why does malloc(0) work, but malloc(-1) does not work? This is because, as stipulated in the C standard, the parameters passed in to malloc must be unsigned data. However, I checked the information on the Internet and found that some people said that passing in -1 can run through, maybe there is a problem with the compiler, which parses -1 into 0xff ff ff ff (generally speaking, the number defaults to 4 bytes, And -1's complement is this).

insert image description here

Summarize

(1) The behavior of malloc(0) is allowed by the C language standard.
(2) What malloc(0) returns is not necessarily a null pointer, and the results produced by different environments are different.
(3) The space pointed to by the pointer returned by malloc(0) may not be accessed. (Again, it is possible !!!)
(4) The space returned by malloc(0) depends on the environment.
(5) The pointer generated by malloc(0) needs to be passed to free() for release.

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/131233403