简体   繁体   English

使用sizeof分配结构指针的正确方法是什么?

[英]what's the correct way to malloc struct pointer using sizeof?

Imagine I've the following struct 想象一下,我有以下结构

struct Memory {
    int type;
    int prot;
};

typedef struct Memory *Memory;

How would I initialise it using malloc()? 如何使用malloc()初始化它?

Memory mem = malloc(sizeof(Memory));

or 要么

Memory mem = malloc(sizeof(struct Memory));

What is the correct way to allocate that? 分配它的正确方法是什么?

Your struct declaration is a bit muddled up, and the typedef is wrong on many levels. 您的struct声明有些混乱,并且typedef在许多级别上都是错误的。 Here's what I'd suggest: 这是我的建议:

//typedef + decl in one
typedef struct _memory {
    int type;
    int prot;
} Memory;

Then allocate like so: 然后像这样分配:

Memory *mem = malloc(sizeof *mem);

Read the malloc call like so: "Allocate the amount of memory required to store whatever type mem is pointing to" . 像这样读取malloc调用: “分配存储mem指向的任何类型所需的内存量” If you change Memory *mem to Memory **mem , it'll allocate 4 or 8 bytes (depending on the platform), as it now stands it'll probably allocate 8 bytes, depending on the size of int and how the compiler pads the struct check wiki for more details and examples . 如果将Memory *mem更改为Memory **mem ,它将分配4或8个字节(取决于平台),按目前的情况,它可能会分配8个字节,具体取决于int的大小以及编译器的填充方式有关详细信息和示例,请参见 struct check Wiki

Using sizeof *<the-pointer> is generally considered to be the better way of allocating memory, but if you want, you can write: 通常认为使用sizeof *<the-pointer>是分配内存的更好方法,但是如果需要,可以编写:

Memory *mem = malloc(sizeof(Memory));
Memory *mem = malloc(sizeof(struct _memory));

They all do the same thing. 他们都做同样的事情。 Mind you, if you typedef a struct, that's probably because you want to abstract the inner workings of something, and want to write an API of sorts. 请注意,如果您typedef一个结构的定义,那可能是因为您想要抽象某些东西的内部工作原理,并想要编写各种API。 In that case, you should discourage the use of struct _memory as much as possible, in favour of Memory or *<the-pointer> anyway 在这种情况下,您应该尽可能不鼓励使用struct _memory ,而应该使用Memory*<the-pointer>

If you want to typedef a pointer, then you can write this: 如果要typedef指针,则可以编写以下代码:

typedef struct _memory {
    int type;
    int prot;
} *Memory_p;

In which case this: 在这种情况下:

Memory_p mem = malloc(sizeof *mem);

might seem counter intuitive, but is correct, as is: 可能看起来与直觉相反,但是正确的,如下所示:

Memory_p mem = malloc(sizeof(struct _memory));

But this: 但是这个:

Memory_p mem = malloc(sizeof(Memory_p));

is wrong (it won't allocate the memory required for the struct, but memory to store a pointer to it). 是错误的(它不会分配该结构所需的内存,而是存储指向它的指针的内存)。

It's a matter of personal preference, perhaps, but I personally find typedef s obscure certain things. 也许这是个人喜好的问题,但是我个人认为typedef掩盖了某些东西。 In many cases this is for the better (ie FILE* ), but once an API starts hiding the fact you're working with pointers, I start to worry a bit. 在许多情况下,这样做是更好的选择(例如FILE* ),但是一旦API开始隐藏您正在使用指针的事实,我就开始担心。 It tends to make code harder to read, debug and document... 它倾向于使代码更难阅读,调试和记录...

Just think about it like this: 只是这样想:

int *pointer, stack;

The * operator modifies a variable of a given type, a pointer typedef does both. *运算符会修改给定类型的变量,指针typedef会同时执行这两种操作。 That's just my opinion, I'm sure there are many programmers that are far more skilled than me who do use pointer typedefs. 那只是我的看法,我敢肯定,有很多使用指针typedef的程序员比我熟练得多。
Most of the time, though, a pointer typedef is accompanied by custom allocator functions or macro's, so you don't have to write odd-looking statements like Memory_p mem = malloc(sizeof *mem); 但是,在大多数情况下,指针typedef会附带自定义分配器函数或宏,因此您不必编写看起来很奇怪的语句,例如Memory_p mem = malloc(sizeof *mem); , but instead you can write ALLOC_MEM_P(mem, 1); ,但是您可以编写ALLOC_MEM_P(mem, 1); which could be defined as: 可以定义为:

#define ALLOC_MEM_P(var_name, count) Memory_p var_name = malloc(count * sizeof *var_name)

or something 或者其他的东西

Both

 typedef struct Memory * Memory;

and

 Memory mem = malloc (sizeof (Memory));

are wrong. 错了。 The correct way to do it is : 正确的方法是:

typedef struct memory
{
     int type;
     int prot;
} *MEMPTR;

or 要么

struct memory
{
     int type;
     int prot;
};

typedef struct memory *MEMPTR;

The name of the structure should be different than the name of a pointer to it. 结构的名称应与指向它的指针的名称不同。

This construction 这个建筑

struct {
    int type;
    int prot;
} Memory;

defines an object with name Memory that has type of unnamed structure. 定义一个名称为Memory的对象,该对象具有未命名结构的类型。

Thus the next construction 因此下一个建筑

typedef struct Memory *Memory;

defined 1) a new type struct Memory that has nothing common with the definition above and the name Memory. 定义1)一种新的类型struct Memory ,与上面的定义和名称Memory没有共同之处。 and 2) another new type name Memory that is pointer to struct Memory . 和2)另一个新类型名称Memory ,它是指向struct Memory指针。

If the both constructions are present in the same compilation unit then the compiler will issue an error because name Memory (the name of the pointer) in the typedef declaration tries to redeclare the object of the type of the unnamed structure with the same name Memory . 如果这两种构造都存在于同一编译单元中,则编译器将发出错误,因为typedef声明中的名称Memory (指针的名称)试图重新声明具有相同名称Memory的未命名结构类型的对象。

I think you mean the following 我想你是说以下

typedef struct Memory {
    int type;
    int prot;
} Memory;

In this case you may use the both records of using malloc like 在这种情况下,您可以像使用malloc一样使用两条记录

Memory *mem = malloc( sizeof( Memory ) );

and

struct Memory *mem = malloc( sizeof( struct Memory ) );

or 要么

Memory *mem = malloc( sizeof( struct Memory ) );

or 要么

struct Memory *mem = malloc( sizeof( Memory ) );

because now the two identifiers Memory are in two different name spaces, The first one is used with tag struct and the second is used without tag struct. 因为现在两个标识符Memory位于两个不同的名称空间中,所以第一个用于标记struct ,第二个不使用标记struct。

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

相关问题 使用 malloc 为结构指针分配内存和将结构指针指向结构的内存地址有什么区别? - What is the difference between using malloc to allocate memory to a struct pointer and pointing a struct pointer to a struct's memory address? 调整结构大小的最合适方法是什么 - What's the most appropriate way to sizeof a struct 使用指针和 offsetof() 访问结构的正确方法是什么 - What is correct way to access a struct using pointer and offsetof() “malloc(sizeof(struct a *))”和“malloc(sizeof(struct a))”是否相同? - Are “malloc(sizeof(struct a *))” and “malloc(sizeof(struct a))” the same? 将 sizeof() 与指向 C 中的结构的指针一起使用 - Using sizeof() with pointer to struct in C 初始化指向struct的指针数组的正确方法是什么? - What is the correct way to initialize an array of pointer to struct? 填充结构的正确(现代)方法是什么? - What's the correct (modern) way to pad a struct? malloc(sizeof(结构元素)*大小); 或malloc(sizeof(struct element)* 1); - malloc(sizeof(struct element) * size); or malloc(sizeof(struct element) * 1); C中的typedef结构的malloc sizeof - malloc sizeof a typedef struct in C Malloc sizeof 结构的单个成员? - Malloc sizeof individual members of a struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM