변수를 extern double&
으로 선언하고 포인터를 static const struct
에 사용하고 싶습니다. 어딘가 다른 변수를 실제로 다른 구조체의 멤버로 정의하려고합니다. 첨부 된 코드는 내가 예상 한대로 x86
에서 작동합니다.C++ : "extern reference"선언
문제는 내장 ARM
상황에서 작동하지 않는다는 것입니다 : 다른 모든 유효한 보이는 동안 초기화 static const struct
내부의 void* void_pointer
, 그냥 NULL
있다.
여기에서 시도해 볼 수있는 구문이 표준에서 정의되었거나 일부 상세 구현이 정의 되었습니까? 어떻게 디버깅합니까? 무엇이 잘못 될 수 있습니까? 실제로 여기서 일어나는 것은 무엇입니까?
#include <iostream>
// declare and define place where the actual content is stored
struct storage_struct { double double_array[2]; double double_variable; };
struct storage_struct db = {{3.0,4.0},1.0};
// declare "double_reference" as extern
extern double &double_reference;
// declare and defince a struct to hold the pointer to the extern reference:
struct target_struct { void *void_pointer; };
static const struct target_struct dut { &double_reference };
// and now connect the "extern refernce" to an actual value from the database-struct
double &double_reference = db.double_variable;
int main() {
std::cout << "testPrint with initial values:\n";
std::cout << "void pointer: '" << dut.void_pointer << "', value: '"
<< *(double *)dut.void_pointer << "'\n";
std::cout << "\n";
// change content in the database
db.double_variable = 11.0;
db.double_array[0] = 1444.0;
db.double_array[1] = 238947.0;
std::cout << "adress of storage_struct: " << &db << "\n";
std::cout << "adress of double_variable: " << &db.double_variable << "\n";
std::cout << "adress of double_reference: " << &double_reference << "\n";
std::cout << "\n";
std::cout << "testPrint with changed values:\n";
std::cout << "void pointer: '" << dut.void_pointer << "', value: '"
<< *(double *)dut.void_pointer << "'\n";
std::cout << "\n";
return 0;
}
다음으로 컴파일 및 실행 : g++ -std=c++11 -o test main.cpp && ./test
- 매력처럼 작동합니다. 에서 ARM이를 플래시 μC - void_pointer
는 × 00 (그것은 또한 데비안 ARM
에 작동합니다)입니다
변수가 'extern double &'로 처음 선언 된 이유는 무엇입니까? 링커가 연결합니다. 이 테스트의 또 다른 버전에서는 서로 다른 부분이 여러 컴파일 단위로 분산되어 있습니다. x86에서 작동합니다. – user3520187
변수가 여러 번역 단위에 분산되어 있으면 정적 초기화 순서로 실패합니다. 링커는 참조를 찾습니다. 그렇지만 포인터가 초기화 될 때 참조가 초기화되지 않았으므로 포인터의 값은 지정되지 않습니다. – seldon
그래서'static const struct dut'을 일부 init-function으로 옮기면 μC 측의 실제 문제를 해결할 수 있습니다 ** ** 정적 초기화 순서 실패가 실제 문제라고 확신하지는 못합니다. 내 질문에 게시 된 코드는 TU에 있으며 μC에서는 작동하지 않습니다 ... 그리고 생성자가 없습니다 ...? – user3520187