Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMUZ140190 Tinashe Chinyanga
7,406 PointsWhat is the size of the pointer, (int *x), as compared to the variable it points to when stored in memory?
The variable int i will store a value which consumes some amount of memory. When a pointer of type int is declared, how much memory does this variable consume in comparison to the variable is points to
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Tinashe,
On a 32 bit system a pointer is likely to be 4 bytes and probably 8 bytes on a 64 bit system.
It's independent of the type of data it's pointing to. So whether it's an int * or a char * they both would take up the same number of bytes. So the 2 pointers themselves might be 4 bytes each, the int might be 4 bytes, and the char might be 1 byte.
MUZ140190 Tinashe Chinyanga
7,406 PointsMUZ140190 Tinashe Chinyanga
7,406 PointsThanks for the feedback. So essentially, however large or small the data we are pointing to changes, the pointer size remains constant thus saving memory consumption when passed by reference rather than by value right.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsThat's correct, It's useful to pass a pointer when you have a large data object so that you're not copying the entire thing.
Passing a pointer also lets you modify the data it points to from inside that function.