不含指针的结构体赋值实现,直接内存拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h> #include <stdlib.h> struct A { int c; char b[20]; char strs[10][10]; int k; }; int main() { // A a = {...}; // 帮我初始化 struct A a = { 1, // Initialize c with some integer "Example String" , // Initialize b with a string (fewer than 20 characters) { // Initialize strs with strings (each fewer than 10 characters) "String1" , // First string "String2" , // Second string "String3" , // Third string // You can initialize more or leave them empty which will default to zeros }, 42 // Initialize k with some integer }; struct A b; b = a; printf ( "b.strs[0]=%s\n" , b.strs[0]); } |
看下 b = a 的反汇编结果:
1 2 3 4 5 6 7 | struct A b; b = a; 009C18E0 mov ecx,20h 009C18E5 lea esi,[a] 009C18EB lea edi,[b] 009C18F1 rep movs dword ptr es:[edi],dword ptr [esi] printf ( "b.strs[0]=%s\n" , b.strs[0]); |
sizeof(A) = 128 == 20h*4(dword)
标签:
c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-12-05 hmm CDN检测