Skip to content

Commit 2811d11

Browse files
committed
[ub] Applied suggestions from code review
1 parent 31fbb5f commit 2811d11

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

source/ub.tex

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
\begin{codeblock}
2929
void f()
3030
{
31-
void *p = malloc(sizeof(int) + sizeof(float)); // undefined behavior, cannot create
32-
// both int and float in same place
33-
int& i = *reinterpret_cast<int*>(p);
34-
float& f = *reinterpret_cast<float*>(p);
31+
void *p = malloc(sizeof(int) + sizeof(float));
32+
*reinterpret_cast<int*>(p) = 0;
33+
*reinterpret_cast<float*>(p) = 0.0f; // undefined behavior, cannot create
34+
// both int and float in same place
3535
}
3636
\end{codeblock}
3737
\end{example}
@@ -126,7 +126,7 @@
126126
~S() {}
127127
};
128128

129-
float f() {
129+
void f() {
130130
S* p = new S;
131131
p->~S();
132132
delete p; // undefined behavior, operand of delete, lifetime has ended and \tcode{S}
@@ -739,12 +739,12 @@
739739
\pnum
740740
\begin{example}
741741
\begin{codeblock}
742-
int main() {
743-
__uint128_t x2 = -1;
744-
float f = x2; // undefined behavior on systems where the range of
745-
// representable values of float is [-max,+max] on system where
746-
// represetable values are [-inf,+inf] this would not be UB
747-
}
742+
int main() {
743+
__uint128_t x2 = -1;
744+
float f = x2; // undefined behavior on systems where the range of
745+
// representable values of float is [-max,+max] on system where
746+
// represetable values are [-inf,+inf] this would not be UB
747+
}
748748
\end{codeblock}
749749
\end{example}
750750

@@ -857,11 +857,11 @@
857857
\pnum
858858
\begin{example}
859859
\begin{codeblock}
860-
struct B { virtual ~B(); };
861-
void f() {
862-
B bs[1];
863-
B* dp = dynamic_cast<B*>(bs+1); // undefined behavior
864-
}
860+
struct B { virtual ~B(); };
861+
void f() {
862+
B bs[1];
863+
B* dp = dynamic_cast<B*>(bs+1); // undefined behavior
864+
}
865865
\end{codeblock}
866866
\end{example}
867867

0 commit comments

Comments
 (0)