简体   繁体   English

结构和内存分配

[英]C struct and memory allocation

When you allocate space for memory how do you tell if you need to allocate more space for it? 当您为内存分配空间时,如何知道是否需要为其分配更多空间? Is there a check or so you can do on your new memory to make sure it is doing OK? 是否有支票或您可以对新内存进行检查以确保其工作正常? ( allocated memory for a struct). (为结构分配了内存)。

Because what i am thinking is that a struct is a set amount of data and even though I pass it around a lot it should never need more than the size of the struct correct? 因为我在想的是结构是一定数量的数据,即使我大量传递数据,它也永远不需要超过正确的结构大小?

If you're just using a simple struct , you don't need more memory allocated for it as time goes on. 如果您只是使用简单的struct ,那么随着时间的流逝,您不需要为其分配更多的内存。 You just create the struct , use it, and clean it up if required. 您只需创建struct ,使用它,然后根据需要清理它。 If you are dynamically allocating your struct (ie: with malloc ), then you test the value of the pointer-to-struct you create and see if it is NULL . 如果要动态分配结构(即,使用malloc ),则需要测试所创建的结构指针的值,看看它是否为NULL If it is NULL , then the memory allocation failed, and you can either retry, or abandon further operations (ie: exit on error condition). 如果为NULL ,则内存分配失败,您可以重试或放弃进一步的操作(即:在错误情况下退出)。

#include <stdio.h>

typedef struct myStruct {
  int i;
  char c;
} myStruct;

int main(void) {
  // Static allocation, no cleanup required
  myStruct staticStruct;
  staticStruct.i = 0;
  staticStruct.c = 'c';

  // Dynamic allocation, requires cleanup
  myStruct* dynamicStruct;
  dynamicStruct = malloc(sizeof(myStruct));
  if (dynamicStruct == NULL) {
    printf("Memory allocation error!\n");
    return (-1);
  } else {
    printf("Successfully allocated memory!\n");
  }

  dynamicStruct->i = 1;
  dynamicStruct->c = 'd';
  free(dynamicStruct);  // Release allocated memory
  dynamicStruct = NULL; // Somewhat common practise, though not 100% necessary
  return 0;
}

Now, if you need to create an array of dynamically allocated structs, and you've used them all up, and need more, you'd likely be best off with a slightly more complicated approach, like a dynamically allocated linked list of structs. 现在,如果您需要创建一个动态分配的结构数组,并且用尽了它们,并且还需要更多,则最好采用稍微复杂一些的方法,例如动态分配的结构链接列表。 A good example can be found in the "References" section below. 在下面的“参考”部分中可以找到一个很好的例子。 Also, I've included a link to a somewhat related question I answered on memory allocation in C. It has some good examples that might also help clear up this topic for you. 另外,我还提供了一个链接,该链接指向我在C语言中有关内存分配的一些相关问题。它提供了一些很好的示例,这些示例也可能有助于您解决该问题。

References 参考


  1. C Linked List Data Structure Explained with an Example C Program , Accessed 2014-03-25, <http://www.thegeekstuff.com/2012/08/c-linked-list-example/> C链表数据结构用示例C程序解释 ,已访问2014-03-25, <http://www.thegeekstuff.com/2012/08/c-linked-list-example/>
  2. Difference between declared string and allocated string , Accessed 2014-03-25, <https://stackoverflow.com/questions/16021454/difference-between-declared-string-and-allocated-string> 声明的字符串与已分配的字符串之间的差异 ,已访问2014-03-25, <https://stackoverflow.com/questions/16021454/difference-between-declared-string-and-allocated-string>

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

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