在内存中引用数组

时间:2016-10-05 20:17:26

标签: c++

我有这种内存布局:

0018FBD2 ?? ?? ?? ??    ?? ?? ?? ??
0018FBDA AA AA AA AA    BB BB BB BB  <- stuff I'm interested in
0018FBE2 ?? ?? ?? ??    ?? ?? ?? ??

在C中,我会这样做:

int* my_array = (int*) 0x18FBDA;
my_array[0]; // access

但是,我正在使用C ++,我想声明一个引用:

int (&my_array)[2] = (???) 0x18FBDA;

为了这样使用:

my_array[0]; // access

但正如你所看到的,我不知道如何施展它:

int (&my_array)[2] = (???) 0x18FBDA;

我该怎么做?它甚至可能吗?

2 个答案:

答案 0 :(得分:7)

我发现使用数组引用的概念有点复杂,比如提到的 tadman 。但你可以通过取消引用指针来完成任何类型的操作。

sysname   | release | version                      | nodename          | machine  | login
"Windows" | "7 x64" | "build 7601, Service Pack 1" | "WINDOWS MACHINE" | "x86-64" | "Admin" 

另外,如果你要进行这样的演员表演,不要让它看起来无辜。这样的事情应该在IMO中脱颖而出。

答案 1 :(得分:2)

我会选择C风格的指针。但是,如果你真的想要 C ++方式(它不是,不仅仅是你在问题中提出的那样):

char *decl = "(declare-const p0 Bool)";
char *assert = "(assert(= p0 true))";

Z3_ast ast = Z3_parse_smtlib2_string(ctx, (Z3_string)decl, 0, 0, 0, 0, 0, 0);
z3::expr eq1(ctx, ast);
solver.add(eq1);

//extract declarations from the solver, ctx or ast
...

//Parse the assert with the previous declarations
Z3_ast ast = Z3_parse_smtlib2_string(ctx, (Z3_string)assert, 0, 0, 0, num_decls, symbols, decl_list);
z3::expr eq2(ctx, ast);
solver.add(eq2);
solver.check();

这只是

的更清晰的符号
template<typename T, size_t S>
using arr = T[S];

auto& my_arr = *reinterpret_cast<arr<int,2>*>(0x18FBDA);