what's the correct way to malloc struct pointe

2020-05-03 11:40发布

Imagine I've the following struct

struct Memory {
    int type;
    int prot;
};

typedef struct Memory *Memory;

How would I initialise it using malloc()?

Memory mem = malloc(sizeof(Memory));

or

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

What is the correct way to allocate that?

标签: c
3条回答
混吃等死
2楼-- · 2020-05-03 12:19

This construction

struct {
    int type;
    int prot;
} Memory;

defines an object with name Memory that has type of unnamed structure.

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. and 2) another new type name Memory that is pointer to 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.

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

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.

查看更多
乱世女痞
3楼-- · 2020-05-03 12:27

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.

查看更多
Ridiculous、
4楼-- · 2020-05-03 12:46

Your struct declaration is a bit muddled up, and the typedef is wrong on many levels. 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". 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.

Using sizeof *<the-pointer> is generally considered to be the better way of allocating memory, but if you want, you can write:

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. In that case, you should discourage the use of struct _memory as much as possible, in favour of Memory or *<the-pointer> anyway

If you want to typedef a pointer, then you can write this:

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 typedefs obscure certain things. 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. 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. That's just my opinion, I'm sure there are many programmers that are far more skilled than me who do use pointer typedefs.
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);, but instead you can write 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

查看更多
登录 后发表回答