C 结构体成员不存储分配的值

发布于 2025-01-09 09:10:53 字数 3767 浏览 1 评论 0原文

我正在使用微处理器(PIC32MX250),并且创建了一个存储各种成员的结构:

typedef struct {
    char element1[20];
    unsigned char element2;
    unsigned char element3[2];
    unsigned char element4[2];
    unsigned char element5[2];
    unsigned char element6[2];
    unsigned char element7;
    unsigned char element8;
    unsigned char element9[2];
    unsigned char element10;
    unsigned char element11[2];
    unsigned char element12;
    char element13[7];
    unsigned char service_day;
    unsigned char service_month;
    unsigned char service_year;
    unsigned char service_hour;
    unsigned char service_minute;
    unsigned char service_status[2];
} usb_service_t;

然后我调用一个函数来读取闪存内的元素并将它们传输到该结构,如下所示:

usb_service_t g_service_container;
void read_service (unsigned int memory_address)
{
    byte loop_idx = 0;
    byte buf[64] = { 0 };

    (void) Flash_Read(memory_address, buf, 64, 0);

    g_service_container.service_day= buf[0]; // <<< PROBLEM HERE

    g_service_container.service_month= buf[1];
    g_service_container.service_year= buf[2];
    g_service_container.service_hour= buf[3];
    g_service_container.service_minute= buf[4];
    for (loop_idx = 0; loop_idx < 20; loop_idx++)
    {
        g_service_container.targa[loop_idx] = buf[5 + loop_idx];   
        if (g_service_container.element1[loop_idx] == 0x8e)
        {
            g_service_container.element1[loop_idx] = 'A';
        }
        else if (g_service_container.element1[loop_idx] == 0x99)
        {
            g_service_container.element1[loop_idx] = 'O';
        }
        else if (g_service_container.element1[loop_idx] == 0x9a)
        {
            g_service_container.element1[loop_idx] = 'U';
        }
    }
    g_service_container.element1[20] = '\0';
    g_service_container.element2= buf[25];
    for (loop_idx = 0; loop_idx < 2; loop_idx++)
    {
        g_service_container.element3[loop_idx] = buf[26 + 1 - loop_idx];
        g_service_container.element4[loop_idx] = buf[28 + 1 - loop_idx];
        g_service_container.element5[loop_idx] =
                                                    buf[30 + 1 - loop_idx];
        g_service_container.element6[loop_idx] = buf[32 + 1 - loop_idx];
        g_service_container.element9[loop_idx] = buf[36 + 1 - loop_idx];
        g_service_container.service_status[loop_idx] = buf[38 + 1 - loop_idx];
        g_service_container.element11[loop_idx] =
                                                    buf[41 + 1 - loop_idx];
    }
    g_service_container.element7= buf[34];
    g_service_container.element8= buf[35];
    g_service_container.element10= buf[40];
    g_service_container.element12= buf[43];
    for (loop_idx = 0; loop_idx < 7; loop_idx++)
    {
        g_service_container.element13[loop_idx] = buf[44 + loop_idx];
    }
    g_service_container.element13[7] = '\0';

}

当我从结构体成员的缓冲区,我可以使用 MPLAB 调试器看到,g_service_container.service_day 始终为 0,特别是 buf[0] 不为零(其他人则读取缓冲区值)。

我试图强制成员的值写入一个常量值并且它接受它。

然而以不同的方式编写结构,如下所示,它可以工作

typedef struct {
    char element1[20];
    unsigned char element2;
    unsigned char element3[2];
    unsigned char element4[2];
    unsigned char element5[2];
    unsigned char element6[2];
    unsigned char element7;
    unsigned char element8;
    unsigned char element9[2];
    unsigned char element10;
    unsigned char element11[2];
    unsigned char element12;
    char element13[7];
    unsigned char service_status[2];  // NOW I CHANGE THE ORDER OF THIS MEMBER
    unsigned char service_day;
    unsigned char service_month;
    unsigned char service_year;
    unsigned char service_hour;
    unsigned char service_minute;
} usb_service_t;

I'm working with a microprocessor (PIC32MX250) and I created a struct that stores various members:

typedef struct {
    char element1[20];
    unsigned char element2;
    unsigned char element3[2];
    unsigned char element4[2];
    unsigned char element5[2];
    unsigned char element6[2];
    unsigned char element7;
    unsigned char element8;
    unsigned char element9[2];
    unsigned char element10;
    unsigned char element11[2];
    unsigned char element12;
    char element13[7];
    unsigned char service_day;
    unsigned char service_month;
    unsigned char service_year;
    unsigned char service_hour;
    unsigned char service_minute;
    unsigned char service_status[2];
} usb_service_t;

Then I call a function that reads the elements inside a flash memory and transfers them to the struct, like this:

usb_service_t g_service_container;
void read_service (unsigned int memory_address)
{
    byte loop_idx = 0;
    byte buf[64] = { 0 };

    (void) Flash_Read(memory_address, buf, 64, 0);

    g_service_container.service_day= buf[0]; // <<< PROBLEM HERE

    g_service_container.service_month= buf[1];
    g_service_container.service_year= buf[2];
    g_service_container.service_hour= buf[3];
    g_service_container.service_minute= buf[4];
    for (loop_idx = 0; loop_idx < 20; loop_idx++)
    {
        g_service_container.targa[loop_idx] = buf[5 + loop_idx];   
        if (g_service_container.element1[loop_idx] == 0x8e)
        {
            g_service_container.element1[loop_idx] = 'A';
        }
        else if (g_service_container.element1[loop_idx] == 0x99)
        {
            g_service_container.element1[loop_idx] = 'O';
        }
        else if (g_service_container.element1[loop_idx] == 0x9a)
        {
            g_service_container.element1[loop_idx] = 'U';
        }
    }
    g_service_container.element1[20] = '\0';
    g_service_container.element2= buf[25];
    for (loop_idx = 0; loop_idx < 2; loop_idx++)
    {
        g_service_container.element3[loop_idx] = buf[26 + 1 - loop_idx];
        g_service_container.element4[loop_idx] = buf[28 + 1 - loop_idx];
        g_service_container.element5[loop_idx] =
                                                    buf[30 + 1 - loop_idx];
        g_service_container.element6[loop_idx] = buf[32 + 1 - loop_idx];
        g_service_container.element9[loop_idx] = buf[36 + 1 - loop_idx];
        g_service_container.service_status[loop_idx] = buf[38 + 1 - loop_idx];
        g_service_container.element11[loop_idx] =
                                                    buf[41 + 1 - loop_idx];
    }
    g_service_container.element7= buf[34];
    g_service_container.element8= buf[35];
    g_service_container.element10= buf[40];
    g_service_container.element12= buf[43];
    for (loop_idx = 0; loop_idx < 7; loop_idx++)
    {
        g_service_container.element13[loop_idx] = buf[44 + loop_idx];
    }
    g_service_container.element13[7] = '\0';

}

When I assign the elements from the buffer to the member of the struct, I can see with the MPLAB debugger, that g_service_container.service_day is always 0, in particular buf[0] is not zero (the others read the buffer value instead).

I tried to force the value of the member writing a constant value and it accepts it.

However writing the struct in a different way, as shown below, it works:

typedef struct {
    char element1[20];
    unsigned char element2;
    unsigned char element3[2];
    unsigned char element4[2];
    unsigned char element5[2];
    unsigned char element6[2];
    unsigned char element7;
    unsigned char element8;
    unsigned char element9[2];
    unsigned char element10;
    unsigned char element11[2];
    unsigned char element12;
    char element13[7];
    unsigned char service_status[2];  // NOW I CHANGE THE ORDER OF THIS MEMBER
    unsigned char service_day;
    unsigned char service_month;
    unsigned char service_year;
    unsigned char service_hour;
    unsigned char service_minute;
} usb_service_t;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2025-01-16 09:10:53

听起来编译器正在填充结构。请参阅这篇文章了解其工作原理:https://stackoverflow.com/a/40642888/9766462

Sounds like the compiler is padding the struct. See this post on how this works: https://stackoverflow.com/a/40642888/9766462

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文