uintptr_t and array query

V

vippstar

I had once posted a message here on comp.lang.c, which asked if a
multi-dimensional array can be treated as a single-dimension array.

Message-ID: <38b37f10-252e-48f8-bffa-
(e-mail address removed)>

From what I've gathered, it is undefined behavior. Code such as:

#include <stdio.h>

int main(void) {
int m[2][2] = {0}, *p = m, i = 0;
while(i++ < 4) printf("%d\n", *p++);
return 0;
}

Invokes undefined behavior, an optimizer may ignore references after
(int *)m[0] + 1, because the object m[0] "contains" two ints only.

However I have this query now:

#include <stdint.h>
/* ... */
int m[2][2];
uintptr_t k = (uintptr_t)m;
uintptr_t j = (uintptr_t)m[1];

It is guaranteed that k + sizeof (int) * 2 == j. It's also guaranteed
that (int *)j == (int *)k.

Therefore, this code is correct:

#include <stdio.h>
#include <stdint.h>

int main(void) {
int m[2][2] = {0}, i = 0;
uintptr_t k = (uintptr_t)m;
while(i++ < 4) {
printf("%d\n", *(int *)k);
k += sizeof (int);
}
return 0;
}

There's three possibilities:

* The former code is not incorrect, unlike what I've been told
* The latter code is incorrect, to my surprise
* The former is incorrect, while the latter is correct.

Which one of them is it?
 
V

vippstar

No such thing is guaranteed. Consider a DOS compiler with "huge"
memory model. Each pointer is represented by a pair (segment, offset).
For starters, there is no reason to believe that the segment and
offset end up in the place in the uintptr_t where you believe it will,
so it could very well be that the difference between (uintptr_t) p and
(uintptr_t) (p+1) is 0x10000 * sizeof (*p), not sizeof (*p), as you
assume. Second, if p+1 is in a different segment than p, your code
breaks as well.

Thanks :). Yes, I know that two different representations might be
the same value (ie same pointer), but I hadn't thought of it.
Then it's case two,
* The latter code is incorrect, to my surprise

As Richard Heathfield said, treating a multi-dimensional array as a
single-dimension one in practice will work, but I wasn't truly
interested in that, I thought the standard was inconsistent on the
matter (but I was wrong).
 

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,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top