Re: contiguity of arrays

From: Wojtek Lerch (Wojtek_L_at_yahoo.ca)
Date: 10/19/04


Date: Tue, 19 Oct 2004 16:14:12 -0400

David Hopwood wrote:
> Wojtek Lerch wrote:
>> If the struct type is not an array type, the definition of "array
>> type" doesn't support your position, does it?
>
> The standard never defines "array", but if array means "object of array
> type",
> then it follows from 6.3.2.3 #7 that there is a valid conversion from a
> pointer to the above struct, to a pointer to an array of (at least) 3 ints.

No it doesn't. Couldn't arrays of ints have stricter alignment
requirements than structs of ints or simple ints?

>> Can you find words somewhere else in the standard that allow you to
>> access the struct as an array?
>
> 6.3.2.3 #7. Strictly speaking, that clause doesn't say that the converted
> pointer points to the objects (of types compatible with the target type)
> that are stored at the same location as the original pointer -- but that is
> clearly the intent. Otherwise, what is the purpose of casts between
> pointers of different types?

I don't know, but just because there's no obvious purpose it doesn't
mean that any interpretation that gives it a purpose must necessarily be
correct.

Anyway, you can get around that by converting to a character pointer
first. The byte that the character pointer points to is guaranteed be
the first byte of both objects.

>> And does it matter? 6.5.6p8 defines pointer math in terms of the
>> array that the object *is* an element of.
>
> That's because there is only one such array for a given effective type.

Or none at all. If an object is declared as a struct, its effective
type is not an array type. If an allocated object hasn't been written
to yet, it has no effective type. But you're claiming that both are
arrays of more than one int for the purpose of 6.5.6p8. Pointer math
has nothing to do with the effective type of the object you're pointing to.

> However, there are in general many arrays that have the object as an
> element, and in this case there is a conversion from a pointer-to-struct
> to a pointer to an int array.

Ah, so you mean that the first of the three ints in the struct is an
element of an array of two ints, and of an array of three ints, at the
same time? But doesn't that mean that if I take a pointer to it and add
two, then dereferencing the result is both defined and undefined at the
same time? Actually, no: it violates a "shall" in 6.5.6p8, and
therefore it is undefined. You're not going to agree with that, are you?...



Relevant Pages

  • Re: question about pointer define
    ... p is a pointer to an array of 3 ints. ... Note that we considered this conversion rule in two different ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... an array of struct. ... You cannot assign a pointer to struct to a long* ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)
  • Re: Looking for a more elegant way to do memory offsets
    ... The mallocfunction returns memory that is "suitably aligned" ... >which in it contains the pointer to the first element in the linked ... and here a "struct dem_mem" does not contain a pointer to another ... This is not a linked-list access, but rather an array access: ...
    (comp.lang.c)
  • Re: Malloc Query
    ... the arena is an array even though the program using it does not care. ... since the pointer will be of the expected type. ... _every_ struct such that it can be the element type of an array? ... the alignment requirement, to allow for arrays... ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)