Size of memory allocation for pointers

Oct 19, 2016 at 6:19pm
I am slowly starting to really get a grasp on pointers, but I am quite confused on how the memory allocation of pointers is beneficial:

- On most compilers, a pointer is either 4 bytes or 8 bytes. In contrast to this, it is known that pointers, while taking up space in memory, take up much less space than the data types they point to. If this is the case, how can a pointer of size 4 or 8 bytes ever take up less space than the data type it points to (int - 4 bytes, char - 1 byte, etc.)?

(I understand that pointers are most useful when dealing with dynamic memory allocation, but I am just interested in the size of memory, not the use of that memory right now)
Oct 19, 2016 at 7:29pm
A pointer is a variable that contains a memory address. That is all it contains. It does not contain the data at the memory address that it represents, much like an address book doesn't contain all of the houses for which it stores addresses.
Oct 20, 2016 at 12:01am
I understand that.

My question is more focused on the size of the pointer itself inside of the memory.
Oct 20, 2016 at 12:30am
For example,
1
2
3
4
5
struct VeryBigObject{
    char big_array[1024 * 1024 * 1024];
};

VeryBigObject *pointer = new VeryBigObject;
sizeof(VeryBigObject) = 1 GiB, sizeof(VeryBigObject *) = 4 or 8 bytes.
Oct 20, 2016 at 2:28am
I've never thought of it this way, it makes total sense now. Thanks!
Topic archived. No new replies allowed.