Re: cast pointer to int
- From: somenath <somenathpal@xxxxxxxxx>
- Date: Fri, 4 Jan 2008 02:14:31 -0800 (PST)
On Jan 4, 11:34 am, Jack Klein <jackkl...@xxxxxxxxxxx> wrote:
On Thu, 3 Jan 2008 21:16:43 -0800 (PST), somenath
<somenath...@xxxxxxxxx> wrote in comp.lang.c:
Hi All,
I was going through one tutorial about C language's pointer.
Apparently a very bad tutorial about pointers in C, one that goes into
all kinds of non-portable and implementation-defined details that are
completely unnecessary, and not defined by the C language itself.
After reading the tutorial I got couple of questions regarding cast
of pointer to integer .
In the exercise there is one question as mentioned .
This is an extremely bad question, and one that 99.9% of C programmers
will never need to know the answer to in order to write correct code.
"What is the output of following program? Assume that stack grows
towards lower addresses. Also assume that variables are pushed on to
the stack in the order in which they are declared.
"
Where does it say that you should actually assume a "stack", something
that is common on many platforms, but neither defined nor required by
C?
For all the questions, assume size of int is 4, size of short is 2 and
sizeof char is 1 byte.
I work with implementations where this is not true. One that I work
with quite frequently has the following sizes for integer types:
char 1 byte
short 1 byte
int 1 byte
long 2 bytes
long long 4 bytes
Another has char, short, int, and long all 1 byte. I haven't looked
to see if they have a C99 compiler version, if they do size of long
long will be 2 bytes.
#include<stdio.h>
int main(void)
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", (int)p1-(int)p2);
There are platforms where the value of a pointer is too large to fit
into an int, so these casts product undefined behavior.
return 0;
}
The output of the program is
4.
The explanation of the output is as given blow.
"All the local variables are stored in stack. So, x, y, p1 and p2,
Absolutely untrue, on some platforms in all cases, on many platforms
in at least some cases.
all these variables will be part of the stack. Since, stack follows
"last in first out" principle, the first local variable is pushed into
the stack. i.e x is pushed first, followed by y, followed by p1 and
p2. So, &p2 and &p1 would differ by 4 bytes. Since p1 is pushed
earlier than p2, it will have higher memory address than p2. Since the
pointers are cast to integer, by subtracting them, they just behave
like normal numbers and hence output would be 4."
My questions are
Your questions should be to the person who wrote the tutorial. They
should include such topics as these:
1. Why are you wasting all this effort on non-portable nonsense?
2. Why not just explain the proper declaration, definition, and use
of pointers without all this unnecessary stuff?
1) if the size of int is 4 is it safe to assume that sizeof pointer
to int also will be 4 ?
No.
2) Is it safe to cast pointer to integer and use them ?
No, in fact there is no requirement in the C standard that there even
be an integer type wide enough to represent a pointer.
3) And if the size of int and and size of pointer to int differ the
what will happen to extra bits ?
Undefined behavior happens, which is not a problem because there is no
need to actually do this sort of stupid casting in real programs
written by programmers who know what they are doing.
Please help me to understand these points.
Better you should try to understand why this information is not
necessary and largely a waste of time.
Many thanks for the advice, guidance . But I can not ignore this kind
of tutorial.
Let me explain my situation . I got job one of the big software
company in our country. In that we need to pass one C examination. For
that examination they provide tutorial and exercises .If I don't pass
I will not get good project assignment or may loose job
My feeling is that in our company no body bothers about portability.
If it works for Linux and intel platform its enough .May be that's the
reason they bother about so much system specific details. Really I am
not sure what is the cause of providing such kind of tutorial.
.
- Follow-Ups:
- Re: cast pointer to int
- From: santosh
- Re: cast pointer to int
- References:
- cast pointer to int
- From: somenath
- Re: cast pointer to int
- From: Jack Klein
- cast pointer to int
- Prev by Date: Re: superseded by c++?
- Next by Date: Re: superseded by c++?
- Previous by thread: Re: cast pointer to int
- Next by thread: Re: cast pointer to int
- Index(es):
Relevant Pages
|
|