From top to bottom:
1.
You forgot to #include stdlib.h
to use calloc()
and malloc()
. Implicit declarations are prohibited since C99.
2.
int main(int argc, char* argv[])
Your program does not need to get arguments into it.
This:
int main (void)
would be more appropriate.
3.
int size = 0;
size
should never have a negative value. So it would be more appropriate to declare it as unsigned int
or even better size_t
.
4.
struct testMalloc* test = 0 ;
You can use 0
to initialize a pointer. It's perfectlty valid as 0
is a null pointer constant. But better use NULL
when dealing with pointers and not 0
to show the pointer intention and increase the readability.
struct testMalloc* test = NULL;
5.
calloc(sizeof(struct testMalloc));
calloc
requires two arguments in comparison to malloc
. The first needs to be number of items and the second the size of one item.
calloc(sizeof(1,struct testMalloc));
6.
test = (struct testMalloc*) calloc(sizeof(struct testMalloc));
You do not need to cast the return value of malloc()
or calloc()
.
7.
You forgot to check the returned pointed from calloc()
for a null pointer if the allocation has failed. Always check the return value of memory-management functions.
test = calloc(1, sizeof(struct testMalloc));
if (test == NULL)
{
fputs("Allocation failed!", stderr);
// error routine.
}