I have a query about the behavior of C/C++ dealing with blindly incrementing a pointer.
So, I have a pointer to an int as a parameter to a function
func(int* thePointer) {...
and I have a loop inside that function
while(*thePointer) {
++thePointer;
}
I understand that as long as there are int‘s in the memory beyond this pointer the loop will continue, but what if the memory belongs to part of another memory type? Say you increment into the first 4 bytes of a double. Will the int still have a value/will the loop continue in this case?
Disclaimer: I know this is very most likely bad practice. This is a purely academic question.
In memory there is no such thing as a int or a double. Memory is just memory: placeholder for bytes.
So, if you keep incrementing a pointer to int, you will point to the next four bytes in memory and that’s it. If you attempt to use that portion of the memory through the pointer to integer, you will probably treat its content as if it were an int.
Eventually, you will point to a region of the memory not being assigned to your process and your program will exit with a SEGMENTATION FAULT.