-
weird computation result
Hi everybody,
I have this line of code:
Code:
sizeInputPtr++;
if(sizeInputPtr >= sizeArray+sizeof(int)*MAXARRAYSIZE) {
sizeInputPtr = sizeArray;
}
I was having a problem where it seems sizeInputPtr never resets so I checked the values using watch in MSVC .NET 2003 and this is what I got:
Code:
sizeArray 0x00321558 int [20]
0x00321558+(sizeof(int)*20) 0x003215a8 unsigned int
sizeArray+(sizeof(int)*20) 0x00321698 int [20]
MAXARRAYSIZE is defined as being 20 using
#define MAXARRAYSIZE
It does not make sense to me.
The first line says that the array of integers sizeArray start at address 0x00321558
The second line is to test what is the end address of the array which seems to be correct at address 0x003215a8. 20 integers = 80 bytes.
The third line is confusing since it should give me the same value as the second line but it's much more.
If any guru out there could lend a hand in understanding this, I'd appreciate. Thanks a lot,
Amish
-
You don't need to take into the size of the object when doing pointer arithmetic.
If sizeInputPtr is a pointer to the same type as your array elements are, and MAXARRAYSIZE is the size of the array, then all you need to stop at the end of the array is
if(sizeInputPtr >= sizeArray+MAXARRAYSIZE)
-
Thanks that seem to work, I guess the + operator does the appropriate conversion with the right hand side to the data type of the left hand side of the operator when it comes to pointers. Thanks for helping out,
Amish