Point-to-Int

I ran into this question
If p is a pointer-to-int variable with the current value 440, and the size of an int on our system is 8 bytes. What is the value of p after the following expression has executed:
p++

I've tried a bunch of solution but none seem to be correct. I've gotten 440, 441, 0, NULL, \271, 881, 880 111, 110.25 but these aren't right maybe I don't understand what the question is asking but can anyone else understand and maybe elaborate on it for me?
Remember the old math class questions where they tell you all kinds of stuff about two trains hurtling toward each other, and you have to figure out what is relevant and what is not to solve the problem?

Random Textbook Question That I Made Up wrote:
Engine #1, operated by a ruggedly handsome fellow with jet black hair and blue eyes, is now travelling at 50 mph eastward on the Golden Arches line, so called because of the number of McDonalds restaurants that grew up around the track-laying towns...

Get the idea? You have to think a little about the information you are given.

(To be fair, in this case you are not given any useless information, but you are expected to know something about pointer arithmetic.)


Hint: There is an integer at address 440. What is the address of the integer after that one? (It can't be 441, because an integer is not one byte wide.)

Hope this helps.
So what I tried is this,
1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, const char * argv[])
{
    int *p;
    p = new int;

    *p = 440;
    
    cout << p << endl;
    p++;
    cout << p << endl;
    return 0;
}


this gives me the output of 0x100103a90 and 0x100103a94 but if the int is 8 bytes it would move 8 spots right? So it would be 0x100103a98 but I enter these addresses and it's still wrong. I know addresses are different depending on the IDE am I doing this right? What is a way i can use this address as an answer since this is a fill in the blank and its wrong...
Are you sure an int is 8 bytes long? Try cout << sizeof(int) << endl; and see what you get.

Your output suggests to me that the size of an int is 4 bytes long.
Well I know an int is normally 4 bytes, but in the question it states that there system size for int is 8 bytes

also i did
p++;
p++; which gave me the 8 bytes which was
0x100103a98
Last edited on
> also i did
> p++;
> p++; which gave me the 8 bytes which was
> 0x100103a98

So, now try the same code with some type which has a size of eight bytes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdint>

std::uintptr_t numeric_value( const void* ptr ) 
{ return reinterpret_cast<std::uintptr_t>(ptr) ; }

template < typename T > void test()
{
    std::cout << "size of object is: " << sizeof(T) << '\n' ;
    
    T array[4] ; // array of 4 objects
    
    // print out the last three digits of the numeric value of the addresses
    for( int i = 0 ; i < 4 ; ++i ) std::cout << numeric_value( std::addressof(array[i]) ) % 1000 << ' ' ;
    std::cout << '\n' ;
    
    T* p = std::addressof(array[0]) ; // points to element at position zero
    std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
    
    ++p ; // increment it
    std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value

    ++p ; // increment it
    std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value

    ++p ; // increment it
    std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
    
    std::cout << "\n------------------------------\n" ;
}

int main()
{
    test<int>() ;
    test<long>() ;
    test<long double>() ;
    test< char[32] >() ;
    test< char[27] >() ;
}

http://coliru.stacked-crooked.com/a/e3730f240115d312
So it's 448 gosh I feel stupid right now...

Thanks for the help guys!
Last edited on
Topic archived. No new replies allowed.