Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 253

# pointer / memory based question

Can somebody give me detailed result of what is happening behind this program

#include <stdio.h>
int main()
{
int *p, *q;
p = (int *)1000;
q = (int *)2000;
printf("%d",(q-p));
return 0;
}
Answer on C complier is 500
and on VC++ compiler is 250
0
rohitdivas
1 Solution

Commented:
The "result" is the offset of the memory location pointed to by "q" to the memory location pointed to by "p".
So wherever the compiler places the two constant values: 1000 and 2000, will determine the final result. It will most probably be different for different compilers.

Paul
0

Commented:
You are doing pointer arithmetic (NOT integer arithmetic)...

int *p, *q; /*Pointer to an integer*/

p = (int *)1000; /*This means, make the ADDRESS of p to be 1000 in memory (memory offset 1000)

p = (int *)2000; /*This means, make the ADDRESS of q to be 2000 in memory (memory offset 2000)

q-p; /*Subtract the POINTER q from the POINTER p - based on the size of an int (since they are pointers to ints...*/

My guess is your C compiler is 16-bit, while VC++ compiler is 32-bit.

In 16-bit, an int is 2 bytes, hence you have room for 500 ints in 1000 bytes of memory...

In 32-bit, the size of an int is 4 bytes, hence you have room for 250 ints in 1000 bytes of memory...

0

Commented:

The answer is only different on compilers that have different sizes of ints.  The memory offsets will ALWAYS be 1000 bytes apart - so it is only the size of the int that matters...
0

Commented:
rohitdivas:

By the way, don't do anything else with p and q - like try and actually store an integer in them, e.g.

*p=7; /* that will give you a memory access violation */

0

Commented:
Try this on both compiler, spot the difference,
and you will understand

#include <stdio.h>
int main()
{
int *pi=1000, *qi=2000;
char *pc=1000, *qc=2000;
long *pl=1000, *ql=2000;

printf("char(%d)    : %d  ",sizeof(char),(qc-pc));
printf("integer(%d) : %d  ",sizeof(integer),(qi-pi));
printf("long(%d)    : %d  ",sizeof(long),(ql-pl));
return 0;
}

0

Commented:
> Paul,
> The answer is only different on compilers that have
> different sizes of ints.  The memory offsets will ALWAYS
> be 1000 bytes apart - so it is only the size of the int
> that matters...

Yes, ignore my comment, it can't possibly be right - it was way too late to be answering questions last night :(
0

Author Commented:
Here I would like to ask you one thing:-
We are substracting the memories; but your below line makes me assume that we are allocating memory. How come....

In 16-bit, an int is 2 bytes, hence you have room for 500 ints in 1000 bytes of memory...

In 32-bit, the size of an int is 4 bytes, hence you have room for 250 ints in 1000 bytes of memory...

Can you explain the above mentioned query ?

0

OwnerCommented:
No memory was allocated. When you do pointer arithmetic, the compiler divides the nominal difference between the two pointers by the _size_ of the object being pointed to. This gives a number that counts the number of objects of that size that lie between the two pointer positions.
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.