why java does not support pointers

C

Chris Smith

Roedy Green said:
In C, a pointer's value is the machine address of the thing pointed
to, even if another pointer. It has no internal hidden indirection.

The specification makes no guarantee of that fact.
Potentially a reference could encode facts about the object or could
be several layers of indirection, e.g. to an out of ram backup.
Because all details of how it works are hidden, that gives maximum
flexibility for implementors.

Yep, that's true of pointers in C, too.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

The specification makes no guarantee of that fact.

I think you would find it does in practice.. If you add 1 to a C
pointer you get the next byte. If the pointer were implemented as a
handle you would get gibberish.
 
M

Mike Schilling

Roedy Green said:
I think you would find it does in practice.. If you add 1 to a C
pointer you get the next byte. If the pointer were implemented as a
handle you would get gibberish.

"pointer + 1" is an idiom that does the right thing, not a simple integer
addition. That's why, in a naive implementation,


struct a { char[100]c };

struct a *ptr;
ptr++; /* increments ptr by 100 */

while

ptr = (struct a*)((int)ptr + 1); /* undefined */

might do almost anything,
 
L

Luc The Perverse

Mike Schilling said:
Roedy Green said:
I think you would find it does in practice.. If you add 1 to a C
pointer you get the next byte. If the pointer were implemented as a
handle you would get gibberish.

"pointer + 1" is an idiom that does the right thing, not a simple integer
addition. That's why, in a naive implementation,


struct a { char[100]c };

struct a *ptr;
ptr++; /* increments ptr by 100 */

while

ptr = (struct a*)((int)ptr + 1); /* undefined */

might do almost anything,

Which was an annoyance when I wanted to manipulate bits in a double. I had
to cast through a void pointer to an unsigned char pointer to be able to
access the individual bytes. Fortunately (although later I realized also
intuitively) there was no additional work done in the assembly. The stupid
"protection" kept me from just directly casting to a pointer. In
retrospect, I should have just written that routine in assembly.
 
M

Mike Schilling

Luc The Perverse said:
Mike Schilling said:
Roedy Green said:
In C, a pointer's value is the machine address of the thing pointed
to, even if another pointer. It has no internal hidden indirection.

The specification makes no guarantee of that fact.

I think you would find it does in practice.. If you add 1 to a C
pointer you get the next byte. If the pointer were implemented as a
handle you would get gibberish.

"pointer + 1" is an idiom that does the right thing, not a simple integer
addition. That's why, in a naive implementation,


struct a { char[100]c };

struct a *ptr;
ptr++; /* increments ptr by 100 */

while

ptr = (struct a*)((int)ptr + 1); /* undefined */

might do almost anything,

Which was an annoyance when I wanted to manipulate bits in a double. I
had to cast through a void pointer to an unsigned char pointer to be able
to access the individual bytes. Fortunately (although later I realized
also intuitively) there was no additional work done in the assembly. The
stupid "protection" kept me from just directly casting to a pointer. In
retrospect, I should have just written that routine in assembly.

I don't follow you;

double d;
char *ptr = (char *)&d;

should work fine in either C or C++.
 
S

Stefan Ram

Mike Schilling said:
I don't follow you;
double d;
char *ptr = (char *)&d;
should work fine in either C or C++.

At least for C, it is said in ISO/IEC 9899:1999 (E):

"When a pointer to an object is converted to a pointer to
a character type, the result points to the lowest
addressed byte of the object. Successive increments of the
result, up to the size of the object, yield pointers to
the remaining bytes of the object.", 6.3.2.3, #7
 
L

Luc The Perverse

Mike Schilling said:
Luc The Perverse said:
Mike Schilling said:
message On Wed, 4 Jan 2006 11:26:22 -0700, Chris Smith <[email protected]>
wrote, quoted or indirectly quoted someone who said :

In C, a pointer's value is the machine address of the thing pointed
to, even if another pointer. It has no internal hidden indirection.

The specification makes no guarantee of that fact.

I think you would find it does in practice.. If you add 1 to a C
pointer you get the next byte. If the pointer were implemented as a
handle you would get gibberish.

"pointer + 1" is an idiom that does the right thing, not a simple
integer addition. That's why, in a naive implementation,


struct a { char[100]c };

struct a *ptr;
ptr++; /* increments ptr by 100 */

while

ptr = (struct a*)((int)ptr + 1); /* undefined */

might do almost anything,

Which was an annoyance when I wanted to manipulate bits in a double. I
had to cast through a void pointer to an unsigned char pointer to be able
to access the individual bytes. Fortunately (although later I realized
also intuitively) there was no additional work done in the assembly.
The stupid "protection" kept me from just directly casting to a pointer.
In retrospect, I should have just written that routine in assembly.

I don't follow you;

double d;
char *ptr = (char *)&d;

should work fine in either C or C++.


It did not allow me to do that - of course I was using a microsoft compiler.
 
M

Mike Schilling

Luc The Perverse said:
It did not allow me to do that - of course I was using a microsoft
compiler.

Was it .NET Managed C++? I know I've done things like this in the old VC++
days. And a real compiler (gcc) has no trouble with it.
 
L

Luc The Perverse

Mike Schilling said:
Was it .NET Managed C++? I know I've done things like this in the old
VC++ days. And a real compiler (gcc) has no trouble with it.


Oh you can do it, you just have to use an intermediary (void *) cast.

I didn't mean to imply it couldn't be done, only that it was annoying.

I think you have to use a volatile double as well to prevent the compiling
from assuming that the doble value hasn't changed. (Usually this wasn't a
problem, but I didn't want a no-aliasing optimization turned on and messing
it up.)
 
M

Mike Schilling

Luc The Perverse said:
Oh you can do it, you just have to use an intermediary (void *) cast.

Not in GCC, which is why I was curious which compir required that.
 
L

Luc The Perverse

Mike Schilling said:
Not in GCC, which is why I was curious which compir required that.


Ask old Billy Boy. I suppose it is to prevent the coder from mistakenly
thinking he has cast the value, not just the address.
 
M

Mike Schilling

Luc The Perverse said:
Ask old Billy Boy. I suppose it is to prevent the coder from mistakenly
thinking he has cast the value, not just the address.

Even if I had his ear, I don't think he'd know which compiler you were using
:)
 
L

Luc The Perverse

Mike Schilling said:
Even if I had his ear, I don't think he'd know which compiler you were
using :)

Sorry - I misunderstood. It was Microsoft Visual Studio C++ 6.0
Professional.
 

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
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top