简体   繁体   English

C - 使用 malloc,重新分配和释放。 我得到太多 memory 泄漏,怎么了?

[英]C - Using malloc, realloc and free. I get too many memory leaks, what's wrong?

So, my goal was to define a struct in which there is -所以,我的目标是定义一个结构,其中有 -

  1. A command name (eg - "print")命令名称(例如 - “打印”)
  2. Command arguments counter命令 arguments 计数器
  3. A strings array containing the arguments.包含 arguments 的字符串数组。

You can review my code, but I'm really having a hard time understanding what am I doing wrong -您可以查看我的代码,但我真的很难理解我做错了什么 -

  1. I use malloc to dynamically set my_struct.command size我使用 malloc 动态设置 my_struct.command 大小
  2. I use malloc to dynamically set my_struct.arguments array size我使用 malloc 动态设置 my_struct.arguments 数组大小
  3. I use realloc to dynamically increase my_struct.arguments size for every argument I set我使用 realloc 为我设置的每个参数动态增加 my_struct.arguments 大小
  4. I use malloc to dynamically set my_struct.arguments[i] size我使用 malloc 动态设置 my_struct.arguments[i] 大小
  5. I finally call cleanup(), to free any dynamically assigned pointers.我最后调用 cleanup() 来释放任何动态分配的指针。

I keep getting LOTS of memory leaks.我不断收到大量 memory 泄漏。 But I cannot understand why.但我不明白为什么。

Help and tips will be kindly appreciated.帮助和提示将不胜感激。

#include <stdio.h>
#include <stdlib.h>

struct  {
    char *command;
    int arguments_count;
    char **arguments;
} my_struct;

void cleanup(void);

int main() {
    int i;

    my_struct.command = (char *)malloc(6*sizeof(char));

    my_struct.command = "print";
    my_struct.arguments_count = 1;
    my_struct.arguments = (char **)malloc(sizeof(char *));

    my_struct.arguments[0] = "hello";

    for(i = 1 ; i < 10; i++) {
        my_struct.arguments = (char **)realloc(my_struct.arguments, sizeof(char *)*(i+1));
        my_struct.arguments[i] = (char *)malloc(8*sizeof(char));
        my_struct.arguments[i] = "hello";
        my_struct.arguments_count++;
    }

    printf("Arguments count is: %d\n", my_struct.arguments_count);
    printf("The arguments are:\n");

    for(i = 0; i < 10; i++) {
        printf("%s\n", my_struct.arguments[i]);
    }

    cleanup();

    exit(0);
}

void cleanup(void) {
    int i;

    for(i = 0; i < 10; i++)
        free(my_struct.arguments[i]);

    free(my_struct.arguments);
    free(my_struct.command);
}

strdup - The strdup() function returns a pointer to a new string which is a duplicate of the string s. strdup - strdup() function 返回一个指向新字符串的指针,该字符串是字符串 s 的副本。 Memory for the new string is obtained with malloc , and can be freed with free . Memory 的新字符串是用malloc获得的,并且可以用free释放。

my_struct.command = strdup("print");

my_struct.arguments_count = 1;
my_struct.arguments = (char**) malloc(sizeof(char*));
my_struct.arguments[0] = strdup("hello");

for (int i=1; i < 10; ++i) {
    // if the number of args is known, allocate before entering the loop
    my_struct.arguments = (char**) realloc(my_struct.arguments, sizeof(char*)*(i+1));
    my_struct.arguments[i] = strdup("hello");
    my_struct.arguments_count++;
}

// in your cleanup use the arguments_count var instead of the literal 10
for (int i=0; i < my_struct.arguments_count; ++i)

Your mistake was:你的错误是:

// allocate a memory block of 6 bytes
// assign the address of that block to command
my_struct.command = malloc(6);

// then you assigned the address of the string 'print' to command
// therefore the previous allocated block is lost -> mem leak
my_struct.command = "print";

// strdup does the following
return memcpy(malloc(strlen(str) + 1), str, strlen(str) + 1);

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

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