I get null memory using following code of malloc/calloc. Sometimes it fails to allocate memory for "name1" and then strcpy fails. Please guide.
struct testMalloc
{
char name1[90];
char name2[90];
struct testMalloc* ptr;
};
int main(int argc, char* argv[])
{
struct testMalloc* test = 0 ;
int size = 0;
size = sizeof(struct testMalloc);
printf("Size of struct is %d", size);
test = (struct testMalloc*) calloc(sizeof(struct testMalloc));
strcpy((test->name1), "hdshdssdsdfsfffffffffffffffffffffffffffffh");
return 0;
}
You do not include <stdlib.h>
for the compiler to know the signature of calloc
and in this case it uses K&R
calling convention.
If you include <stdlib.h>
the code won't compile before to correctly call the calloc
.