C语言 为 C 中的结构分配内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2177391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:21:04  来源:igfitidea点击:

Allocating memory for a Structure in C

cmallocstructure

提问by Blackbinary

I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use

我的任务是创建一个为结构动态分配内存的程序。通常我们会使用

x=malloc(sizeof(int)*y);

However, what do I use for a structure variable? I don't think its possible to do

但是,我对结构变量使用什么?我认为不可能做到

struct st x = malloc(sizeof(struct)); 

Could someone help me out? Thanks!

有人可以帮我吗?谢谢!

回答by dirkgently

My favorite:

我最喜欢的:

#include <stdlib.h>

struct st *x = malloc(sizeof *x); 

Note that:

注意:

  • xmust be a pointer
  • no cast is required
  • include appropriate header
  • x必须是指针
  • 不需要演员表
  • 包括适当的标题

回答by David Thornley

You're not quite doing that right. struct st xis a structure, not a pointer. It's fine if you want to allocate one on the stack. For allocating on the heap, struct st * x = malloc(sizeof(struct st));.

你做得不太对。 struct st x是一个结构,而不是一个指针。如果你想在堆栈上分配一个也没关系。为了在堆上分配,struct st * x = malloc(sizeof(struct st));.

回答by Nikolai Fetissov

struct st* x = malloc( sizeof( struct st ));

struct st* x = malloc( sizeof( struct st ));

回答by Martin Beckett

It's exactly possible to do that - and is the correct way

完全有可能做到这一点 - 并且是正确的方法

Assuming you meant to type

假设你想打字

struct st *x = malloc(sizeof(struct st)); 

ps. You have to do sizeof(struct) even when you know the size of all the contents because the compiler may pad out the struct so that memebers are aligned.

附:即使您知道所有内容的大小,您也必须执行 sizeof(struct) ,因为编译器可能会填充结构以便成员对齐。

struct tm {
  int x;
  char y;
}

might have a different size to

可能有不同的大小

struct tm {
  char y;
  int x;
}

回答by codaddict

This should do:

这应该做:

struct st *x = malloc(sizeof *x); 

回答by alemjerus

struct st *x = (struct st *)malloc(sizeof(struct st));

struct st *x = (struct st *)malloc(sizeof(struct st));

回答by Ritwik Bose

I believe, when you call sizeofon a structtype, C recursively calls sizeof on the fields of the struct. So, struct st *x = malloc(sizeof(struct st));only really works if struct sthas a fixed size. This is only significant if you have something like a variable sized string in your struct and you DON'T want to give it the max length each time.

我相信,当你调用sizeof一个struct类型时,C 会递归调用struct. 因此,struct st *x = malloc(sizeof(struct st));只有在struct st具有固定大小的情况下才真正起作用。这仅在您的结构中有诸如可变大小字符串之类的东西并且您不想每次都给它最大长度时才有意义。

In general,

一般来说,

struct st *x = malloc(sizeof(struct st));

works. Occasionally, you will run into either variable sized structs or 'abstract' structs (think: abstract class from Java) and the compiler will tell you that it cannot determine the size of struct st. In these cases, Either you will have to calculate the size by hand and call malloc with a number, or you will find a function which returns a fully implemented and malloc'd version of the struct that you want.

作品。有时,您会遇到可变大小的结构或“抽象”结构(想想:Java 的抽象类),编译器会告诉您它无法确定 struct st 的大小。在这些情况下,您要么必须手动计算大小并使用数字调用 malloc,要么您会找到一个函数,该函数返回您想要的结构的完全实现和 malloc 版本。