Reputation: 11
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;
}
Upvotes: 0
Views: 537
Reputation: 15032
From top to bottom:
You forgot to #include stdlib.h
to use calloc()
and malloc()
. Implicit declarations are prohibited since C99.
int main(int argc, char* argv[])
Your program does not need to get arguments into it.
This:
int main (void)
would be more appropriate.
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
.
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;
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));
test = (struct testMalloc*) calloc(sizeof(struct testMalloc));
You do not need to cast the return value of malloc()
or calloc()
.
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.
}
Upvotes: 1
Reputation: 733
There are few syntax errors:
struct testMalloc* test = NULL;
This is how a NULL pointer is initializedcalloc(sizeof(struct testMalloc));
too few arguments passed to calloc
.
The correct form is calloc(no_of_elements, sizeof(type));
Here is the correct implementation of your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct testMalloc
{
char name1[90];
char name2[90];
struct testMalloc* ptr;
};
int main(int argc, char* argv[])
{
struct testMalloc* test = NULL;
size_t size = sizeof(struct testMalloc);
printf("Size of struct is %ld\n", size);
if((test = (struct testMalloc*)calloc(1, size)) == NULL){
return -1; //Failed to allocate memory
}
else {
strcpy((test->name1),"hdshdssdsdfsfffffffffffffffffffffffffffffh");
printf("%s\n",test->name1);
}
return 0;
}
Upvotes: 1
Reputation: 67476
sizeof
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct testMalloc
{
char name1[20];
char name2[90];
struct testMalloc* ptr;
};
struct testMalloc *allocAndCopy(const char *str)
{
struct testMalloc *ptr = malloc(sizeof(*ptr));
if(ptr)
{
strncpy(ptr -> name1, str, sizeof(ptr -> name1));
ptr -> name1[sizeof(ptr -> name1) - 1] = 0;
}
return ptr;
}
int main(int argc, char* argv[])
{
struct testMalloc* test = allocAndCopy("hdshdssdsdfsfffffffffffffffffffffffffffffh");
if(test) printf("the string is: %s\n", test -> name1);
}
Upvotes: 2
Reputation: 15793
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
.
Upvotes: 7
Reputation: 35512
calloc
takes 2 arguments: the number of elements, and the size of each element. It will zero out the allocated memory. What you're looking for is malloc
, which only takes 1 argument: the total size of the allocated memory chunk, and it does not zero out the allocated memory.
Upvotes: 3