Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

Tags:

I acknowledge that all three of these have a different meaning. But, I don't understand on what particular instances would each of these apply. Can anyone share an example for each of these? Thank you.

       malloc(sizeof(int))        malloc(sizeof(int *)) (int *)malloc(sizeof(int)) 
like image 483
rajagrawal Avatar asked Mar 05 '13 04:03

rajagrawal


People also ask

What is int *) malloc sizeof int ))?

malloc(sizeof(int*)) means you are allocating space off the heap to store a pointer to an int . You are reserving as many bytes as a pointer requires. This returns a value you should cast to an int ** . (A pointer to a pointer to an int .)

Why int * is used in malloc?

malloc() function is used to dynamically reserve some space in the memory that is to be assigned to some variable later. int* acts as an pointer to the value which the function malloc() is returning. It returns a value void*, to this int* is a pointer it implicitly converts the pointer value to int.

Why do we write int *) before malloc int * ip int *) malloc sizeof int ));?

It is for the syntax correctness. It is for the type-casting. It is to inform malloc function about the data-type expected.

How many bytes is malloc sizeof int * 4?

void* malloc(size_t size) e.g. Since the size of int is 4 bytes, this statement will allocate 20 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.


2 Answers

malloc(sizeof(int)) means you are allocating space off the heap to store an int. You are reserving as many bytes as an int requires. This returns a value you should cast to int *. (A pointer to an int.) As some have noted, typical practice in C is to let implicit casting take care of this.

malloc(sizeof(int*)) means you are allocating space off the heap to store a pointer to an int. You are reserving as many bytes as a pointer requires. This returns a value you should cast to an int **. (A pointer to a pointer to an int.)

(int *)malloc(sizeof(int)) is exactly the same as the first call, but with the the result explicitly casted to a pointer to an int.

Note that on many architectures, an int is the same size as a pointer, so these will seem (incorrectly) to be all the same thing. In other words, you can accidentally do the wrong thing and have the resulting code still work.

like image 56
Gort the Robot Avatar answered Nov 02 '22 11:11

Gort the Robot


The syntax pattern that is most foolproof is:

 int *p;  p = malloc (cnt * sizeof *p); 

This syntax will not force you to change the code if the type (and or size...) of *p changes, eg in

 struct mysstruct *q;  q = malloc (cnt * sizeof *q); 

Which will avoid problems like

struct mysstruct *z; z = malloc (cnt * sizeof(struct hisstruct)); // Auch! 

, plus: the sizeof expr form is also shorter.


UPDATE: to demonstrate the correctness of p = malloc(CNT * sizeof *p) this test program:

#include <stdio.h> #include <stdlib.h>  struct mystruct {         int i;         char s[14];         }; int main(void) { struct mystruct *p; size_t siz;  siz = sizeof p; printf("Sizeof p := %zu\n", siz);  siz = sizeof *p; printf("Sizeof *p := %zu\n", siz);  printf("Allocating %zu (%u structs, each of size %zu) bytes to be assigned to p...\n"         , 10u * sizeof *p         , 10u, sizeof *p         ); p = malloc(10 * sizeof *p);  return 0; } 

Which outputs here:

Sizeof p := 8 Sizeof *p := 20 Allocating 200 (10 structs, each of size 20) bytes to be assigned to p... 
like image 37
wildplasser Avatar answered Nov 02 '22 10:11

wildplasser