分配和重新分配结构及其元素

Allocating and reallocating a struct and its elements

我有几个关于为结构及其成员分配内存的问题。

假设我有这样的结构:

struct _MyStruct
{
    char *a;
}
typdef struct _MyStruct MyStruct;
  1. 我希望'a'是一个动态字符串,我想给它分配内存。在这种情况下,我是否也应该为 MyStruct 分配内存?示例:

    MyStruct *myStr = malloc(sizeof(MyStruct)); //necessary?
    MyStruct *myStrCopy = myStr;
    myStrCopy->a=malloc(sizeof(char));
    //checking for null//
    
  2. 现在假设我为结构 (myStr) 分配了 X 内存量。现在,当我给'a'分配内存时,分配的内存是在分配给myStr的内存中,还是它得到了一个新的内存块?

  3. 我可以分配给 'a' 的内存多于分配给 myStr 的内存吗?

  4. 假设我想用realloc()放大'a'。我应该先放大myStr吗?如果我这样做,那么需要多少内存 (sizeof(myStr)*size_of_my_string)?

  1. 第一种情况

    MyStruct *myStr = malloc(sizeof(MyStruct)); //necessary?
    

    是的,非常喜欢。如果不首先分配给 myStr,尝试取消引用 myStr(以达到 a)是 undefined behaviour。此外,FWIW,

    myStrCopy->a=malloc(sizeof(char));
    

    只为一个 char 分配内存,确保那是您真正想要的。如果你真的想要那个,在那种情况下,你可以将其重写为

    myStrCopy->a=malloc(1);
    

    因为sizeof(char)C中保证是1

  2. 通过malloc()和家人的每一次分配都会给你一个新的记忆,如果成功的话。

  3. 你可以,而且你应该。实际上没有关系。它们是独立的变量,需要单独的内存分配。

  4. 不,只有 a 上的 realloc() 就足够了。

澄清一下: myStra 只是指向某个内存位置的指针。这并不意味着它们共享相同的内存位置。

因此分配 myStra 并不会增加它自身的变量。 它在进程的虚拟内存space某处请求新内存,并将地址存储在相应的指针中..

所以回答你的问题:是的,你可以为 a 分配一个大于 myStr.

的内存

更新以获得更好的说明:

 0x000 | 0x004 | 0x008 | 0x0012 | 0x0016
   ^                                 ^
   |                                 |
   myStr                             a

所以 myStr 可能位于内存中与 a 完全不同的地方。

不需要为 structure.Just 分配 char* 就足够了。说

MyStruct str;

str.a = malloc( sizeof(char)*10);

str 将在堆栈中。 'a' 指向的内存将在堆中。所以当 str 超出范围时,该对象将被破坏。但不是 'a' 指向的动态分配的。我们已经手动删除了它。 'a'.

指向的动态内存大小,结构体大小不变

只需重新分配'a'指向的内存即可。

str.a = realloc(a, sizeof(char)*20);
  1. I want 'a' to be a dynamic string and I want to allocate memory to it. Should I allocate memory to MyStruct too in this case?

好吧,您的 MyStruct 始终需要存在,但是有多种方法可以做到这一点,您应该选择适合您的用例的最简单的方法。

基本方法:

MyStruct myStr;
myStr.a = malloc(N); // "N chars please!"

// You can still get a pointer to this object:
foo(&myStr);

// Don't forget to free the `char` buffer later
free(myStr.a);

动态分配 — 有效,但不是必需的:

MyStruct* myStr = malloc(sizeof(MyStruct));
myStr->a = malloc(N); // "N chars please!"

// It's already a pointer, so:
foo(myStr);

// Don't forget to free the `char` buffer later
free(myStr->a);

// And then the struct
free(myStr);
  1. Now suppose I allocated an X amount of memory to the struct (myStr). Now, when I allocate memory to 'a', is the memory allocated within the memory allocated to myStr, or does it get a new block of memory?

这是一个新街区。

每个动态分配的内存块都是完全独立的。当您将成员变量 a 设为指针时,您确保尽管 a 存在于结构中,但它指向的东西却不存在(除非您使其指向自身,大声笑)。

myStr (or *myStr):                       your malloc'd memory:

0          32                       0   8   16   24   32   40  ...
+----------+                        +------------------------------+
| char* a——|———————————————————————→| text or whatever here        |
+----------+                        +------------------------------+
 (somewhere in memory)                 (somewhere else in memory)

无论您以何种方式构建,上图均有效myStr

  1. Can I allocate more memory to 'a' than I allocated to myStr?

是啊,随便你。它是分开的。你有间接.

  1. Suppose I want to enlarge 'a' with realloc(). Should I enlarge myStr first? If I do, then by what amount of memory (sizeof(myStr)*size_of_my_string)?

没有