如何将结构分配给结构的指针数组? [英] How to assign a struct to an array of pointers of the struct?

查看:56
本文介绍了如何将结构分配给结构的指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.所以我有以下结构:

Okay. So I have the following struct:

struct pkt {
    int seqnum;
    int acknum;
    int checksum;
    char payload[20];
};

现在我有以下变量:

struct pkt packet;
struct pkt* packets[1000];

现在在对 packet 进行几次操作后,我尝试将其分配给数组单元,如下所示:

Now after a few manipulations of packet I am trying to assign it to an array cell as follows:

packets[counter++] = (pkt*)&packet;

但这会以某种方式将数组的所有单元格在新赋值时更改为新赋值的值.

But that is somehow changing all the cells of the array upon a new assignment to the value of the new assignment.

我做错了什么?

我刚开始接触 C,所以我对指针和地址不是很熟悉.

I have just started off with C, so I am not so familiar with pointers and addresses.

EDIT:完成赋值的函数:

void A_output(struct msg message)
{
    struct pkt packet;
    packet.acknum = 0;
    packet.seqnum = 0;
    memset(&packet.payload, '\0', 20);
    memcpy((char*)&packet.payload, (char*)&message.data, 20);
    memset(&packets[counter]->payload, '\0', 20);
    packets[counter++] = (pkt*)&packet;
    printAll();

    if(nextseq < winbase + winsize) {
        packets[nextseq]->seqnum = nextseq;
        setChecksum(packets[nextseq]);
        tolayer3(0, *packets[nextseq]);

        if(winbase == nextseq) 
            starttimer(0, increment);
        nextseq++;
    }
}

既然这个值是作为函数的参数来的,那么不是每次都有一个新的内存地址吗?

Since the value is coming as a parameter of a function, doesn't it have a new memory address every time?

推荐答案

void A_output(struct msg message)
{
    struct pkt packet;

[...]

    packets[counter++] = (pkt*)&packet;

最后一行将具有自动存储期(局部变量)的变量的地址分配给具有静态存储期(全局)的对象.一旦您的函数退出,具有自动存储期的变量不再存在,因此指针未指向有效位置.稍后使用此指针是未定义的行为,但很可能您的函数每次被调用时都会为您的 struct pkt packet 重用相同的内存位置,从而导致您的行为描述.

This last line assigns the address of a variable with automatic storage duration (a local variable) to an object with static storage duration (a global). Once your function exits, the variable with automatic storage duration doesn't exist anymore, therefore the pointer doesn't point to a valid location. Using this pointer later is undefined behavior, but it's quite likely that your function reuses the same memory location for your struct pkt packet every time it is invoked, leading to the behavior you describe.

例如,您需要做的是使用 malloc()newpackets 中的每个元素分配内存:

What you have to do is for example allocate memory for every element in packets with malloc() or new:

struct pkt *packet = malloc(sizeof *packet); // C style
// or:
pkt *packet = new pkt(); // C++ style

[...] // fill *packet

packets[counter++] = packet;

完成后不要忘记 free() (C) 或 delete (C++) 所有数据包.

Don't forget to free() (C) or delete (C++) all the packets when you're done.

附带说明,在您的原始代码中,演员 (pkt*) 是多余的.&packet 已经具有 struct pkt * 类型.但无论如何这都是错误的,所以在这里无关紧要.

On a side note, in your original code, the cast (pkt*) is redundant. &packet already has the type struct pkt *. But as this is wrong anyways, it doesn't matter much here.

这篇关于如何将结构分配给结构的指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆