Say I have a random struct, for example, a chess position.
typedef char chessPos[2];
and I have linked list of chess positions.
typedef struct _chessPosCell
{
chessPos position;
struct _chessPosCell* next;
} chessPosCell;
Now, I want to create a new pointer to a list. so I use the following:
chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));
Now, I want to check if the memory has been allocated correctly. So I want to create a specific function to check EVERY allocation in my code.
void checkAllocation(void* ptr)
{
if (ptr == NULL)
{
printf("Memory allocation failure. \n");
exit(1);
}
}
My question is, how do I send my new allocated memory?
1.
checkAllocation(&answer);
checkAllocation(answer);
(The difference is just the '&')
I'm asking this because I've been discussing with a friend. I'm using option 1, because option 2 gives me Visual Studio warning of "Derefencing NULL pointer "answer". And the friend says I need to use option 2, because I want to check "answer" allocation, and not it's address allocation. However, option 2 gives me the warning mentioned above!
So I've been a bit confused. Can anyone please explain this part to me? Thanks in advance!