简体   繁体   English

struct c 动态分配 memory

[英]struct c dynamically allocate memory

I am using a struct and I want to initialize a maximum of 10 ports.我正在使用一个结构,我想初始化最多 10 个端口。 However, when the program is running it could be a lot less, we don't know until run-time.但是,当程序运行时,它可能会少很多,直到运行时我们才知道。 However, this will be the max.但是,这将是最大值。 I have never done struct like this before, as I normally dynamically allocate using calloc and delcare like this *ports as the value type.我以前从未做过这样的 struct ,因为我通常使用 calloc 和 delcare 动态分配 *ports 作为值类型。

However, I can't understand this但是,我无法理解这一点

*ports[MAX_PORTS]. Am I creating 10 pointers that point to port objects?

And

*ports = (struct port_t*) calloc(2, sizeof(*ports)); 

Looks like I am allocating a single pointer that points to 2 port objects allocated on the free store?看起来我正在分配一个指向自由存储上分配的 2 个端口对象的单个指针?

I can't understand why I am using a dot operator with a arrow operator?我不明白为什么我将点运算符与箭头运算符一起使用? ports[0]->port_id = 20;端口[0]->port_id = 20; printf("port_id: %d\n", ports[0]->port_id); printf("port_id: %d\n", 端口[0]->port_id);

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

#define MAX_PORTS 10

struct port_t
{
    int port_id;
    char name;
} *ports[MAX_PORTS];

int main(void)
{
    *ports = (struct port_t*) calloc(2, sizeof(*ports));

    ports[0]->port_id = 20;

    printf("port_id: %d\n", ports[0]->port_id);

    return 0;
}

normally, what I have done in the passed is this:通常,我在传递中所做的是:

struct port_t
{
    int port_id;
    char name;
} *ports;

ports = (struct port_t*) calloc(2, sizeof(*ports));

And then assign with either of the following below.然后使用以下任一项进行分配。 However, the previous programmer has declared everything like I have displayed at the top so I can't change anything.但是,之前的程序员已经声明了我在顶部显示的所有内容,因此我无法更改任何内容。

ports[0].port_id = 10;
ports->port_id = 10;

Many thanks for any suggestions,非常感谢您的任何建议,

Your first code block has你的第一个代码块有

struct port_t
{
    int port_id;
    char name;
} *ports[MAX_PORTS];

which is an array of pointers.这是一个指针数组。 This means later when you use这意味着稍后当您使用

ports[0]->port_id

you are dereferencing the first pointer in the array.您正在取消引用数组中的第一个指针。 There is also some ugliness surrounding the size of what you are actually calloc'ing.您实际调用的内容的大小也有一些丑陋。 You're actually replacing your array of 10 with an array of 2. What you've got there is generally ugly and error prone.您实际上是用 2 的数组替换了 10 的数组。您所拥有的通常很难看且容易出错。

I believe your intentions are more along the lines of:我相信你的意图更接近于:

struct port_t
{
    int port_id;
    char name;
} *ports;

int main(void)
{
    *ports = (struct port_t*) calloc(2, sizeof(*ports));

    ports[0].port_id = 20;

    printf("port_id: %d\n", ports[0].port_id);

    return 0;
}

Since you are using C99, you could avoid calloc()/malloc(), if you really want to by using C99's variable array declaration.由于您使用的是 C99,因此如果您真的想使用 C99 的变量数组声明,则可以避免使用 calloc()/malloc()。

port_t array_on_mains_stack[some_runtime_port_count];
ports = array_on_mains_stack;

The trick there is that since it is on the stack, it is only valid from that function and any function called by it.诀窍在于,由于它在堆栈上,因此它仅对 function 和它调用的任何 function 有效。 Once you return from that function, it is of course freed.一旦你从那个 function 返回,它当然会被释放。

*ports[MAX_PORTS]. Am I creating 10 pointers that point to port objects?

Yes, you're making an array of ten pointers是的,你正在制作一个由十个指针组成的数组

*ports = (struct port_t*) calloc(2, sizeof(*ports));

...but this line is nonsense. ...但是这条线是胡说八道。 It's the same as this:与此相同:

ports[0] = (struct port_t*) calloc(2, sizeof(port_t));

ie. IE。 You're setting the first pointer to point to enough memory for two ports.您将第一个指针设置为指向足够的 memory 用于两个端口。

For such a small thing it would make much more sense to make ten ports but not use them all:对于这么小的事情,制作十个端口但不要全部使用它们会更有意义:

#define MAX_PORTS 10

struct port_t
{
    int port_id;
    char name;
} ports[MAX_PORTS];

/* number of ports in use */
int numPorts = 0;

int main(void)
{
    numPorts = 3;
    for (int i=0; i<numPorts; i++) {
     ports[i].port_id = i;
     printf("port_id %d: %d\n", i, ports[i].port_id);
    }
    return 0;
}

ports is and array of pointers to port_t objects, so by doing ports[0] you get a pointer, not an object, and you need to access it with -> ports 是指向 port_t 对象的指针数组,因此通过执行 ports[0] 你会得到一个指针,而不是 object,你需要使用 -> 访问它

Grab a good resource on pointers in C and read it from cover to cover.在 C 中获取有关指针的良好资源并从头到尾阅读。 There are also tutorials on reading C declarations.还有阅读 C 声明的教程。 You won't be able to understand this topic by getting answers to random questions.通过获得随机问题的答案,您将无法理解该主题。

Here 'sa link worth reading.是一个值得阅读的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM