Until recently, I have been using the following method to reserve memory for structs:
ITEM item[10];
ITEM* pitem[10];
pitem[0] = &item[0];
pitem[1] = &item[1];
...ect
This has been working fine, but I thought that using malloc might be preferable, so I changed it to:
ITEM* pitem[10];
pitem[0] = malloc(sizeof(ITEM));
pitem[1] = malloc(sizeof(ITEM));
...ect
However when I made this change, all the instances where I used a comparison with NULL, the program crashed. For example:
if (pitem[0]->member != NULL)
{
pitem[0]->member = member_001;
}
I've gone through the code again and again, using breakpoints and monitering variables and I'm fairly certain that the code only crashes when it comes to a NULL comparison. So, I was wondering if I should just go back to my old method or if I am using malloc wrong?
Thanks in advance!