Pointers with C- a query

S

s.subbarayan

Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
Regards,
s.subbarayan
 
M

Martin Ambuhl

s.subbarayan said:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?

Why in the world would anyone want to? Your question is similar to
asking "why don't people just cut off their left arms?".
 
K

Kurt Watzka

s.subbarayan said:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?

Java is not "able to do this without pointers". Anything derived from
Object _is_ represented as a pointer (under a different name) in Java.
If you follow a null reference, you get an exception in Java.
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.

You could make _every_ object a pointer and get rid of the syntactic
differences between "pointer to user defined type" and "user defined
type" in a language similar to C. Just forbid storage class "auto"
for anything that is not a primitive built-in type.
While pointers provide flexibility most bugs are with respect to
pointers.
So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

Forbid pointer arithmetic, add bounds checks to array access? Forbid the
use of types without a specifed (and matching) range as array indices?
Make free() take a pointer to the pointer to be disposed, and keep a
list of all pointer variables in your program so that free can set
the pointer _and all other pointer variables holding the same value_
to a trap representation? Forbid type casts on pointers?

While we are at it: Add explicit preconditions and postconditions
to functions. Add explicit conditions for validitiy to every user
defined type and make the language check those before and after every
use of an object of the user defined type. Make loop variants explicit.
Make loop invariants explicit.

And, last but not least: Change the name of the language. C is a language
that does not make you pay for things you don't need. If you need them,
you can implement them in C, and pay for them.

Kurt Watzka
 
C

Case

s.subbarayan said:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.

When you pass around objects in a Java program you're actually
passing around 'pointers' (references). In Java you don't write
the * and & as you would do in C because working with 'pointers'
has been built into the language.

I would argue that it is impossible to write well structured C
programs without using pointers. There are several applications
for pointers, which are hard/stupid to implement without:

a. Passing large chucks of data to functions (avoiding making
local copies.
b. Passing data to a function to be modified inside.
c. Various 'object-oriented' programming techniques using function
pointers.
d. Creating variable size data-structures like trees, lists, ...
e. ...

Perhaps we could say that imperative programming languages do well
with an abstract representation of a memory address (which 'is' a
pointer)? Well at least this might result in some interesting
responses. ;-)

Kees alias Case
 
L

Leor Zolman

Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?

Java is an object-oriented language, and uses the "reference" type to refer
to objects. Primitive types are handled more-or-less the same way as they
are in C. Thus there's a dichotomy in Java: objects gets referred to only
via references (to facilitate performance and garbage collection), while
stand-alone ("naked") primitives are referred to directly by name, as in C,
for performance reasons (note there's no garbage collection for naked
primitives.). Primitives in Java rarely need to be accessed indirectly;
when they do, you in fact have to wrap them in an object and then use a
reference to that object.

C, on the other hand, has only one mechanism to refer to data indirectly,
and that's the pointer. It is the only game in town; it reflects well C's
nature of being "close to the machine", and allows the use of indirection
with or without dynamic allocation, and even with garbage collection if you
take the trouble to set up a custom framework for it. Pointers are the most
general-purpose way to do it, and provided the facilities necessary to make
C the first viable high-level (relatively speaking) systems programming
language. If you modified C to "remove pointers", it wouldn't be C any
longer. C++, for example, provides its own distinct flavor of reference
type, suitable for off-loading a number of things that, in C, have to be
done with pointers. But references aren't a universal replacement for
pointers even in C++.
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

The potential for pointer bugs is just the price you pay for the low-level
control C gives you. It is essentially a choice (made by /someone/, if not
you personally) regarding what language to use. If you need the greatest
possible "micro-management" of your application's behavior, you end up
using C. If you're willing to pay some price (in terms of either
performance, or learning curve struggles, or code size, or whatever), you
might choose to use a different language such as Java or C++ or Python or
something else.
-leor
 
M

Malcolm

s.subbarayan said:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows > java
able to do this with out pointers?All computer programs need to address memory (ie use pointers) internally. C
makes this fairly transparent to the user. Other languages hide the
underlying operation, at the cost of usually making code slower, but with
the advantage that it is less easy to access memory illegally.You could try writing a "C--" (without pointers). When you try to write real
programs with it you will probably find that you need at the very least an
expandable array, and probably a "reference" scheme as well.
 
E

Eric Sosman

Malcolm said:
able to do this with out pointers?

All computer programs need to address memory (ie use pointers) internally.

Off-topic nit: Alan Turing demonstrated that this is false.
 
M

Martin Dickopp

Eric Sosman said:
Off-topic nit: Alan Turing demonstrated that this is false.

Even a Turing machine has a "pointer" as a part of its state: the
current tape position.

Martin
 
M

Michael Wojcik

Even a Turing machine has a "pointer" as a part of its state: the
current tape position.

An n-PDA (n > 1) is equivalent to a TM. I don't see any pointer-
equivalent in an n-PDA.

Pointers aren't necessary. They're just very convenient. And to
answer the OP's question: if pointers were removed from C, it would
no longer be C.
 
D

Dan Pop

In said:
I had this following doubt,while java is able to carryon with out
pointers

Not true. It just doesn't call them pointers and doesn't make as flexible
as the C pointers.
why C language cant be modified to remove pointer?

Try to engage your brain and explain the advantages of a C without
pointers. What class(es) of applications would be better addressed by
such a language?

Removing pointers from C simply because newbies have a hard time getting
them right is NOT going to happen: one is supposed to be a C newbie for
a few months and something better than that for the rest of his life as a
C programmer...

Dan
 
S

Stephen Sprunk

s.subbarayan said:
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?

Removing pointers from C is like removing the gas pedal from a car. It'd be
a lot easier to learn, but you wouldn't get anywhere.

References in Java, Perl, etc. are just pointers with garbage collection and
other abstractions to make them "safe". This conflicts with the primary
goal of C, which is to be as close to assembly as possible while remaining
(mostly) portable. Since the machine deals in pointers, any abstraction
above that necessarily means a loss in run-time efficiency.
Jus want to know alternatives to pointers which can be used with
C.

There is no alternative to pointers in a non-trivial C program because
they're a fundamental part of the language. If you want a language that
hides pointers from you, go learn Java or Perl.
While pointers provide flexibility most bugs are with respect to
pointers.

Perhaps among novices, but not among experienced programmers. Being able to
use pointers correctly is a key attribute of someone who really understands
C.

S
 
V

Viktor Lofgren

s.subbarayan said:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
Regards,
s.subbarayan

You got things wrong: Every object is a pointer in Java. You just don't see
it. Basic types such as int and short are not pointers. But everything else is.
 
M

Malcolm

Dan Pop said:
Try to engage your brain and explain the advantages of a C without >
pointers. What class(es) of applications would be better addressed > by
such a language?My MiniBasic doesn't use pointers. There is no PEEK or POKE. However it does
have a DIM statement and it is possible to redimension arrays, which of
course calls malloc() internally. Trying an out-of-bounds access will cause
an error.
The advantage of MiniBasic is that it is impossible, barring bugs on my
part, to cause undefined behaviour. The idea is to allow programs to be
extended by anyone who learnt a bit of BASIC from having a microcomputer
back in the olden days.
 
A

August Derleth

It's already in use by how many people?

Well, the implementors, for a few.

Good point, I suppose: C-- is probably a solution looking for a problem,
at least at this stage. Maybe when seriously complex hardware
architectures filter down to the mass-market PCs (for example, RISC-like
out-of-order execution and VLIW slot-filling), compiler designers will
want to palm off as much of the /real/ hard work to the backend folks,
making intermediate languages more interesting.

August "EPIC challenges to compiler design..." Derleth
 
M

Michael Wojcik

I would consider a top-of-stack position a pointer.

Sigh. I was going to address this proleptically, then didn't out
of laziness.

I can see the argument for calling the current tape position in a
TM a "pointer". It marks one of the positions on the tape as
current. Machine operations can change it.

The top of the stack in a PDA is not a pointer in any useful sense.
What makes a pointer a pointer per se is that it can be changed to
point to a different object. The top of the stack "points" to only
one thing: whatever is currently on top of the stack. "Changing"
this "pointer" has side effects: it's either a pop, which is a
destructive operation, or a push, which has much more limited
semantics than a "move tape" (or equivalently "move tape pointer")
operation.

Calling the top of the stack a pointer in this sense is uninteresting.
It's equivalent to calling any variable a "pointer". (That a stack
can be implemented with a moving pointer is utterly beside the point.)

--
Michael Wojcik (e-mail address removed)

Every allegiance to some community eventually involves such a fetish,
which functions as the disavowal of its founding crime: is not 'America'
the fetish of an infinitely open space enabling every individual to
pursue happiness in his or her own way? -- Slavoj Zizek
 
J

Joona I Palaste

You got things wrong: Every object is a pointer in Java. You just don't see
it. Basic types such as int and short are not pointers. But everything else is.

Your terminology is lax. Objects are not pointers or vice versa. Every
object *variable* is really an object *pointer* variable in Java, but
the things those variables' values point at are real, honest-to-gosh,
all-signing, all-dancing objects. How do you suppose pointers with
nothing to point at could ever be of any use?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
E

Eric Sosman

Michael said:
Sigh. I was going to address this proleptically, then didn't out
of laziness.

I can see the argument for calling the current tape position in a
TM a "pointer". It marks one of the positions on the tape as
current. Machine operations can change it.

Rhetorical question: If you were building a Turing machine,
how many bits would you use to hold the value of this "pointer?"
(Hint: The tape has a countably infinite number of cells.)

Rhetorical question: Since Turing himself allocated zero bits
of his conceptual machine to designate the position of "HERE,"
how many different values can his "pointer" take on? (Hint:
What is the Shannon entropy of a constant signal?)
> Calling the top of the stack a pointer in this sense is uninteresting.
> It's equivalent to calling any variable a "pointer". (That a stack
> can be implemented with a moving pointer is utterly beside the point.)

Exactly -- just as in the case of "HERE" in a Turing machine.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top