?
Solved

pointer / memory based  question

Posted on 2003-03-23
8
Medium Priority
?
246 Views
Last Modified: 2010-04-15
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
Comment
Question by:rohitdivas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 8189697
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
 
LVL 6

Accepted Solution

by:
gj62 earned 120 total points
ID: 8189707
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
 
LVL 6

Expert Comment

by:gj62
ID: 8189713
Zebada/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...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8189721
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8190679
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
 
LVL 6

Expert Comment

by:zebada
ID: 8191198
> 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 Comment

by:rohitdivas
ID: 8192911
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8194629
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question