Re: Another String reversal question

From: Dave Harris (brangdon_at_cix.co.uk)
Date: 12/23/03


Date: 22 Dec 2003 21:09:33 -0500

dhruvbird@gmx.net (Dhruv) wrote (abridged):
> So, I guess adding 1 to the end of the array would mean that the
> higher 16-bits in the register would get incremented by 1 and
> the lower 16-bits would roll back to 0?

With some hardware, the higher 16 bits would be unchanged. Pointer
arithmetic only affected the low 16-bits; the CPU might not even
have been capable of 32-bit arithmetic directly.

Thus (char *) 0x1001ffff + 1 = (char *) 0x10010000 for these pointers,
and p > p+1.

(For some compilers, only the low 16 bits took part in pointer
comparisons, but this wasn't ratified by the standard. It meant
(char *)0x10014444 == (char *) 0x20024444, and you had to be
sure you had the right kind of NULL. Unpleasant.)

> So, isn't one past the end of the array also some memory that the
> running process might not bw owning?

It could be, but the C++ standard requires that the compiler make it
work. This may mean allocating one more byte than necessary. In
practice for alignment reasons you may need several bytes, but there
doesn't have to be a whole object there. In that respect your diagram
is misleading.

To allow one before the beginning, we would need sizeof(object) bytes,
which is more onerous because an object can be arbitrarily large.

-- Dave Harris, Nottingham, UK

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated. First time posters: Do this! ]



Relevant Pages

  • Re: struct initilization via memset
    ... >>will all initialize the pointer to a null pointer. ... By the time of the first ANSI C standard, C had been around for well ... over 10 years and C compilers were developed for quite a few different ... expression with a value of 0 cast to pointer to void. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: NULL in variadic function calls
    ... >> Standard. ... >> It is true that on most modern compilers and architectures, ... value is a null pointer: ... valid pointer value, representing an actual, valid memory ...
    (comp.unix.programmer)
  • Re: struct initilization via memset
    ... > By the time of the first ANSI C standard, C had been around for well ... > compilers generated code that was most efficient for their particular ... and the definition of the macro NULL. ... > expression with a value of 0 cast to pointer to void. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: A malloc question, part 2
    ... When you do pointer arithmetic, if you start with a well-aligned ... end with a well-alignment pointer. ... The C standard says that if you use a well-aligned pointer, ... other hardware, different things happen. ...
    (comp.lang.c)
  • Re: class variables: scoping
    ... >> standard, though there are variations you can try. ... > with other compilers as well. ... But you can't convert a pointer to member function into a void*, ... > that problems may arise in multithreaded applications. ...
    (comp.lang.cpp)

Loading