关于C++结构体与指针内存操作代码的技术问询
Hey there, let's break down each of your questions step by step using the code you shared:
1. What is Thing t{8,15}?
This is aggregate initialization in C++. Since Thing is a struct with no user-defined constructors, destructors, or assignment operators, it qualifies as an aggregate type. When you use curly braces like this, the values inside are assigned to the struct's members in the exact order they're declared. So:
8gets assigned tot.x(the first member ofThing)15gets assigned tot.y(the second member)
It has nothing to do with creating an integer array—this is strictly initializing the members of your Thing struct instance.
2. What does std::cout << "\nt = " << place[0] << ", " << place[1]; do?
You're totally on the right track! Let's unpack this line:
int* place = (int *)&t;takes the memory address of yourThinginstancetand casts it to anint*. Since the first member ofThingis anint x, this pointer now points directly tot.xin memory.- In C++,
place[0]is exactly equivalent to*(place + 0)—it dereferences the pointer to grab the value at the starting address (which ist.x, so 8). place[1]translates to*(place + 1). Since anintis typically 4 bytes on most systems, adding 1 to anint*moves the pointer forward by 4 bytes. With a default-structuredThing(no padding between members), this lands right ont.y, so it prints 15.
So yes, this line prints the values of t.x and t.y by accessing them via a pointer treated like an array.
3. What's the logic behind int y = *( int *)(( char *)&t + 4);?
This is a low-level memory manipulation trick—let's break it down piece by piece, since you're learning pointers and memory:
&t: Grabs the memory address of theThinginstancet(typeThing*).(char *)&t: Casts that address to achar*. Unlikeint*, achar*increments by 1 byte when you add to it (sincecharis always 1 byte in size per the C++ standard).(char *)&t + 4: Moves the pointer forward by 4 bytes. On most systems, anintis 4 bytes, so this skips over the first membert.x(which occupies the first 4 bytes oft's memory).(int *)((char *)&t + 4): Casts this new address back to anint*, so now it points directly to the memory location wheret.yis stored.*(int *)...: Dereferences thatint*to get the value stored at that address—this ist.y(15).
A quick heads-up: This code relies on two assumptions that aren't guaranteed across all compilers or systems:
intis exactly 4 bytes (it could be 8 on some rare 64-bit setups, though 4 is standard).- There's no padding between the
xandymembers ofThing. Compilers sometimes add padding to align struct members for performance, which would break this code. But for learning how pointer casting and memory addressing works, it's a great example.
For reference, here's the full code again:
struct Thing { int x , y ; int* getPlaces () { return &x ; } }; int main () { Thing t {8 ,15}; int* place = (int *)&t ; std :: cout << "\nt = " << place [0] << ", " << place [1]; int y = *( int *)(( char *)&t + 4); std :: cout << "\nt.y = " << y ; int* location = t . getPlaces (); location [0] = 17; std :: cout << "\nt.x = " << t . x ; }
内容的提问来源于stack exchange,提问作者ParksideAdrian

