Byte Address Arithmetic Debate

S

Steve Pope

Frederick Gotham said:
Steve Pope:
Sorry I don't understand, could you please explain that?

Picture a computer memory that is both byte-addressable and
word-addressable, where a word is four bytes. The word
address 1000 (decimal) would address a word containing the
four bytes at byte addresses 4000, 4001, 4002, and 4003 (decimal).

I don't know of any modern machines that do this, but it has
been done and it can save code space.

Steve
 
F

Frederick Gotham

Steve Pope:
Picture a computer memory that is both byte-addressable and
word-addressable, where a word is four bytes. The word
address 1000 (decimal) would address a word containing the
four bytes at byte addresses 4000, 4001, 4002, and 4003 (decimal).

I don't know of any modern machines that do this, but it has
been done and it can save code space.


But I'm converting everything to char* beforehand, shouldn't that sort
everything out?
 
B

Bo Persson

Steve said:
Frederick Gotham said:
Steve Pope:
I don't see why there would be anything wrong with the following:

struct SomePOD {
int a;
char b;
int arr[5];
};

struct Base {
double a;
SomePOD b;
void *c;
};

struct Derived : Base {
double d;
Base e;
};

#include <cstddef>

template<class A,class B>
std::ptrdiff_t BytesBtwn(A const *const p,B const *const q)
{
return (char const volatile*)q - (char const volatile*)p;
}

int main()
{
Derived const volatile obj = Derived();

ptrdiff_t const i = BytesBtwn(obj.b.arr+2,&obj.e.b.a);
}

This would not give the difference in bytes on architectures
for which the address of an int is a word address.

I suspect that it just might work, either because a C++ byte will be that
same size as a word, or that casting to a char pointer will not be a
reinterpret_cast, but involve an actual conversion.

In either case you will get a byte distance, for an implementation specific
definition of a byte.
(Now, I admit not having seen such an architecture for 20
years or so, but they may still be around.)

We have others, that are word adressable and use special part word
operations to access individual characters. That makes the above casts even
more interesting. :)


Bo Persson
 
S

Steve Pope

Frederick Gotham said:
Steve Pope:
But I'm converting everything to char* beforehand, shouldn't that sort
everything out?

I'm not sure the language requires that an expression like
(char *) pint, where pint is a pointer to int, does the required
conversion.

It seems though things like malloc() would not generally work properly
if conversions like this were not done as one would naturally
expect, so maybe you can rely on it.

Steve
 
F

Frederick Gotham

Steve Pope:
I'm not sure the language requires that an expression like
(char *) pint, where pint is a pointer to int, does the required
conversion.


I think in does. If the Standard doesn't explicitly state this, then it
should.

I believe the Standard says somewhere that "void*" and "char*" must have
identical representation. (Let's forget for the moment that we're not
allowed access multiple members of a union).

union ByteAddress {
void *pv;
char *pc;
};

int i;

ByteAddress n;

n.pv = &i; /* This is definitely OK */

"pc" and "pv" should be identical right now. Therefore, we could do:

int arr[2];

ByteAddress a,b;

a.pv = arr;
b.pv = arr+1;

ptrdiff_t i = b.pc - a.pc;

(Again, I acknowledge that the Standard forbids use of unions in this
fashion.)

Anyway, I digress. If you don't like the following:

(char*)pint

, then I suppose you can write the following instead:

static_cast<char*>( static_cast<void*>(pint) );

(Actually this sounds utterly ridiculous as I write it -- programmers have
been casting to char* in C for decades...)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top