不能使用memcpy填充结构

发布于 2025-01-18 22:22:57 字数 914 浏览 3 评论 0原文

我正在尝试编写一个程序来模仿一些装配代码(不要问我为什么大声笑),看起来应该这样。

它应该使用int值以填充由长值组成的结构的内存。

当我调试程序时,在第一次迭代sizef(int)*a = 0中,一切都很好。

但是在第二个迭代中,a = 1sizeof(int)*1 = 4,但&amp; ss+sizeof(int)*a不等于&amp; ss+4,而是&amp; ss+0xa0 ...然后为a = 2,<代码>&amp; ss+0x140 。它不断乘以40(十进制系统)。

十六进制0xa0 = 4*40十进制。十六进制0x140 = 8*40十进制...

如何使此工作?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    long a[5];
} StructType1;

StructType1 foo()
{
    StructType1 ss;
    int bb = 7;
    int a = 0;
    while (a < 10)
    {
        memcpy(&ss + sizeof(int) * a, &bb, 4);
        a++;
    }

    return ss;
}

int main()
{
    StructType1 s = foo();
    printf("%ld\n", s.a[0]);
}

I am trying to write a program to mimic some assembly code(don't ask me why lol), and it should look something like this.

It should use int value in order to populate the memory of the structure that consists of long values.

When I debug the program, in the first iteration sizeof(int)*a = 0 and everything is good.

But in the second iteration, a=1 and sizeof(int)*1=4, but &ss+sizeof(int)*a is not equal to &ss+4 but rather to &ss+0xA0... Then for a = 2, &ss+0x140. It is constantly multiplying by 40 (decimal system).

hex 0xA0 = 4*40 decimal. hex 0x140 = 8*40 decimal...

How to make this work?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    long a[5];
} StructType1;

StructType1 foo()
{
    StructType1 ss;
    int bb = 7;
    int a = 0;
    while (a < 10)
    {
        memcpy(&ss + sizeof(int) * a, &bb, 4);
        a++;
    }

    return ss;
}

int main()
{
    StructType1 s = foo();
    printf("%ld\n", s.a[0]);
}

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

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

发布评论

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

评论(1

浊酒尽余欢 2025-01-25 22:22:57

正如user3386109所评论的,&ss的类型是指向StructType1对象的指针,因此添加了一个整数n 计算此类对象数组中第 n 个下一个对象的地址,将 n 乘以该对象的大小即可获取字节地址。

出于您的目的,您应该将 &ss 转换为指向字符类型的指针。

这是修改后的版本:

#include <stdio.h>
#include <string.h>

typedef struct {
    long a[5];
} StructType1;

StructType1 foo(void) {
    StructType1 ss;
    int bb = 7;
    int a = 0;
    int n = sizeof ss / sizeof(int);
    while (a < n) {
        memcpy((char *)&ss + sizeof(int) * a, &bb, sizeof(int));
        a++;
    }
    return ss;
}

int main() {
    StructType1 s = foo();
    for (int i = 0; i < 5; i++) {
        printf("%ld%c", s.a[i], "    \n"[i]);
    }
    return 0;
}

As commented by user3386109, the type of &ss is pointer to a StructType1 object, thus adding an integer n to it computes the address of the n-th next object in an array of such objects, multiplying n by the size of the object to the get the byte address.

For you purpose, you should cast &ss as a pointer to a character type.

Here is a modified version:

#include <stdio.h>
#include <string.h>

typedef struct {
    long a[5];
} StructType1;

StructType1 foo(void) {
    StructType1 ss;
    int bb = 7;
    int a = 0;
    int n = sizeof ss / sizeof(int);
    while (a < n) {
        memcpy((char *)&ss + sizeof(int) * a, &bb, sizeof(int));
        a++;
    }
    return ss;
}

int main() {
    StructType1 s = foo();
    for (int i = 0; i < 5; i++) {
        printf("%ld%c", s.a[i], "    \n"[i]);
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文