assigning 'misaligned value' to a pointer 
Author Message
 assigning 'misaligned value' to a pointer

Does the following program exhibit any UB or IB if an object of type T is
misalligned would it have been placed at the location buffer+1, or does the
standard guarantee that such a "misalligned value" can be assigned to a pointer
of type pointer to T. Hope this makes some sense.

/* inclusion of this header will introduce a complete type T */
#include "my_type.h"
int main(void)
{
    char buffer[sizeof(T) + sizeof(T)];
    T *p = buffer + 1;
    return 0;

Quote:
}



Mon, 14 Feb 2005 21:16:40 GMT  
 assigning 'misaligned value' to a pointer


Quote:
> Does the following program exhibit any UB or IB if an object of type T
> is misalligned would it have been placed at the location buffer+1, or
> does the standard guarantee that such a "misalligned value" can be
> assigned to a pointer of type pointer to T. Hope this makes some
> sense.

> /* inclusion of this header will introduce a complete type T */
> #include "my_type.h"
> int main(void)
> {
>     char buffer[sizeof(T) + sizeof(T)];

Wrong. You may have misalignment problems. You should define it:

   T buffer[2];

Quote:
>     T *p = buffer + 1;
>     return 0;
> }

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.


Mon, 14 Feb 2005 23:05:49 GMT  
 assigning 'misaligned value' to a pointer

Quote:
>Does the following program exhibit any UB or IB if an object of type T is
>misalligned would it have been placed at the location buffer+1, or does the
>standard guarantee that such a "misalligned value" can be assigned to a pointer
>of type pointer to T. Hope this makes some sense.

Undefined behaviour.

Quote:
>/* inclusion of this header will introduce a complete type T */
>#include "my_type.h"
>int main(void)
>{
>    char buffer[sizeof(T) + sizeof(T)];
>    T *p = buffer + 1;

This requires a diagnostic, conversions between non-void pointers of
different types are never performed implicitly.

    T *p = (T *)(buffer + 1);

results in undefined behaviour if there is an alignment problem:

       [#7]  A  pointer  to  an  object  or  incomplete type may be
       converted to a pointer to a different object  or  incomplete
       type.   If the resulting pointer is not correctly aligned
       for  the  pointed-to  type,  the  behavior   is   undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 14 Feb 2005 23:08:25 GMT  
 assigning 'misaligned value' to a pointer
"Emmanuel Delahaye"

Quote:

> Wrong. You may have misalignment problems. You should define it:

This would defeat the purpose of the code, wouldn't it? You did notice the
question I had, didn't you?


Mon, 14 Feb 2005 23:15:39 GMT  
 assigning 'misaligned value' to a pointer
"Dan Pop"

Quote:


> >/* inclusion of this header will introduce a complete type T */
> >#include "my_type.h"
> >int main(void)
> >{
> >    char buffer[sizeof(T) + sizeof(T)];
> >    T *p = buffer + 1;

> This requires a diagnostic, conversions between non-void pointers of
> different types are never performed implicitly.

Yes, my bad.

Quote:
>     T *p = (T *)(buffer + 1);

> results in undefined behaviour if there is an alignment problem:

>        [#7]  A  pointer  to  an  object  or  incomplete type may be
>        converted to a pointer to a different object  or  incomplete
>        type.   If the resulting pointer is not correctly aligned
>        for  the  pointed-to  type,  the  behavior   is   undefined.

Thank you, must have overlooked this one in the C standard.


Mon, 14 Feb 2005 23:23:52 GMT  
 assigning 'misaligned value' to a pointer


Quote:
>> Wrong. You may have misalignment problems. You should define it:

> This would defeat the purpose of the code, wouldn't it? You did notice
> the question I had, didn't you?

In fact, after having thought more, your code is correct. Sorry about that.
sizeof takes padding byes too.

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.



Mon, 14 Feb 2005 23:34:58 GMT  
 assigning 'misaligned value' to a pointer
Hi,

While we are on this subject, how about the following example:

    typedef struct
    {
        int data1;
    } struct_1;

    typedef struct
    {
        int data1;
        int data2;
    } struct_2;

    struct_1 *ptr1;
    struct_2 *ptr2;

    ptr2 = (struct_2*) malloc (sizeof (struct_2));
    ptr2->data1 = 12345;

    ptr1 = (struct_1*) ptr2;
    ptr1->data1;    /* Does this guarantee to return 12345 ? */

Is the above code valid, or a buggy one?  Thanks.

Regards,

Bill



Mon, 14 Feb 2005 23:36:00 GMT  
 assigning 'misaligned value' to a pointer


Quote:
> In fact, after having thought more, your code is correct.

<...>

I meant this part:

{
   char buffer[sizeof(T) + sizeof(T)];
   ...

Quote:
}

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.


Mon, 14 Feb 2005 23:39:04 GMT  
 assigning 'misaligned value' to a pointer
"Emmanuel Delahaye"

Quote:


> >> Wrong. You may have misalignment problems. You should define it:

> > This would defeat the purpose of the code, wouldn't it? You did notice
> > the question I had, didn't you?

> In fact, after having thought more, your code is correct. Sorry about that.
> sizeof takes padding byes too.

I do not know what you mean by all this (especially the very last sentence), but
never mind; Dan Pop already answered my question.


Mon, 14 Feb 2005 23:39:08 GMT  
 assigning 'misaligned value' to a pointer


Quote:
> I do not know what you mean by all this (especially the very last
> sentence)<...>

"sizeof takes padding bytes too."

Better like that?

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.



Mon, 14 Feb 2005 23:41:13 GMT  
 assigning 'misaligned value' to a pointer


Quote:
> While we are on this subject, how about the following example:

>     typedef struct
>     {
>         int data1;
>     } struct_1;

>     typedef struct
>     {
>         int data1;
>         int data2;
>     } struct_2;

>     struct_1 *ptr1;
>     struct_2 *ptr2;

>     ptr2 = (struct_2*) malloc (sizeof (struct_2));

No need for a cast here. Note that malloc() guarantees a correct alignment
for any type.

Quote:
>     ptr2->data1 = 12345;

>     ptr1 = (struct_1*) ptr2;
>     ptr1->data1;    /* Does this guarantee to return 12345 ? */

Yes.

Quote:
> Is the above code valid, or a buggy one?  Thanks.

It's valid, as long as the first elements of the structures have the same
types. This trick only works portably with the first element.

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.



Mon, 14 Feb 2005 23:45:42 GMT  
 assigning 'misaligned value' to a pointer

Quote:



>>> Wrong. You may have misalignment problems. You should define it:

>> This would defeat the purpose of the code, wouldn't it? You did notice
>> the question I had, didn't you?

> In fact, after having thought more, your code is correct. Sorry about that.
> sizeof takes padding byes too.

Since it's an array of char even the start of it isn't necessarily
aligned correctly to store a T object.


Mon, 14 Feb 2005 23:48:00 GMT  
 assigning 'misaligned value' to a pointer

Quote:

> Does the following program exhibit any UB or IB if an object of type T is
> misalligned would it have been placed at the location buffer+1, or does the
> standard guarantee that such a "misalligned value" can be assigned to a pointer
> of type pointer to T. Hope this makes some sense.

> /* inclusion of this header will introduce a complete type T */
> #include "my_type.h"
> int main(void)
> {
>     char buffer[sizeof(T) + sizeof(T)];
>     T *p = buffer + 1;
>     return 0;
> }

    A diagnostic is required for the attempted conversion of
the `char*' pointer value `buffer+1' to the `T*' pointer `p'.
(Unless `T' turns out to be equivalent to `char', of course.)

    If you write a cast to perform the conversion explicitly,
as either of

        T *p = (T*)(buffer + 1);
        T *p = ((T*) buffer) + 1;

the result of the conversion is implementation-defined, and
the pointer `p' may or may not be usable.  AFAIK, though, it
is all right to form this possibly useless value if you don't
actually try to use it.   ("AFAIK" is somewhat less than 100%,
though -- especially when it comes to details of constructs
I would never personally use.)

--



Mon, 14 Feb 2005 23:25:17 GMT  
 assigning 'misaligned value' to a pointer

Quote:
>While we are on this subject, how about the following example:

>    typedef struct
>    {
>        int data1;
>    } struct_1;

>    typedef struct
>    {
>        int data1;
>        int data2;
>    } struct_2;

>    struct_1 *ptr1;
>    struct_2 *ptr2;

>    ptr2 = (struct_2*) malloc (sizeof (struct_2));
>    ptr2->data1 = 12345;

>    ptr1 = (struct_1*) ptr2;
>    ptr1->data1;    /* Does this guarantee to return 12345 ? */

>Is the above code valid, or a buggy one?  Thanks.

I think the code is valid, assuming that the malloc call succeeds.

The relevant text of the standard is:

       If  a  union  contains
       several structures that share a common initial sequence (see
       below), and if the union object currently  contains  one  of
       these  structures,  it  is  permitted  to inspect the common
       initial part of any of them anywhere that a  declaration  of
       the  completed type of the union is visible.  Two structures
       share a common initial  sequence  if  corresponding  members
       have compatible types (and, for bit-fields, the same widths)
       for a sequence of one or more initial members.

Of course, the aliasing is done with pointer arithmetic in our example,
not with a union, but the issue is otherwise the same.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 15 Feb 2005 00:37:35 GMT  
 assigning 'misaligned value' to a pointer

Quote:


>>> Wrong. You may have misalignment problems. You should define it:

>> This would defeat the purpose of the code, wouldn't it? You did notice
>> the question I had, didn't you?

>In fact, after having thought more, your code is correct.

Nope, it isn't.

Quote:
>Sorry about that.  sizeof takes padding byes too.

But this still doesn't guarantee that the array of char will be correctly
aligned for any kind of T access.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 15 Feb 2005 00:28:31 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. CArchive: can't assign pointer to archive instance

2. Value, value, who's got the value?

3. Using FSCANF to assign values with pointers

4. Maximum pointer value assigned with new or malloc

5. I can't get the value of datagrid's edit textbox

6. I can't find the value of DataGrid's edit textbox

7. error C2105: '++' needs l-value

8. C2102: '&' requires l-value

9. 'atoi' return value - help

10. 'USE_MFC'-macro value

11. Global Structure doesn't retain it's value

12. Removing a value from a set of values or'd together

 

 
Powered by phpBB® Forum Software