является той проблемой с проблемой компилятора macos g ++ (при использовании оптимизации)?
Проблема состоит в том, что при распределении с () или {} объектом с пустым конструктором, компилятор неверно думает, что объект ненициализируется, и, таким образом, не делает неконтролируемые оптимизации.
emere, что emere, что -то, эмеризации, что -то), что emere -emere. к вызову к печати, который будет печатать участники объекта.
Компилятор, однако должен знать, что память объекта была обновлена по () или {}, , даже если класс имеет пустой конструктор, и что он, таким образом, не может игнорировать значения членов объекта. Кажется, проблема только Apple. < /P>
Вот код: < /p>
#include "stdio.h"
#include
class ThirdPartyWidget {
public:
ThirdPartyWidget() { /* constructor does nothing, hence members are left uninitialized */ }
int data;
void p() {
// print object values
printf("this %p (%lu): data: %d\n", this, sizeof(*this), data);
}
void px() {
// dump the object in hexadecimal
printf("px: this %p (%zu): ", this, sizeof(*this));
unsigned char * c = (unsigned char *)this;
for (int i=0; i < sizeof(*this); i++) {
printf(" %02x", *c++);
}
printf("\n");
}
};
int main() {
// allocate on the stack, uninitialized
ThirdPartyWidget w0;
// NO initialization.
// 'w1->data' contains uninitialized value, so the
// compiler is allowed to ignore those values when p()
// is initially called
auto w1 = new ThirdPartyWidget;
// Value initialization with parenthesis.
// The memory for w2 is *first* zeroed out, even though the constructor does nothing, so
// the compiler should know that 'w2->data' is valid.
auto w2 = new ThirdPartyWidget();
// List initialization with empty braces.
// This also performs value initialization.
auto w3 = new ThirdPartyWidget{};
printf("\nw0\n");
// w0 is uninitialized so the MacOS compiler correctly
// doesn't bother to pass any values to printf
w0.p();
w0.px();
printf("\nw1\n");
// w1 is uninitialized so the MacOS compiler correctly
// doesn't bother to pass any values to printf
w1->p();
w1->px();
printf("\nw2\n");
// at this point the MacOS compiler incorrectly
// thinks that the members variables of w2
// are NOT initialized, so it ignores them and
// passes nothing to printf()
w2->p();
w2->px();
printf("\nw3\n");
// at this point the MacOS compiler incorrectly
// thinks that the members variables of w3
// are NOT initialized, so it ignores them and
// passes nothing to printf()
w3->p();
w3->px();
// since px() has been called, the MacOS compiler
// knows that the members
// variables are now valid, so it stops ignoring them
w3->p();
return 0;
}
< /code>
Скомпилировано с: < /p>
g++ -O3 -std=c++23 buggy_cpp_initialization.cpp -o buggy_cpp_initialization.bin
< /code>
и вывод: < /p>
buggy_cpp_initialization with O3 opt
w0
this 0x16f5131dc (4): data: 154385960
1000006c0: b9400268 ldr w8, [x19]
1000006c4: a900a3f9 stp x25, x8, [sp, #0x8]
1000006c8: f90003f3 str x19, [sp]
1000006cc: aa1403e0 mov x0, x20
1000006d0: 9400000c bl 0x100000700
< /code>
Компилятор < /p>
g++ --version
Apple clang version 17.0.0 (clang-1700.0.13.5)
Target: arm64-apple-darwin24.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Подробнее здесь: https://stackoverflow.com/questions/796 ... 700-0-13-5
MacOS Clang Compiler выпуск (Apple Clang версия 17.0.0 (Clang-1700.0.13.5)))) ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Задача Leetcode 1700. Количество студентов, которые не могут пообедать
Anonymous » » в форуме Python - 0 Ответы
- 44 Просмотры
-
Последнее сообщение Anonymous
-