Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading Struct not Located at Four-boundary

Reply
Thread Tools

Reading Struct not Located at Four-boundary

 
 
thomas
Guest
Posts: n/a
 
      08-20-2010
Hi,

I have a struct A(undefined, can be any form) located at memory
pointed by "char *ptr".
I want to read it with "(A*)ptr".

Now I wonder if the position of ptr may affect the behavior when
accessing the struct.

Consider the following condition:
-----------------------------------------------
_ _ _ _ | _ _ _ _ |
0 1 2 3 4 5 6 7
position of pointer "ptr" = 2.

If the first element of struct A is an int-type one, it will span
positions 2~5.
The CPU will load 0~3, and then 4~7 to get the data of the int-type
member.
But we C++ programmers don't need to care the position of ptr, right?

(I remember some accesses to unbounded memory positions may get system
crash, but I cannot remember in which case. A little confused.)
 
Reply With Quote
 
 
 
 
thomas
Guest
Posts: n/a
 
      08-20-2010
On Aug 20, 5:00*pm, Paavo Helde <(E-Mail Removed)> wrote:
> thomas <(E-Mail Removed)> wrote in news:bb2ff772-9706-4b51-bcbb-
> (E-Mail Removed):
>
>
>
> > Hi,

>
> > I have a struct A(undefined, can be any form) located at memory
> > pointed by "char *ptr".
> > I want to read it with "(A*)ptr".

>
> > Now I wonder if the position of ptr may affect the behavior when
> > accessing the struct.

>
> > Consider the following condition:
> > -----------------------------------------------
> > _ _ _ _ | _ _ _ _ |
> > 0 1 2 3 * 4 5 6 7
> > position of pointer "ptr" = 2.

>
> > If the first element of struct A is an int-type one, it will span
> > positions 2~5.
> > The CPU will load 0~3, and then 4~7 to get the data of the int-type
> > member.

>
> Accessing unaligned data is UB, depending on the platform this may crash
> or produce wrong results. On some platforms it may works (notably x86),
> but with a performance penalty.
>
> For portable code one should memcpy the data in a suitable aligned buffer
> before casting it to int*.
>
> > But we C++ programmers don't need to care the position of ptr, right?

>
> We should care that our programs work (not only at the moment, but also
> on other systems and 20 years from now).
>
> Regards
> Paavo


OK.. One thing I want to make sure is whether "new/malloc" will
guarantee that the address of the allocated memory will be aligned
properly?
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-20-2010
thomas <(E-Mail Removed)>, on 20/08/2010 02:35:35, wrote:

> OK.. One thing I want to make sure is whether "new/malloc" will
> guarantee that the address of the allocated memory will be aligned
> properly?


I don't know about "malloc", but for "new" the answer is yes, it will be
properly allocated for the type with which "new" is called.

You can't obviously allocate via "new" for a type then cast the result
to another type and finally expect it to be correctly aligned for the
latter type: some cases will work, some others won't, but no guarantee
there - if I recall correctly.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-20-2010
On 08/20/10 09:35 PM, thomas wrote:
>
> OK.. One thing I want to make sure is whether "new/malloc" will
> guarantee that the address of the allocated memory will be aligned
> properly?


Yes.
--
Ian Collins
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-20-2010
Francesco S. Carta <(E-Mail Removed)>, on 20/08/2010 11:43:23, wrote:

> thomas <(E-Mail Removed)>, on 20/08/2010 02:35:35, wrote:
>
>> OK.. One thing I want to make sure is whether "new/malloc" will
>> guarantee that the address of the allocated memory will be aligned
>> properly?

>
> I don't know about "malloc", but for "new" the answer is yes, it will be
> properly allocated for the type with which "new" is called.


(the above should read "properly _aligned_ for" etcetera)

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-20-2010
On 20 aug, 12:35, thomas <(E-Mail Removed)> wrote:
> On Aug 20, 5:00*pm, Paavo Helde <(E-Mail Removed)> wrote:
>
>
>
> > thomas <(E-Mail Removed)> wrote in news:bb2ff772-9706-4b51-bcbb-
> > (E-Mail Removed):

>
> > > Hi,

>
> > > I have a struct A(undefined, can be any form) located at memory
> > > pointed by "char *ptr".
> > > I want to read it with "(A*)ptr".

>
> > > Now I wonder if the position of ptr may affect the behavior when
> > > accessing the struct.

>
> > > Consider the following condition:
> > > -----------------------------------------------
> > > _ _ _ _ | _ _ _ _ |
> > > 0 1 2 3 * 4 5 6 7
> > > position of pointer "ptr" = 2.

>
> > > If the first element of struct A is an int-type one, it will span
> > > positions 2~5.
> > > The CPU will load 0~3, and then 4~7 to get the data of the int-type
> > > member.

>
> > Accessing unaligned data is UB, depending on the platform this may crash
> > or produce wrong results. On some platforms it may works (notably x86),
> > but with a performance penalty.

>
> > For portable code one should memcpy the data in a suitable aligned buffer
> > before casting it to int*.

>
> > > But we C++ programmers don't need to care the position of ptr, right?

>
> > We should care that our programs work (not only at the moment, but also
> > on other systems and 20 years from now).

>
> > Regards
> > Paavo

>
> OK.. One thing I want to make sure is whether "new/malloc" will
> guarantee that the address of the allocated memory will be aligned
> properly?


Allocation functions return pointers to storage that is appropriately
aligned for objects of any type. You can provide your own allocation
functions, then these are also assumed to do the same.
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-20-2010
Öö Tiib <(E-Mail Removed)>, on 20/08/2010 02:49:39, wrote:

> On 20 aug, 12:35, thomas<(E-Mail Removed)> wrote:


<snip>

>> OK.. One thing I want to make sure is whether "new/malloc" will
>> guarantee that the address of the allocated memory will be aligned
>> properly?

>
> Allocation functions return pointers to storage that is appropriately
> aligned for objects of any type.


I'm not sure I understand this correctly, and if I do, I was not aware
of this: does that mean that "new" always allocates using the stricter
alignment requirements for any possible type _regardless_ of the type it
is called with?

I strongly suspect I misunderstood your statement.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
thomas
Guest
Posts: n/a
 
      08-20-2010
On Aug 20, 5:49*pm, Öö Tiib <(E-Mail Removed)> wrote:
> On 20 aug, 12:35, thomas <(E-Mail Removed)> wrote:
>
>
>
> > On Aug 20, 5:00*pm, Paavo Helde <(E-Mail Removed)> wrote:

>
> > > thomas <(E-Mail Removed)> wrote in news:bb2ff772-9706-4b51-bcbb-
> > > (E-Mail Removed):

>
> > > > Hi,

>
> > > > I have a struct A(undefined, can be any form) located at memory
> > > > pointed by "char *ptr".
> > > > I want to read it with "(A*)ptr".

>
> > > > Now I wonder if the position of ptr may affect the behavior when
> > > > accessing the struct.

>
> > > > Consider the following condition:
> > > > -----------------------------------------------
> > > > _ _ _ _ | _ _ _ _ |
> > > > 0 1 2 3 * 4 5 6 7
> > > > position of pointer "ptr" = 2.

>
> > > > If the first element of struct A is an int-type one, it will span
> > > > positions 2~5.
> > > > The CPU will load 0~3, and then 4~7 to get the data of the int-type
> > > > member.

>
> > > Accessing unaligned data is UB, depending on the platform this may crash
> > > or produce wrong results. On some platforms it may works (notably x86),
> > > but with a performance penalty.

>
> > > For portable code one should memcpy the data in a suitable aligned buffer
> > > before casting it to int*.

>
> > > > But we C++ programmers don't need to care the position of ptr, right?

>
> > > We should care that our programs work (not only at the moment, but also
> > > on other systems and 20 years from now).

>
> > > Regards
> > > Paavo

>
> > OK.. One thing I want to make sure is whether "new/malloc" will
> > guarantee that the address of the allocated memory will be aligned
> > properly?

>
> Allocation functions return pointers to storage that is appropriately
> aligned for objects of any type. You can provide your own allocation
> functions, then these are also assumed to do the same.


But if I use placement new, I can specify the address of a struct (may
be not properly aligned).
It's programmers' own risk to do this right?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-20-2010
On 08/20/10 10:06 PM, thomas wrote:
> On Aug 20, 5:49 pm, Öö Tiib<(E-Mail Removed)> wrote:
>>
>> Allocation functions return pointers to storage that is appropriately
>> aligned for objects of any type. You can provide your own allocation
>> functions, then these are also assumed to do the same.

>
> But if I use placement new, I can specify the address of a struct (may
> be not properly aligned).
> It's programmers' own risk to do this right?


Very much so.

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-20-2010
On 08/20/10 10:02 PM, Francesco S. Carta wrote:
> Öö Tiib <(E-Mail Removed)>, on 20/08/2010 02:49:39, wrote:
>
>> On 20 aug, 12:35, thomas<(E-Mail Removed)> wrote:

>
> <snip>
>
>>> OK.. One thing I want to make sure is whether "new/malloc" will
>>> guarantee that the address of the allocated memory will be aligned
>>> properly?

>>
>> Allocation functions return pointers to storage that is appropriately
>> aligned for objects of any type.

>
> I'm not sure I understand this correctly, and if I do, I was not aware
> of this: does that mean that "new" always allocates using the stricter
> alignment requirements for any possible type _regardless_ of the type it
> is called with?


operator new only knows the size of an object, not its type.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
Printer not located in new laptop from wireless network =?Utf-8?B?dG1jZ2luNzEwMzk1?= Wireless Networking 3 04-19-2006 02:15 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
To coerce or not...? struct sockaddr vs struct sockaddr_in James Harris C Programming 4 10-09-2003 10:06 PM



Advertisments