I am learning about malloc function & I see this difference but I cannot understand it
What is the difference between
ptr = (*int) malloc(sizeof(int)*N)
and
ptr = malloc(sizeof(int)*N)
I am learning about malloc function & I see this difference but I cannot understand it
What is the difference between
ptr = (*int) malloc(sizeof(int)*N)
and
ptr = malloc(sizeof(int)*N)
In C
cast of the malloc
is considered the bad practice.
C++
is more type strict and you need to cast if you want to assign pointer with pointer of other type. But I would rather avoid direct use of the malloc
(and generally pointers) in the C++ program.
In both I would rather use object in the sizeof
operator
ptr = malloc(sizeof(*ptr)*N);