-
June 18th, 2014, 04:19 AM
#1
explain output
Hi,
When I run this program I get 10 as output. Can any one explain why output is 10 ? Thanks !
Code:
int *i, *j;
i = (int *)60;
j = (int *)20;
printf("%d\n", i-j);
-
June 18th, 2014, 04:31 AM
#2
Re: explain output
The behaviour is undefined since the pointers probably do not point to elements within the same array object, or one past the end. That said, when you subtract pointers, the difference is not the difference between the value of the pointers, but rather the difference in positions of the elements of the array (or one past the end) that the pointers point to. So, if sizeof(int) == 4, then i - j will give 10, were the behaviour well defined.
-
June 18th, 2014, 05:01 AM
#3
Re: explain output
Originally Posted by laserlight
The behaviour is undefined since the pointers probably do not point to elements within the same array object, or one past the end. That said, when you subtract pointers, the difference is not the difference between the value of the pointers, but rather the difference in positions of the elements of the array (or one past the end) that the pointers point to. So, if sizeof(int) == 4, then i - j will give 10, were the behaviour well defined.
Thanks for your reply. Because no array in involved here, i found this output quite vague.
-
June 18th, 2014, 05:46 AM
#4
Re: explain output
Code:
i = (int *)60;
j = (int *)20;
i is a pointer to a memory location that contains values of type int so i is a memory address and is set to the memory address 60. The same for j which is set to the memory address 20. If you try to dereference these pointers you'll probably get a run-time exception. Unless you're doing some esoteric programming it is unlikely you want to explicitly set a pointer to a memory address. Usually pointers are set to the address of other variables or from some memory functions (such as new) that provide correct and usable memory pointers.
You can only add or subtract pointers of the same type and the value is the difference between them divided by the size of the type to which they point. So in this case the result is equivalent to (i - j) / sizeof(int) which is (60 - 20) / 4 which is 10.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 19th, 2014, 08:04 AM
#5
Re: explain output
In addition to previous answers, to get 40, replace int* with char*
Pointer arithmetic results depend on pointers type.
-
June 20th, 2014, 05:23 AM
#6
Re: explain output
also, if the pointers aren't aligned properly to the type, then the subtraction may:
- give the rounded result
- cause a hardware exception
- ... ?
also, loading pointers with values (other than 0) that don't point to actual memory, may cause a hardware exception on some compilers/CPUs.
trying to read/write an int on an unaligned address causes hardware exceptions on some compilers/CPUs.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|