R
Ishildur schrieb:
Ok, kannst du mir ein Codebeispiel geben, wie ich folgende Daten "korrekt" auslese:
Das wirklich richtig zu machen ist nicht mit ein paar Zeilen getan, und da ich mir nicht hundertprozentig sicher bin ob ich in diesem Fall wirklich recht habe, lasse ich das lieber mal. Es würde vermutlich auf etwas hinauslaufen, was ähnlich der zweiten Antwort (I have to disagree with many of the responses here...) auf eine ähnliche Frage bei Stack Overflow ist.
Du kannst ja mal selbst im Standard die Abschnitte 3.10.15 und 5.2.10 durchlesen und mit all ihren Implikationen verstehen, dann siehst du warum das nicht so einfach ist.
Denn dieser Code kopiert nun das dritte Oktett des ersten Integers in Val, das ja nun sicher nicht mehr 32bit aligned ist...
Es spielt keine Rolle, wie der Speicher angelegt wurde. Der Zugriff ist entscheidend. Ausgehend von der Annahme, dass uint8 ein [unsigned] char ist und geringere Anforderungen an das Alignment stellt, ist der Zugriff in Ordnung:
3.10.15 schrieb:
15 If a program attempts to access the stored value of an object through an lvalue of other than one of the fol-
lowing types the behavior is undefined:
— the dynamic type of the object,
— a cv-qualified version of the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of
the object,
— an aggregate or union type that includes one of the aforementioned types among its members (includ-
ing, recursively, a member of a subaggregate or contained union),
— a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
— a char or unsigned char type.
Janjan schrieb:
ZeigerTyp *zeiger = ...;
reinterpret_cast<int*>(zeiger);
Das heißt, ich weiß was ich mache, ich weiß dort liegen _int_ Daten für mich, also mecker nicht.
Wenn du dir die entsprechenden Stellen im Standard durchliest, wirst du sehen dass es so einfach nicht ist.