0

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!

Braiam
  • 1
  • 11
  • 47
  • 78
thatDude
  • 45
  • 5

2 Answers2

2
  1. If you use Visual Studio and write in C language (not C++ which is default) change in project properties language used:

enter image description here

Then you will not need (and you should not) to cast pointers.

(The difference is just the '&')

The & gives the reference (address) of the object.

int *pointer = malloc(sizeof(*pointer));

Defines the variable pointer having type pointer to int and initializes it with the value returned by the function malloc.

If you want to hack if the stored value is NULL you want to use this variable

if(pointer == NULL)

When you add the & you get the reference (address) of the variable pointer not the value stored in this variable.

Same is with other types.

int i = 5;

if(i == 5)

&i will give the reference of the variable i not the value stored in this variable.

0___________
  • 60,014
  • 4
  • 34
  • 74
1

Use checkAllocation(answer);.

The definition chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell)); calls malloc to reserve some memory. malloc returns the address of that memory (or a null pointer). The definition initializes the value of answer to have that address.

checkAllocation(&answer); would pass the address of the object named answer to checkAllocation. chessAllocation(answer) passes the value of the object named answer to checkAllocation. It is the value of answer that is the address return by malloc, and that is what you want to check.

Incidentally, the definition is better written as chessPosCell *answer = malloc(sizeof *answer));:

  • By using sizeof *answer instead of sizeof(chessPosCell), you get the size of the type being pointed to. This will remain correct even if the type of answer is later changed in code edits.
  • C automatically converts from void * to other pointer-to-object types, so the cast to (chessPosCell*) is unnecessary. And sometimes using a cast can suppress a warning message about a bug, because compilers assume that casts are intentional, that they indicate the author knows what they are doing.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312