Re: null terminated strings



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?
.



Relevant Pages

  • Re: Need help to port VAX code to Alpha and to Itaninum
    ... Not really, the original code was wrong, and the compiler was not ... pointer to the start of the array. ... So you are trying to pass a pointer to a pointer to an array where you ... Also start looking at where you can add the "const" modifier to function ...
    (comp.os.vms)
  • Bounds checked arrays
    ... As everybody knows, the C language lacks ... When the state of this toggle is ON, the compiler ... Important is to know that the array updates ... We have just to allow him/her to specify what to do ...
    (comp.lang.c)
  • Re: Bounds checked arrays
    ... > more at each array access will not make any ... But for those applications which are real-time ... > When the state of this toggle is ON, the compiler ... The C language has a long-standing tradition ...
    (comp.lang.c)
  • Re: Bounds checked arrays
    ... > more at each array access will not make any ... > this problem in the C language to do their dirty ... > When the state of this toggle is ON, the compiler ... > We have just to allow him/her to specify what to do ...
    (comp.lang.c)
  • Re: decrement past beginning is valid?
    ... > What I meant by legal is that a compiler will compile it. ... > that an array is the same as a pointer. ... behave the same way on all platforms. ...
    (alt.comp.lang.learn.c-cpp)