Re: null terminated strings
- From: GreyCloud <mist@xxxxxxxxxxx>
- Date: Sat, 11 Feb 2006 13:35:11 -0700
Dave Froble wrote:
Dave Weatherall wrote:
On Sat, 11 Feb 2006 03:34:57 UTC, Dave Froble <davef@xxxxxxxxxxxxx> wrote:
Larry Kilgallen wrote:
Karsten Nyblad wrote:
Let a pointer in C point to an element in an array. Then the pointer + 1 will point to the next element in the array. (Assuming that the type of the pointer matches the element type of the array.) But I wonder how many people use that feature today.
In article <43ED3339.DBAE181E@xxxxxxxxxxxx>, JF Mezei <jfmezei.spamnot@xxxxxxxxxxxx> writes:
Larry Kilgallen wrote:
Whereas I was wondering how many will remember how it works when they
write such code.
Pointer arithmetic in C is well defined.
But if Karsten is correct that not many people use the feature, the
fact that it is well defined will not help them remember it.
Earlier in this thread, at least I think so, the claim was made that C was designed to have the capabilities of PDP-11 assembler.
I know of no other language that handles arrays in such a manner. Having used a number of assembly languages, I understand what it's doing. What I don't understand is why such a thing was included in any language that's more than an assembler. I've been thinking rather hard on the technique, and I challenge anyone to provide a need for such that isn't more easily done in another manner.
What it suggests to me is the capability of looking at some part of an array element, not necessarily including the first byte/bit in the array.
Dave
consider the autoincrement/decrement addressing modes on pdp and VAX. The value of the index register was incremented according
to the size of the otem being operated on :-
movb (r5)+, r0 ; r5 bumped up one location
movw (r5)+, r0 ; r5 bumped 2
movl (r5)+, r0 ; r5 bumped 4
This is the behaviour that the C pointer arithmetic is following, methinks. It allowed the compiler to translate
p++ or p = p +1
to the correct autoincrement instruction.
Actually if you look at the assembler code generated by many VAX compilers you might find the same constructs. Certainly ISTR VAX-Fortran generating them for x = y DO loops. Once you get to arrays
of structures that are not 'natural' the compiler need to generate ADD instructions using sizeof(array element).
In the 80s, I took on responsibility for some Macro-11 that was full of statements along the lines of
cmp (r0)+,(r0)+
which was just to avoid
add #4,r0
It saved a word ISTR. I wasn't sure the two memory fetches were worth it and put the ADD back. (not to mention the possibility of an access
violation from (r0) when moved to VMS). To be fair the code had been wriiten originally for a small memory pdp (16K?) inder DOS-11. By the time I picked it up it was on RSX (32K non-overlayed tasks) looking to
VMS.
I may have mis-read your point. If so I apologise in advance.
You read it correctly. The point is, why does a language need a special pointer data type when the compiler knows the structure, even if it's a user defined structure. As Larry, Tom, and I have been saying, other languages do not need this, they automatically handle the offsets. A normal numeric data type is sufficient for indicating an element in the structure.
C offers too many opportunities for mistakes.
Quite correctly stated.
I was pointing this out in another newsgroup about big-endian machines like the G5 when doing string manipulation with pointers. It seems that some compilers aren't written correctly to handle this issue properly.
--
Where are we going?
And why am I in this handbasket?
.
- References:
- Re: null terminated strings
- From: Dave Froble
- Re: null terminated strings
- From: Keith A. Lewis
- Re: null terminated strings
- From: Bill Gunshannon
- Re: null terminated strings
- From: Karsten Nyblad
- Re: null terminated strings
- From: Larry Kilgallen
- Re: null terminated strings
- From: Karsten Nyblad
- Re: null terminated strings
- From: Larry Kilgallen
- Re: null terminated strings
- From: JF Mezei
- Re: null terminated strings
- From: Larry Kilgallen
- Re: null terminated strings
- From: Dave Froble
- Re: null terminated strings
- From: Dave Weatherall
- Re: null terminated strings
- From: Dave Froble
- Re: null terminated strings
- Prev by Date: Re: Another sad day for VMS...
- Next by Date: Re: PDPs in the news!
- Previous by thread: Re: null terminated strings
- Next by thread: Re: null terminated strings
- Index(es):
Relevant Pages
|