473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

what will happen if we print the following...

int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);

May 25 '07 #1
10 1696
On 25 Maj, 14:47, Shraddha <shraddhajosh...@gmail.comwrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
What do you think will happen?

By the way, this is probably better asked in comp.lang.c

--
Erik Wikström

May 25 '07 #2
Shraddha wrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
This question doesn't make any sense. You'll take the time to post this
to a newsgroup, but you won't compile the code and see for yourself what
happens?

May 25 '07 #3
On May 25, 8:47 am, Shraddha <shraddhajosh...@gmail.comwrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);

What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?

May 25 '07 #4
"Salt_Peter" writes:
On May 25, 8:47 am, Shraddha <shraddhajosh...@gmail.comwrote:
>int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);


What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?
It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
May 25 '07 #5
osmium wrote:
"Salt_Peter" writes:
On May 25, 8:47 am, Shraddha <shraddhajosh...@gmail.comwrote:
int *s=(int *)2000;
int *p=(int *)1000;
>
printf("%d",p-s);

What will happen is nowhere near as important as what the intention
of the code is. What are you trying to do?

It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
Learning a programming language, especially a large one like C++, by
random usenet posts is a fool's game. It wastes everyone's time.

He needs to get a decent book and work through it, asking questions
about things he can't figure out.


Brian
May 25 '07 #6
On May 25, 2:47 pm, Shraddha <shraddhajosh...@gmail.comwrote:
int *s=(int *)2000;
int *p=(int *)1000;
printf("%d",p-s);
It depends very much on the implementation, and it could core
dump, or crash the system. On most systems, however, it will
probably output the value of 1000/sizeof(int).

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #7
On May 25, 3:07 pm, "osmium" <r124c4u...@comcast.netwrote:
"Salt_Peter" writes:
On May 25, 8:47 am, Shraddha <shraddhajosh...@gmail.comwrote:
int *s=(int *)2000;
int *p=(int *)1000;
printf("%d",p-s);
What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?

It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
Unfortunately, that appears to be the case (although i'ld replace
'rules' with 'effects'). I'ld suggest that setting pointers to
unallocated memory using magic numbers is begging for a whiplashing.
Calculating the offset between 2 memory locations and dividing a
hypothetical sizeof(int) is not a constructive discussion for the OP.
Thats not what C++ is about, IMHO.

He might as well do:
#include <iostream>

int main()
{
size_t t(1000);
std::cout << t/sizeof(int) << std::endl;
}

I'ld rather he come forth and explain the purpose or expectations of
the code before we start hitting him with semantics (prefer
static_cast<>() to old casts, magic pointers are evil, etc).
May 25 '07 #8
"Shraddha" wrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
You would do well to compile this, run it, see what happens and remeber it.
#include <stdio.h>

int main(void)
{
double x = 1.e35;
int n;
n = (int) x;
printf("%d\n", n);
return 0;
}

The message is clear. A cast says: "compiler, do this conversion. I am a
smart human and I know what I am doing." The compiler believes you and does
its best to comply. The results may be nonsense, which is what I get on my
compiler. It prints -250. The compilers I have used say "can not convert'
in the pre-cast error message. It really means: I *will not*, unless you
explicitly tell me to.
May 25 '07 #9
"osmium" writes:

#include <stdio.h>

int main(void)
{
double x = 1.e35;
int n;
n = (int) x;
printf("%d\n", n);
return 0;
}
Oops. That's C code, I read it as a C problem and answered accordingly.
May 25 '07 #10
On May 26, 1:14 am, "osmium" <r124c4u...@comcast.netwrote:
"Shraddha" wrote:
int *s=(int *)2000;
int *p=(int *)1000;
printf("%d",p-s);
You would do well to compile this, run it, see what happens and remeber it.
#include <stdio.h>
int main(void)
{
double x = 1.e35;
int n;
n = (int) x;
printf("%d\n", n);
return 0;
}
You would do well *not* to try to run such a program. It's
undefined behavior, and there is no guarantee with regards as to
what might happen.

Rather than playing around, it would be well to learn the
language. Or even basic principles: I know of know language
where such conversions do anything useful---they're either
undefined behavior, as in C/C++, or they are guaranteed to stop
the program. A good programmer verifies before converting,
always.
The message is clear.
A cast says: "compiler, do this conversion. I am a
smart human and I know what I am doing."
The cast, in this case, changes absolutely nothing, since it is
an implicite conversion. At best, it might shut up a warning,
although for various more or less historical reasons, very few
compilers warn about lossy conversions.
The compiler believes you and does its best to comply.
I'd express it differently: the compiler supposes that you know
what you are doing (cast or not), and that if you are assigning
a double to an int, you have verified that it won't overflow.
So it takes no precautions for such an eventuality.
The results may be nonsense, which is what I get on my
compiler. It prints -250. The compilers I have used say "can
not convert' in the pre-cast error message. It really means:
I *will not*, unless you explicitly tell me to.
What compiler is this? Double to int is a perfectly legal
conversion, and the compiler is required to compile the code.
(Actually, not in this case, because even the simplest optimizer
will immediatly determine that the conversion will overflow, and
that the program will contain undefined behavior, regardless of
the input. But in general, this is not verifiable at compile
time.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 26 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
11
by: antonyliu2002 | last post by:
I know that this has been asked and answered thousands of times. As a matter of fact, I know that I need to say If Not Page.IsPostBack Then 'Do something End If for things that needs to be...
7
by: Yvonne | last post by:
We have a Contacts & Events database made up of three tables viz tblContacts, tblEvents and tblAttendance with the latter linking contacts to particular events. The tblContacts table has a...
4
by: Shraddha | last post by:
for( ; ; ); printf("Hi");
8
by: Brian Munroe | last post by:
My example: class A(object): def __init__(self, name): self.__name = name def getName(self): return self.__name
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.