3
char *ptr = malloc(sizeof(char));
printf("\nValue of ptr: %d\n",ptr);
printf("\nValue of address of ptr: %x\n",&ptr);
printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));
*ptr='u';
 printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));

In the above piece of code, the following output is received:

Value of ptr: 1046113600
Value of address of ptr: a2fffa78
Value of pointed by ptr: P
Value of address pointed by ptr: 3e5a6d40
Value of pointed by ptr: u
Value of address pointed by ptr: 3e5a6d40

Does this imply that the default value of the malloc with a single char size is P?

Tried to allocate memory of single char size using malloc and initialized a pointer pointing to the allocated memory. Tried to print the value of the allocated memory.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
NeuronB
  • 53
  • 6
  • 4
    Welcome to SO. Your format specifiers are wrong. For printing a pointer you must use `%p`, not `%d or `%x. – Gerhardh May 08 '23 at 09:58
  • 3
    the value is indeterminate. – samann May 08 '23 at 09:59
  • 3
    You should also be aware that `&(*ptr)` is identical to `ptr` as `&*` or `*&` cancel each other out. You can see that `1046113600` is the same as `0x3E5A6D40`. And both are likely not the value returned by `malloc` because nowadays addresses are likely to be 64bit wide while integers are mostly 32 bits. – Gerhardh May 08 '23 at 10:00
  • Hey @Gerhardh, thanks for explaining. I have a question here. What does ptr represent here exactly? Is it the address of the allocated memory or is it the value of the ptr variable(pointing to the allocated memory) or both are the same? – NeuronB May 08 '23 at 13:11
  • 1
    Neuronb ptr is a variable of type char *. Its value is the address of the memory allocated by malloc() (assuming success). – Paul Lynch May 08 '23 at 13:21
  • 1
    The "value of the pointer variable" is the same as "the address of the allocated memory" in this case as you assign that address to that variable. – Gerhardh May 08 '23 at 13:44

2 Answers2

8

There is no "default value", as malloc does not initialize the memory. Its value is indeterminate. To initialize memory you can either use calloc instead which allocates and then sets all bytes to zero, or you can initialize the memory yourself.

paddy
  • 60,864
  • 6
  • 61
  • 103
6

malloc allocates memory without initializing it. So the allocated memory can contain any set of bit values. Neither default value is used.

Opposite to malloc you can use another allocating function calloc that zero initializes the allocated memory as for example

char *ptr = calloc(1, sizeof(char));

Pay attention to that such calls of printf to output values of pointers like

printf("\nValue of ptr: %d\n",ptr);
printf("\nValue of address of ptr: %x\n",&ptr);

are invalid and have undefined behavior. In particularly for example the conversion specifier d is designed to output integers of the type int and sizeof( int ) can be equal to 4 while sizeof( char * ) can be equal to 8.

From the C Standard (7.21.6.1 The fprintf function)

9 If a conversion specification is invalid, the behavior is undefined.275) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

You should use either the conversion specifier p with pointers as for example

printf("\nValue of ptr: %p\n", ( void * )ptr);

or conversion specifiers defined in header <inttypes.h>

#include <inttypes.h>

//...

printf("\nValue of ptr: %" PRIdPTR "\n", ( intptr_t )( void * )ptr);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Hi @Vlad from Moscow, thank you so much for your detailed answer. Can you please help me clarify some questions or confusions? So the ptr here is pointing to an uninitialized memory allocation(char *ptr=malloc). Now my question is whether outputing ptr(using %p) will return the address of the allocated memory or will it return the value of the variable ptr(which is pointing to the allocated memory) or are both of them same? – NeuronB May 08 '23 at 13:06
  • 1
    @NeuronB - The value of the the variable `ptr` happens to be a number which corresponds to an address in memory. They are the same. The format specifier `%p` expects a pointer, and will output that value. Outputting the contents of memory is done as you have shown in your question: `printf("\nValue contained in memory pointed to by ptr: %c\n",*ptr);` (with a minor edit to wording.) – ryyker May 08 '23 at 13:26
  • Thanks @ryyker for explaining this. Much appreciate it. So the line that you just commented does seem to output a character after malloc (P in this case). Is it a garbage value? – NeuronB May 08 '23 at 13:34
  • 1
    @NeuronB You are right. It is a garbage value. – Vlad from Moscow May 08 '23 at 13:42