Re[3]: как избежать проблем с выравниванием?
От: gear nuke  
Дата: 16.10.09 16:12
Оценка: -1
Здравствуйте, bad_coder, Вы писали:

_>Ну вот те раз, а так!


Так предприняты усилия, и присвоение val2 некорректно. С val1 проблем нет.

7.20.3 Memory management functions
1. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to any type of object
and then used to access such an object or an array of such objects in the space allocated
(until the space is explicitly deallocated).

Это С, в С++ аналогично.

_>

_>void load(char const* buf)
_>{
_>unsigned short val1 = *reinterpret_cast<unsigned short const*>(buf);
_>buf += sizeof(unsigned short);
_>unsigned long val2 = *reinterpret_cast<unsigned long const*>(buf);
_>}


_>int main()
_>{
_>char* buf = new char[1000];
_>//.........
_>load(buf);
_>return 0;
_>}

_>
.
People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird (c) D.Knuth
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.