Thread: weird computation result

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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)

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  3. simple program weird result
    By nav301 in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 02:07 PM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM