Would the following work?
Initialize a pointer (to any type of object) to 0.
int* thisPtr = 0;
Perform pointer arithmetic by incrementing the pointer until it has reached the last memory location. We'll know it's the last memory location because adding 1 to the pointer will not do anything. Keep track of how many memory locations we've visited.
int count = 1;
while (thisPtr + 1 > thisPtr) {
++thisPtr;
++count;
}
Now count
is equal to the number of memory locations we were able to visit. Multiply it by the number of bytes in a pointer.
int bytesInMemory = count * sizeof(int*);
Does that work??? If not, why, and what is the correct way?