Re: cast pointer to int



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.


.



Relevant Pages

  • Re: size_t
    ... common integer types like char, short, int .. ... the idea for size_t was to provide support for various architechtures in a ... integer type as offset to the array on platforms where, say, int would be 32 ... bit but pointers 64 bit. ...
    (comp.lang.cpp)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... the pointer returned by the allocator (either directly or as a component ... int length_of_list; ...
    (comp.lang.c.moderated)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • misc, refocus: new possible lang idea, BSC
    ... object //prototypical object ... it may not be possible to assign between tagged-references and pointers, ... the other, if implemented, will be based on a class/instance system. ... int foo ...
    (comp.lang.misc)