Type of Pointer

C

Chris Dollin

atif said:
It is well known that Pointer just holds address.

No, it isn't. A run-time pointer value /may/ just hold an address,
but it can hold other information as well -- it's not prohibited
by the Standard. A debugging implementation can usefully hold not
only the address, but the permitted range of values of that address,
so that pointer arithmetic can be checked.
But how the machine
came to know that the pointer is holding an address of an Integer so i
will read just 4 bytes and in case of Long 8 bytes.

C pointer variables are typed. The compiler knows at compile-time
what kind of thing the pointer points to, because you tell it.
`int*` variables are supposed to be pointing to `int`s, `char*`
variables to chars, and so on. [Or they may be null pointers,
of course.]
 
A

atif

Hi

It is well known that Pointer just holds address. But how the machine
came to know that the pointer is holding an address of an Integer so i
will read just 4 bytes and in case of Long 8 bytes. Is it the machine
which decides or compiler does something in the back end ?

Hope i will get some good response

Regards
Thanks
 
A

atif

The compiler knows at compile-time
what kind of thing the pointer points to, because you tell it.

It means it is the compiler which generates the final machine code to
either read 4 bytes or 8 bytes when reading an Integer or a Long,
right?

Can you give a reference/book/url which confirms this concept + more
 
R

Richard Heathfield

atif said:
It means it is the compiler which generates the final machine code to
either read 4 bytes or 8 bytes when reading an Integer or a Long,
right?

Wrong. Nothing in the C language requires integers to fit into any
particular number of bytes. I have personally encountered systems
where:

sizeof(int) = 1, sizeof(long) = 1
sizeof(int) = 2, sizeof(long) = 4
sizeof(int) = 4, sizeof(long) = 4

and there's nothing to stop either type occupying any arbitrary number
of bytes. The only size requirements are that int is at least 16 bits
wide (but may be more), and long is at least 32 bits wide (but may be
more). The possibility of padding bits means that sizeof(int) could
conceivably be greater than sizeof(long), despite range limitations.
Can you give a reference/book/url which confirms this concept + more

The best reference you can get for C is ISO/IEC 9899.
 
C

Chris Dollin

atif said:
It means it is the compiler which generates the final machine code to
either read 4 bytes or 8 bytes when reading an Integer or a Long,
right?

It means that the compiler has to use the instructions appropriate to
its chosen representation of C data types -- whatever they are.

[A C implementation isn't required to have 4-byte ints, or 8-byte longs,
or to have bytes that are exactly 8 bits wide.]
Can you give a reference/book/url which confirms this concept + more

Any decent compiler book should explain code generation: this aspect
isn't specific to C.

What's making you care so much?
 
B

Barry Schwarz

It means it is the compiler which generates the final machine code to
either read 4 bytes or 8 bytes when reading an Integer or a Long,
right?

Can you give a reference/book/url which confirms this concept + more

There is no such object type as pointer. (If you think there is, the
burden of proof is on you.) It is a generic phrase used when the
specific type being pointed to is not important and allows us to avoid
cluttering the language with irrelevant details.

Details, however, are not irrelevant to the compiler. When the
compiler generates code to dereference a pointer, it is dealing with a
pointer object that has a specific type, such as pointer to int,
pointer to struct, etc. For a given type T, the compiler knows the
size of T and any internal structure relevant to that type. (If it
doesn't, a diagnostic is required.) If your code dereferences a
pointer to int, the compiler knows that 1) sizeof(int) bytes will be
involved and 2) the pointer points to the first of these bytes.

Given code along the lines of

int a;
int *ptr = &a;
a = 25; /*(1)*/
*ptr = 15; /*(2)*/

why do you think statement (2) requires more of the compiler than
statement (1)? Could the compiler generate the proper code for (1)
without knowing where a is located and how many bytes it occupies. For
statement (2), it must know all about ptr but after evaluating the
left expression, it must now know the exact same data about a as it
did in (1). It's not that much different.


Remove del for email
 
M

Martin Ambuhl

atif said:
Hi

It is well known that Pointer just holds address.

This is not at all well known, not is it necessarily true.
Anything deriving from your false premise can be safely ignored. And is

[...]
 
C

CBFalconer

Barry said:
.... snip ...

There is no such object type as pointer. (If you think there is,
the burden of proof is on you.) It is a generic phrase used when
the specific type being pointed to is not important and allows us
to avoid cluttering the language with irrelevant details.

And what do you call an object declared as:

T *obj;

if it isn't a pointer object?
 
M

Mark McIntyre

It means it is the compiler which generates the final machine code to
either read 4 bytes or 8 bytes when reading an Integer or a Long,
right?

Essentially. The compiler will read the type of the object from your
code, and generate appropriate machine level instructions. Precisely
what these are will depend on the hardware and operating system.
Can you give a reference/book/url which confirms this concept + more

Not really - any book on compiler technology might cover it though.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

SM Ryan

# Hi
#
# It is well known that Pointer just holds address. But how the machine
# came to know that the pointer is holding an address of an Integer so i
# will read just 4 bytes and in case of Long 8 bytes. Is it the machine
# which decides or compiler does something in the back end ?

Some machines encode the data length in the address, but these
are rare. For most machines the address simply distinguishes one
byte or bit and the program must decide for itself what to do
with that. If the program says load a byte, it's a byte address,
or load a word and it's a word address.
 
B

Barry Schwarz

And what do you call an object declared as:

T *obj;

if it isn't a pointer object?

A pointer to T object. The compiler never generates code for generic
pointers because there are no such objects.

The same is true for integers. It is a generic phrase used when we
chose to be deliberately vague (or if you prefer, all-inclusive).
There are char, short, int, etc. objects but the compiler never
generates code for generic integers because there are no such objects.

But you knew this was the point I was making since it was in the next
paragraph you elected not to quote.


Remove del for email
 
C

Chris Torek

Or, to put it in analogy terms, "pointer", like "purple", acts
like an adjective.

Saying "I have a pointer" is like saying "I have a purple" -- it
is incomplete.

And what do you call an object declared as:

T *obj;

if it isn't a pointer object?

If you know you are dealing with some kind of grape, saying "purple"
may be sufficient -- this will distinguish between the ordinary
green Thompson Seedless grape at the supermarkets, and the tasty
Black Corinth at the specialty store. If it could be a grape or
a plum, though, you need to be more specific.

In this case, we know we have a purple "T". :)
 
C

CBFalconer

Barry said:
A pointer to T object. The compiler never generates code for
generic pointers because there are no such objects.

In the above, obj _contains_ a pointer to something of type T, or
is an illegal object (if not properly initialized). It can be
copied, transmitted, altered, maltreated, etc. It _is_ an object.
 
K

Keith Thompson

Chris Torek said:
Or, to put it in analogy terms, "pointer", like "purple", acts
like an adjective.

Saying "I have a pointer" is like saying "I have a purple" -- it
is incomplete.
[...]

I have an orange. :cool:}
 
O

osmium

Chris Torek said:
Or, to put it in analogy terms, "pointer", like "purple", acts
like an adjective.

Saying "I have a pointer" is like saying "I have a purple" -- it
is incomplete.

Simply wonderful explanation!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,174
Latest member
RonnyLoren

Latest Threads

Top