不含指针的结构体赋值实现,直接内存拷贝

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)

 

posted @   bonelee  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2017-12-05 hmm CDN检测
点击右上角即可分享
微信分享提示