简体   繁体   English

如何在 C 中定义结构并为其正确分配 memory?

[英]How do I define a struct and properly allocate memory for it in C?

I've recently started learning C and currently I'm working on a project which involves implementing a struct with two variables and I don't really know how to apparoach this.我最近开始学习 C,目前我正在研究一个项目,该项目涉及实现一个带有两个变量的结构,但我真的不知道如何解决这个问题。

The gist of it is I need to implement a struct which contains two variables, a pointer to an int array AND an int value which indicates the number of elements conatained within the array.它的要点是我需要实现一个包含两个变量的结构,一个指向 int 数组的指针和一个指示数组中包含的元素数量的 int 值。 The size of the array is declared upon the invocation of the constructor and is dependent on the input.数组的大小是在调用构造函数时声明的,并且取决于输入。

For the constructor I'm using a different function which recieves a string as input which is encoded into a decimal code.对于构造函数,我使用不同的 function 接收字符串作为输入,该字符串被编码为十进制代码。 Also this function recieves another input which is a pointer to an int array (the pointer defined in the struct) and the problem is I'm using the malloc() function to allocate memory for it but I dont really understand how and when to use the free() function properly. Also this function recieves another input which is a pointer to an int array (the pointer defined in the struct) and the problem is I'm using the malloc() function to allocate memory for it but I dont really understand how and when to use free() function 正确。

So, the questions are:所以,问题是:

  1. When am I supposed to free the allocated memory?我应该什么时候释放分配的 memory? (assuming I need this struct for later use throughout the program's running time) (假设我需要这个结构供以后在整个程序运行期间使用)
  2. What are the best ways to avoid memory leaks?避免 memory 泄漏的最佳方法是什么? What should you look out for?你应该注意什么?

It's unclear whether you're expected to manage the memory of the array inside, but this is functionally the setup you need for allocating the containing structure.目前尚不清楚您是否需要管理内部阵列的 memory,但这在功能上是分配包含结构所需的设置。

#include <malloc.h>
#include <stdio.h>

struct my_struct {
   size_t num_entries;
   int *array;
};

int main() {
    struct my_struct *storage = malloc(sizeof(struct my_struct));
    storage->num_entries = 4;

    storage->array = malloc(sizeof(int) * storage->num_entries);
    storage->array[0] = 1;
    storage->array[3] = 2;

    printf("allocated %ld entries\n", storage->num_entries);
    printf("entry #4 (index=3): %d\n", storage->array[3]);

    free(storage->array); /* MUST be first! */
    free(storage);
    storage = 0; /* safety to ensure you can't read the freed memory later */
}

if you're responsible for freeing the internal storage array, then you must free it first before freeing the containing memory.如果您负责释放内部存储阵列,则必须先释放它,然后再释放包含的 memory。

The biggest key to memory management: only one part of the code at any time "owns" the memory in the pointer and is responsible for freeing it or passing it to something else that will. memory 管理的最大关键:任何时候只有一部分代码“拥有”指针中的 memory 并负责释放它或将其传递给其他将要的东西。

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

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