Strings...immutable?

S

Stefan Ram

Wojtek said:
I said context.
int a = 0;
0 in this context is not a null pointer constant.

It is. The specification does not require a special context.
So you make up rules about the context that are not written in
the specification anywhere.

For the readers convenience, I will give a more complete
quotation:

»An integer constant expression with the value 0, or such
an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is converted
to a pointer type, the resulting pointer, called a null
pointer, is guaranteed to compare unequal to a pointer to
any object or function.«

ISO/IEC 9899:1999 (E), 6.3.2.3, #3

So, you can rightfully say, that in the above context »0« is
not a »null pointer« (see the second sentence of the
quotation). But it is a /null pointer constant/, even in
»int a = 0;«.

Yes, a /null pointer constant/ is not a /null pointer/ and
vice versa.

I was speaking of »null pointer constant«. You seem to
have been speaking of »null pointer«.
 
W

Wojtek

Stefan Ram wrote :
It is. The specification does not require a special context.
So you make up rules about the context that are not written in
the specification anywhere.

For the readers convenience, I will give a more complete
quotation:

»An integer constant expression with the value 0, or such
an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is converted
to a pointer type, the resulting pointer, called a null
pointer, is guaranteed to compare unequal to a pointer to
any object or function.«

ISO/IEC 9899:1999 (E), 6.3.2.3, #3

So, you can rightfully say, that in the above context »0« is
not a »null pointer« (see the second sentence of the
quotation). But it is a /null pointer constant/, even in
»int a = 0;«.

Yes, a /null pointer constant/ is not a /null pointer/ and
vice versa.

I was speaking of »null pointer constant«. You seem to
have been speaking of »null pointer«.

Ok, I stand corrected.
 
B

blmblm

I know Fortran has call-by-reference, and plenty of other ways of
getting into trouble, but I didn't think it got pointers, or anything
equivalent, until Fortran 90?

I think you're right, if it's standard Fortran you're talking
about, but as I understand it there was at least one nonstandard
extension providing something pointer-like, namely "Cray pointers".
Never worked with them, don't know much about them, but have heard
them discussed often and am pretty sure they came before Fortran 90.

My two cents' worth, and worth about that, maybe.
 
C

Chris Uppal

Patricia said:
Here's a question for people holding that view: Why should the
particular, and arguably peculiar, cluster of features that C associated
with pointers be the ultimate definition of pointerness?

I think part of it might run something like the following.

Most programmers grow up with a strong opinion of unrestricted pointer
manipulation. People who grow up in the C tradition see it as one of the great
strengths of C (rightly, IMO). Others, who grew up being taught by
quiche-eating Pascal programmers (or "professors" -- the two words do not
necessarily have related referents), have internalised the idea that such
things are /bad/. In either case they are unwilling to impute to Java the
same virtues or vices by using the word "pointers" for Java's references.

-- chris
 
J

Joshua Cranmer

Strings immutable? ha!

You're about fifty posts too late.
String s = new String("My String");
String x = s;

System.out.println(s);
Field field = x.getClass().getDeclaredField("value");
field.setAccessible(true);
char[] data = (char[]) field.get(x);
data[1] = 'X';
System.out.println(s);


(Sorry, I felt an evil streak coming on.)

Sorry didn't catch that, my SecurityManager balked.
 
P

Patricia Shanahan

Chris said:
I think part of it might run something like the following.

Most programmers grow up with a strong opinion of unrestricted pointer
manipulation. People who grow up in the C tradition see it as one of the great
strengths of C (rightly, IMO). Others, who grew up being taught by
quiche-eating Pascal programmers (or "professors" -- the two words do not
necessarily have related referents), have internalised the idea that such
things are /bad/. In either case they are unwilling to impute to Java the
same virtues or vices by using the word "pointers" for Java's references.

This paragraph leaves me in a major identity crisis.

I grew up mainly on assembly language, with a scattering of other
languages, in the 1970's, followed by C in the 1980's. On the other
hand, I used UCSD Pascal on my Apple II and I'm a computer science Ph.D.
candidate.

I have not one but two strongly held opinions on unrestricted pointer
manipulation:

1. It is very dangerous, and a source of extremely hard to find bugs.

2. It is absolutely essential in some system code.

It still seems to me that the question of whether Java has pointers
should be resolved by looking at Java and seeing if it has things whose
primary purpose is to point.

Patricia
 
M

Mike Schilling

Chris Uppal said:
I think part of it might run something like the following.

Most programmers grow up with a strong opinion of unrestricted pointer
manipulation. People who grow up in the C tradition see it as one of the
great
strengths of C (rightly, IMO). Others, who grew up being taught by
quiche-eating Pascal programmers (or "professors" -- the two words do not
necessarily have related referents), have internalised the idea that such
things are /bad/. In either case they are unwilling to impute to Java the
same virtues or vices by using the word "pointers" for Java's references.

It's one of the great strengths of C that unrestricted pointer manipulation
is available when it's needed. It is one of the great failings of C there's
no way to guarantee (or at least strongly suggest) only safe pointer
manipulation the rest of the time. And even people who are certain they can
handle pointers with no problem, eventually learn the consequences of the
fact that the guy in the next cubicle over can't.
 
J

John W. Kennedy

Patricia said:
3. Pointers without arithmetic existed in at least one published
language definition, the Pascal Report, when C was still being developed.

Even earlier, in PL/I.


--
John W. Kennedy
"Never try to take over the international economy based on a radical
feminist agenda if you're not sure your leader isn't a transvestite."
-- David Misch: "She-Spies", "While You Were Out"
* TagZilla 0.066 * http://tagzilla.mozdev.org
 
D

Douglas Wells

Even earlier, in PL/I.

Even earlier, in IPL and LISP.

but

I have three contemporary (i.e., pre-1970) references[1] that claim
that the concept of explicit list processing originated with Newell,
Shaw, and Simon in a 1956 paper (with first implementation as IPL-II
in 1957), where the concept of a pointer was known as a link.
(There is a an article on IPL at
http://en.wikipedia.org/wiki/Information_Processing_Language .)

Clearly, the concept was well established with the implementation
of LISP in 1960. And the use of the term pointer was emerging at
least as early as 1964, probably earlier than that.

The addition of pointers to PL/I seems to have occurred in 1967
with the introduction of based storage.

To address some of the other issues that have arisen in this thread,
the concept of pointer was also extant in BCPL vectors[2] as of
1967[3]. So, both Thompson and Ritchie would have had exposure to
both PL/I and BCPL as part of their work on Multics in the 1960s.

I do not happen to know of an earlier language that provided
arithmetic operations on pointers as implemented in C. BCPL's
notion that an lvalue locates a storage element rather than just
a hardware address in clearly a step in that direction.

- dmw

[1] Saul Rosen, 1967; Kramer, Kosinski, and Robinson, 1967; Jean
Sammet, 1969.
[2] BCPL's 'let x = v*' is analogous to C's 'x = p'
[3] Martin Richards, MIT Project MAC M-352, 1967.
 
P

Patricia Shanahan

Martin said:
I take it you never used Forth then?

Its a nasty little language from its unrelenting RPN through its
write-only nature and its 'real coders don't need comments or named
variables' attitude to its code organized in 16 x 64 'screens', but its
difficult to think of anything that's more portable. You translate and
assemble a few tens of low level words (IIRC under 100 lines of
assembler) and its ported!

I've used it a very little, both on an Apple II and later, in the Open
Boot PROM version, when debugging SPARC servers.

I've always tended to think of it more as a stack than a pointer
language, because of its RPN behavior.

Patricia
 
P

Piotr Kobzda

Wojtek said:
Chris Dollin wrote :

That is not a pointer pointing to a pointer. That is a pointer to an
object, which just happens to contain other pointers.

Just repeating sense of one unanswered question from this thread, what's
the difference?
A pointer to a pointer in C would look like:

**ptr;

Where the first level point to a pointer, and the second level points to
an object. Java does not have this.

Really? Pointers are low-level objects. Objects pointed by pointer
objects, might be other pointers, or different objects. Technically
there is no significant difference between them.

The same happens in Java when one object points to another object
holding reference to it.

One important difference between "pointers" in Java, and C (or C++) is
in management over all "pointers" in Java performed by JVM -- which cost
a bit, of course -- but thanks to that, there is no possibility to point
something which is not an object or nothing (null), that's all.

Not me. I am quite happy without pointers.

You are probably happy with different syntax for using pointers,
management of them for you, and restricted available set of operations
available on them -- just like in Java -- aren't you?

Without pointers you have to decide to use Java build-in primitive types
only, quite sad perspective IMHO... ;)


piotr
 
J

Jussi Piitulainen

Steve said:
That breaks down when Java's call-by-value semantics and primitive
arguments versus object arguments. While I prefer 'reference' to
'pointer', it greatly helps to have that general concept available
when explaining why:

f(x)

cannot alter the value of x if it's a primitive, but can alter the
'value' of x if it's an object. Note the quotes - I know it's not
altering the value of x - which is a reference to an object whose
value may change as a result of the call. f() can't change the
reference, but unless x is immutable it most likely can change the
value of the object being referenced. How does one explain this
without introducing the concept of a reference (pointer)?

I see no problem whatsoever. Primitives like boolean, int and double
are immutable. Some objects are mutable, so you can change them if you
have access to them. You can only assign to a variable if you have
access to the variable, not just to the value it has or once had.

"Alter the value of x" is ambiguous: do we talk about the object or do
we talk about the variable? Often it is clear from the context and
there is no problem. To disambiguate, I think I would say that f can
alter the object but cannot refer to the variable at all.

{
int [] x = { 1 };
f(x);
// f may have altered the array
// but it is still the same array: f had no access to the variable
}
The use of references in languages that don't have pointer
arithmetic has been around forever (forget Pascal, that's a newbie
in this area!).

Yes. Lisp goes back to 1960 and has always been that way.
 
C

Chris Uppal

Patricia said:
I grew up mainly on assembly language, with a scattering of other
languages, in the 1970's, followed by C in the 1980's. On the other
hand, I used UCSD Pascal on my Apple II and I'm a computer science Ph.D.
candidate.

I have not one but two strongly held opinions on unrestricted pointer
manipulation:

1. It is very dangerous, and a source of extremely hard to find bugs.

2. It is absolutely essential in some system code.

<nods/>

Much as I like C, and much as I think that C's pointers are amongst its most
magnificent features, I have also been forced to conclude that it's not safe
/enough/ for many real-life scenarios. Such as network-facing code, or code
which is likely to be subjected to malicious input (almost any document format,
and /especially/ just about every multimedia format).

It still seems to me that the question of whether Java has pointers
should be resolved by looking at Java and seeing if it has things whose
primary purpose is to point.

I'll hedge my bets -- it seems best (to me) to stick to the established
"reference" based terminology in most contexts, but it's nice to be able to
switch to talking about pointers in order to explain matters from a slightly
different point of view. That is doubly true on the endlessly recurring
occasions where the word "reference" in "pass-by-reference" is causing
confusion or dispute.

-- chris
 
W

Wojtek

Piotr Kobzda wrote :
Just repeating sense of one unanswered question from this thread, what's the
difference?


Really? Pointers are low-level objects. Objects pointed by pointer objects,
might be other pointers, or different objects. Technically there is no
significant difference between them.

The same happens in Java when one object points to another object holding
reference to it.


Because with a pointer to a pointer, both "point" to the SAME object,
albet with one level of separation.

But a pointer to an object which contains a pointer to another object,
there is different object, not the same object. That is a BIG
difference.

Otherwise you could say that with a HashMap, the reference to that
HashMap "points" to potentially 100's of objects. It does not. It
refers to one object which just happens to have references to other
objects.
One important difference between "pointers" in Java, and C (or C++) is in
management over all "pointers" in Java performed by JVM -- which cost a bit,
of course -- but thanks to that, there is no possibility to point something
which is not an object or nothing (null), that's all.



You are probably happy with different syntax for using pointers, management
of them for you, and restricted available set of operations available on them
-- just like in Java -- aren't you?

I programmed in C for over a decade. I am very comfortable with
pointers, structs and unions containing pointers, pointers to pointers
(to pointers), passing around pointers to functions, etc. Levels of
indirection do not bother me at all.

Moving to Java was a bit of a learning curve (no, not the OO part)
since I did not have some of the language capabilities of C. But
overall I prefer Java, for a number of reasons.
Without pointers you have to decide to use Java build-in primitive types
only, quite sad perspective IMHO... ;)

What? I can design my own types (classes) and use them. I then have
references to those instantiated classes (objects). That is not using
primitives only.
 
S

Stefan Ram

To show that they /coined/ it, still would need to show that
no one else already used it for this purpose in those days.

By coincidence, searching for something else, I found:

»1965 (...)

Harold W.Lawson invents the pointer variable and
introduced this concept into PL/I, thus providing for the
first time, the capability to flexibly treat linked lists
in a general-purpose high level language«

http://www.acm.org/top/tl/content.cgi
 
M

Martin Gregorie

Patricia said:
I've used it a very little, both on an Apple II and later, in the Open
Boot PROM version, when debugging SPARC servers.

I've always tended to think of it more as a stack than a pointer
language, because of its RPN behavior.
Depends how you look at it. When you look at its data handling and
function calling you're right - its purely stack-based and RPN.

However, if you look at the way its dictionary is structured and what
happens when you define a new word you suddenly find you're buried in
mounds of pointer spaghetti.

Like you, I never used it much and only on a 6809 chip. I looked into it
because I was fascinated by its guts and especially how easy it is to
port - there IS no interpreter, just a chunk of assembler that defines a
small collection of the lowest level words plus a single assembler
'branch' instruction to start it running. Everything else (including the
Forth compiler) is just files of Forth source that is compiled as its
loaded into RAM.

FWIW, I got interested in how different languages express the same
algorithm and defined a small but non-trivial problem (read and sanity
check a number, n, and then draw a solid square of asterisks, 'n' per
side, that's centered on the screen) and coded it in all the languages I
had at the time. Forth source was the smallest and least readable, COBOL
and assembler the biggest, C and PL/9 the most readable.
 
J

JT

Lew said:
When I read SCJP study materials, they have always asserted
unequivocally "call-by-value" as the only correct answer to "how does
Java pass method arguments?"

-- Lew
Practical Java Programming Guide Peter Haggar
Praxis 1: Understand that parameters are passed by value, not by reference

import java.awt.Point;
public class PassByValue {
public static void modifyPoint(Point pt, int j){
pt.setLocation(5,5);
j=15;
System.out.println("During modifyPoint " + "pt = " + pt t " and j =
" + j);
}
public static void main(String [] args) {
Point p = new Point(0,0);
int i = 10;
System.out.println("Before modifyPoint " + "p = " + p + " and i = "
+ i);
modifyPoint(p, i);
System.out.println("After modifyPoint " + "p = " + p + " and i = "
+ i);
}
}
 
J

Joshua Cranmer

JT said:
Lew said:
When I read SCJP study materials, they have always asserted
unequivocally "call-by-value" as the only correct answer to "how does
Java pass method arguments?"

-- Lew
Practical Java Programming Guide Peter Haggar
Praxis 1: Understand that parameters are passed by value, not by reference

import java.awt.Point;
public class PassByValue {
public static void modifyPoint(Point pt, int j){
pt.setLocation(5,5);
j=15;
System.out.println("During modifyPoint " + "pt = " + pt t " and j =
" + j);
}
public static void main(String [] args) {
Point p = new Point(0,0);
int i = 10;
System.out.println("Before modifyPoint " + "p = " + p + " and i = "
+ i);
modifyPoint(p, i);
System.out.println("After modifyPoint " + "p = " + p + " and i = " +
i);
}
}

Here's what I get as output (after changing 'pt t' to 'pt +'):
Before modifyPoint p = java.awt.Point[x=0,y=0] and i = 10
During modifyPoint pt = java.awt.Point[x=5,y=5] and j = 15
After modifyPoint p = java.awt.Point[x=5,y=5] and i = 10

Primitive types are passed by value, *but* Objects are passed by
reference. Or, if you want to think of it like this, an Object is really
a pointer, so when you pass Objects, you are passing the address of the
Object.
 
P

Patricia Shanahan

Joshua Cranmer wrote:
....
Primitive types are passed by value, *but* Objects are passed by
reference. Or, if you want to think of it like this, an Object is really
a pointer, so when you pass Objects, you are passing the address of the
Object.

I think using the same word, "Object", for both the pointer and the
thing it points to is unnecessarily confusing. Why not "reference" for
the pointer, and "object" for the pointee?

Also, Java references are opaque pointers. They may be addresses. They
may be phone numbers :). They may be something totally weird. That is
an implementation matter.

In the object-by-reference model, how do you explain null reference
behavior. There is no object to pass, but you can pass a null reference
as a parameter, and only get a NullPointerException if the callee tries
to dereference it.

Patricia
 
M

Mike Schilling

Patricia said:
Joshua Cranmer wrote:
...

I think using the same word, "Object", for both the pointer and the
thing it points to is unnecessarily confusing. Why not "reference" for
the pointer, and "object" for the pointee?

Also, Java references are opaque pointers. They may be addresses. They
may be phone numbers :). They may be something totally weird. That is
an implementation matter.

Are there languages where this isn't true? I don't know of any C or C++
implementations that don't use machine addresses as pointers, but I presume
an implementation that used something else would be strictly conforming.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top