Need help understanding malloc (0) for my example

I am trying to figure out the insides of a double pointer (which is a pointer holding another pointer) in order to form an array of pointers. So, I am trying to run the following code, experimenting with malloc for debugging and seeing how it works. I can't figure out what malloc(0) does in my case, but my code works by outputting "Hello World".

What's the difference between

 pToCharsPointers = (char**) malloc(0); 

and

 pToCharsPointers = (char**) malloc(2 * sizeof(char*)); 

Please check what he does in my case.

 #include <stdio.h> char **pToCharsPointers; int main(void) { pToCharsPointers = (char**) malloc(0); char* pToChars = "Hello"; *pToCharsPointers = pToChars; *(pToCharsPointers + 1)= "World"; printf("%s %s\n", *(pToCharsPointers + 0), *(pToCharsPointers + 1)); return 0; } 

Also, I would really appreciate it if you could explain how double pointers work with an example in memory for visualization, since I don't see myself, although I tried to read about it in many places.

EDIT: Thanks to everyone for sharing your answers, and it really helped to understand. I got a valid pointer with malloc (0) when I printed it, and can dereference it also without problems during several attempts. I wanted to understand why it works. It looks like in my case the undefined behavior was actually expected.

+5
source share
4 answers

In your code

 (char**) malloc(0); 

wrong for two reasons for example

Any code attempting to use the return pointer for code like yours will essentially dereference invalid memory that causes undefined behavior , Thus, the output / behavior of your program cannot be predicted or justified.

OTOH Type Approval

 pToCharsPointers = malloc(2 * sizeof(char*)); 

but you must check the correctness of the returned pointer to ensure that malloc() is successful.

+6
source

I will try to explain:

 pToCharsPointers = (char**) malloc(0); 

this line will result in a pToCharPointers pointer that you cannot use

but you cannot know if an error will come. do not do this!

 char* pToChars = "Hello"; *pToCharsPointers = pToChars; 

this line will result in * pToCharsPointers to pToChar pointing to "Hello".

 *(pToCharsPointers + 1)= "World"; 

this line will write in * (pToCharsPointers + 1) that you cannot know where it indicates, "World". also, a variant of the error, but not sure.

 printf("%s %s\n", *(pToCharsPointers + 0), *(pToCharsPointers + 1)); 

this line will print "Hello Wolrd" if you have not received any errors yet.

+3
source

It just asks for zero bytes to be allocated. It depends on the implementation of malloc if it should return a null pointer or not.

Even if it returns a non-zero pointer, you cannot dereference it, since it will automatically be out of bounds. Highlighting the return pointer will result in undefined behavior in both cases.

When you execute malloc(2 * sizeof(char*)) , you allocate enough space for two pointers to char .

+2
source

You forgot to include <stdlib.h> . Calling malloc() without the correct definition has undefined behavior. Explicit casting of the return value hides the fact that the compiler can assume that it returns an int value. This listing is necessary in C ++, where a function call without a preliminary definition is invalid, but it is counterproductive in C.

Regarding your question: allocating a zero-sized block can have two possible results, depending on the implementation of your C library:

  • malloc(0) can return NULL , and trying to store values ​​at this address has undefined behavior.
  • malloc(0) can return a valid non-zero pointer, but this pointer cannot be dereferenced and is not used to store anything, since the size of the object it points to is 0 .

In both cases, your code has undefined behavior, which may or may not be a failure.

+2
source

Source: https://habr.com/ru/post/1264670/


All Articles