Ask coding questions

← Back to all posts
C++ Pointer Memory Question
cheesehumble (0)

I created a linked list class which only contains one member:
Node* head;

If I create a pointer a created LinkedList object why do I need to add 4 to the pointer to grab the head variable?

Answered by a5rocks (781) [earned 5 cycles]
View Answer
a5rocks (781)

1) pointer operations like that arent very good (in my opinion, though I dont do C++...)
2) C++ structs / objects / whatever in the memory have themselves, then their fields. So you just locate where it is in memory and then add 4 to get the first field.