简体   繁体   English

为结构指针分配内存

[英]allocating memory for structure pointers

Why is it that when we declare a structure pointer we need to allocate memory为什么声明结构体指针时需要分配内存

struct A{
///
};
int main(void)
{
    struct A *var=(struct A*)malloc(sizeof(struct A));
//
//
}

but when we declare a structure variable we do not need to allocate any memory?但是当我们声明一个结构变量时,我们不需要分配任何内存?

struct A var;

This is true for any pointers, not just pointers to structures.这适用于任何指针,而不仅仅是指向结构的指针。 The reason being, when you declare a variable (of type int, char or the type of some struct A), you tell the compiler to create a new variable/instance.原因是,当您声明一个变量(类型为 int、char 或某些 struct A 的类型)时,您会告诉编译器创建一个新的变量/实例。 So, the compiler automatically allocates memory for that variable.因此,编译器会自动为该变量分配内存。 But when you are declaring a pointer to some int or some struct A, you are essentially telling the compiler that you need a reference to some variable, not an a new variable of that type entirely.但是,当您声明指向某个 int 或某个 struct A 的指针时,您实际上是在告诉编译器您需要对某个变量的引用,而不是完全是该类型的新变量。 To illustrate this:为了说明这一点:

struct A{};
int a,b; // New variable a and b
struct A c,d; // New variables c,d of type struct A

// Now take a look at this:

int *px;
px = &a; // px referencing to a, no new int variable created;
px = &b; // px referencing to b, no new int variable created;

struct A* py;
py = &c; // py referencing to c, no new struct A variable created;
py = &d; // py referencing to d, no new struct A variable created;

Now, if you just declare a pointer A* p, here p is not referencing to anything.现在,如果你只是声明一个指针 A* p,这里 p 没有引用任何东西。 So, if you want p to refer to a new instance of struct A, you have to write explicitly:因此,如果您希望 p 引用 struct A 的新实例,则必须明确地编写:

c
p = (struct A*)malloc(sizeof(struct A));
  • struct A var declares a variable var on stack area of main memory with internal structure as declared in the struct A . struct A var在主内存的堆栈区域上声明了一个变量var ,其内部结构与struct A声明struct A

  • struct A * var also declares a variable var on stack area of main memory but the var is a pointer now, and as you may know, a pointer in C is used to store address of a variable, so the var need to know what is the address, the statment (struct A*)malloc(sizeof(struct A)); struct A * var也在主存的栈区声明了一个变量var但是现在这个var是一个指针,你可能知道,C 中的一个指针是用来存储变量的地址的,所以var需要知道是什么地址,语句(struct A*)malloc(sizeof(struct A)); gives you the address.给你地址。

You don't necessarily have to allocate memory when declaring a pointer (it can point to any existing object of the same type), but in order to use a pointer without causing undefined behavior, it has to point to an object of that type.不必将指针声明时分配内存(它可以指向同一类型的任何现有对象),但为了使用一个指针,而不会导致未定义的行为,它必须指向该类型的对象。 Since pointers are used extensively in data structures, it is more often than not that allocated objects are the ones most commonly pointed to, which is why it seems like you have to allocate memory.由于指针在数据结构中被广泛使用,通常分配的对象是最常指向的对象,这就是为什么您似乎必须分配内存。

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

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