Thread: Subtracting from memory address- why does it do this?

  1. #1
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877

    Subtracting from memory address- why does it do this?

    I've just been learning about pointers, and out of curiosity I subtracted 1 from the address of a. Then I tried subtracting character constants.

    With the help of an ascii character value table I noticed that it always subtracts 4 for every 1 integer. I can't figure this out at all, anyone know why it does this?


    Code:
     #
    include<stdio.h>
    main()
    {
    int a, *b; 
    a=10;
    b=&a;
    printf("&a=%d\na=%d\n(referenced)b=%d\n(unreferenced)b=%d\n", &a-1, a, b, *b);
    }
    Output is:
    &a=2686724
    a=10
    b=2686728
    *b=10
    Last edited by Alpo; 04-09-2014 at 11:50 PM. Reason: Forgot to put output

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You should always use %p to print pointers, and cast the pointer to a void *
    Code:
    printf("&a = %p\n", (void *) &a);
    %d may or may not be suitable for representing the value of a pointer.

    It does it because that's what the C standard says it is supposed to do. Adding/subtracting 1 from any pointer moves it by the size of the thing pointed to. Thus, on your system, and int must be 4 bytes. This makes sense since if you increment a pointer, it should point to the next object, not the next byte. Think of using a pointer to step through an array. If it incremented by a single byte, the pointer could end up pointing to the middle of an object and you could have alignment issues (possibly something like a bus error that causes your program to crash). At the very least you would be accessing data in a nonsensical way. Furthermore, it would require different code for systems with different sized ints, longs, etc which would be a maintenance nightmare with no practical benefit. Note, the same type may be different sizes on different systems -- int was commonly only 16 bits back in the day, then 32 for 32 bit systems and now it's 64 on many (but not all) systems/implementations.

    Relevant portion of the C standard, if you want to get technical
    Quote Originally Posted by C99 6.5.6 p8
    8 When an expression that has integer type is added to or subtracted from a pointer, the
    result has the type of the pointer operand. If the pointer operand points to an element of
    an array object, and the array is large enough, the result points to an element offset from
    the original element such that the difference of the subscripts of the resulting and original
    array elements equals the integer expression. In other words, if the expression P points to
    the i-th element of an array object, the expressions (P)+N(equivalently, N+(P)) and
    (P)-N(where N has the value n) point to, respectively, the i+n-th and i−n-th elements of
    the array object, provided they exist. Moreover, if the expression P points to the last
    element of an array object, the expression(P)+1 points one past the last element of the
    array object, and if the expression Q points one past the last element of an array object,
    the expression (Q)-1 points to the last element of the array object. If both the pointer
    operand and the result point to elements of the same array object, or one past the last
    element of the array object, the evaluation shall not produce an overflow; otherwise, the
    behavior is undefined. If the result points one past the last element of the array object, it
    shall not be used as the operand of a unary*operator that is evaluated.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    That actually seems incredibly useful. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  3. Memory Address
    By Stack Overflow in forum C Programming
    Replies: 5
    Last Post: 05-25-2004, 11:43 AM
  4. Memory address
    By Coder87C in forum Windows Programming
    Replies: 1
    Last Post: 03-03-2004, 02:55 AM

Tags for this Thread