"Larry I Smith" <larryXiXsmith@verizon.net> skrev i en meddelelse
news:WHB9e.24712$jd6.18946@trnddc07...[color=blue]
> Peter Koch Larsen wrote:[color=green]
>> "Larry I Smith" <larryXiXsmith@verizon.net> skrev i en meddelelse
>> news:Nzi9e.17695$jd6.749@trnddc07...[color=darkred]
>>>jonathan cano wrote:
>>>>Maciej Sobczak write "ms>":
>>>>ms> Moreover, I think there is no guarantee that new char[] will
>>>>ms> allocate the memory block that is correctly aligned for bish (b4
>>>>ms> in your code).> Certainly, there is no such guarantee for local
>>>>ms> char[] arrays (buf1 in your code).
>>>>
>>>>Thanks for pointing that out.
>>>>
>>>>presumably my original code sample could be fixed like this:
>>>>
>>>> 25 char buf1[sizeof(bish) + ALIGNMENT_BYTES];
>>>> + char *cp = buf1;
>>>>
>>>> + while (not_aligned(cp)) ++cp;
>>>>
>>>> 30 bish * b2 = new((void *)cp) bish(42);
>>>>
>>>>Which leaves the question: Is there a portable way to align a pointer
>>>>so that it meets a platforms strictest alignment requirements or is
>>>>such code doomed to non-portability?
>>>>
>>>>It seems technically feasible that this could be included in a
>>>>language standard although time and/or politics may have kept it out of
>>>>ISO 14882.
>>>>
>>>>--jfc
>>>>
>>>>
>>>> [ See
http://www.gotw.ca/resources/clcm.htm for info about ]
>>>> [ comp.lang.c++.moderated. First time posters: Do this! ]
>>>>
>>>From 'man malloc':
>>>
>>>"For calloc() and malloc(), the value returned is a pointer to the
>>>allocated memory, which is suitably aligned for any kind of variable,
>>>or NULL if the request fails."
>>>
>>>So, memory obtained via malloc() and family is suitably aligned
>>>for any type, including pointers.[/color]
>>
>> This is C, not C++. For C++ operator new only guarantees suitable
>> alignment
>> for the type one is newing for.
>>
>> /Peter[color=darkred]
>>>Regards,
>>>Larry
>>>
>>>
>>>--
>>>Anti-spam address, change each 'X' to '.' to reply directly.[/color]
>>
>>[/color]
>
> IIRC, the 'C' functions are part of C++.
>[/color]
Of course, but the OP specifically used new [], not malloc. If you read the
C++ standard, you will note that new does not have to be implemented via
malloc.
/Peter[color=blue]
> Regards,
> Larry
>
> --
> Anti-spam address, change each 'X' to '.' to reply directly.[/color]