C char数组,指针,malloc,免费

时间:2016-11-30 13:57:08

标签: c pointers char malloc free

我想了解指针,我有一个简单的例子

void third(char ****msg) {
    ***msg = malloc(5 * sizeof (char));
    printf("\nthe msg in third is :%s ", ***msg);
    strcpy(***msg, "third");
    printf("\nthe msg in third after is: %s", ***msg);
    // free(***msg);
}

void change(char***msg) {
    **msg = malloc(5 * sizeof (char));
    printf("\nthe msg in change is :%s ", **msg);
    strcpy(**msg, "change");
    printf("\nthe msg in change after is: %s", **msg);
    third(&msg);
    //   free(**msg);
}

void test(char ** msg) {
    *msg = malloc(5 * sizeof (char));
    printf("\n the msg in test is: %s", *msg);

    strcpy(*msg, "test");
    printf("\nthe msg in test after is: %s\n", *msg);

    change(&msg);
    free(*msg);
}

int main(int argc, char** argv) {

    char * msg;
    test(&msg);
    printf("\nthe msg back in main is: %s", msg);
}

我可以说它工作正常,但是你能否告诉我何时以及如何释放分配的内存,因为如果我从函数中删除//更改而第三次运行它我就会出错。有没有办法在每个函数的第一个print语句中获取消息的内容 - 请参阅otuput:

the msg in test is: 
the msg in test after is: test
the msg in change is :0�� 
the msg in change after is: change
the msg in third is :P�� 
the msg in third after is: third
the msg back in main is: 

有没有办法让msg处于变化状态:测试然后 第三个消息是:改变

2 个答案:

答案 0 :(得分:2)

忘了那个程序,它有太多错误:

  • 您不需要多级间接。两个级别就足够了。如果该函数需要更改地址,只需将指向指针的指针传递给下一个函数。
  • 您尝试在字符串初始化之前打印它。
  • 释放后尝试使用该字符串。
  • 通过反复调用malloc而不清除旧内容来创建内存泄漏。请改用realloc。
  • 您分配的内存量不正确,因此数组的大小不足以容纳您绑定的字符串。另请注意,您需要为null终止符分配足够的空间。

等等。这是一个固定版本:

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

void third(char** msg) {
    const char str[] = "third";

    *msg = realloc(*msg, sizeof(str));
    printf("the msg in third is :%s\n", *msg);

    strcpy(*msg, str);
    printf("the msg in third after is: %s\n", *msg);
}

void change(char** msg) {
    const char str[] = "change";

    *msg = realloc(*msg, sizeof(str));
    printf("the msg in change is :%s\n", *msg);

    strcpy(*msg, str);
    printf("the msg in change after is: %s\n", *msg);

    third(msg);
}

void test(char** msg) {
    const char str[] = "test";

    *msg = malloc(sizeof(str));
    printf("the msg in test is just garabage at this point, no need to print it.\n");

    strcpy(*msg, str);
    printf("the msg in test after is: %s\n", *msg);

    change(msg);
}

int main(int argc, char** argv) {

    char* msg;
    test(&msg);
    printf("the msg back in main is: %s\n", msg);

    free(msg);
}

答案 1 :(得分:1)

在现实生活中,避免使用尽可能多的“明星”(即间接数量)。此外,您的代码还有几个涉及undefined behavior (UB)

的问题
void third(char ****msg) {
    ***msg = malloc(5 * sizeof (char));
    printf("\nthe msg in third is :%s ", ***msg);// Don't do that, ***msg is not intialize => UB
    strcpy(***msg, "third"); // Don't do that => ***msg has 5 bytes, and you copy 6 char => UB
    printf("\nthe msg in third after is: %s", ***msg);
    // free(***msg);
}

void change(char***msg) {
    **msg = malloc(5 * sizeof (char));
    printf("\nthe msg in change is :%s ", **msg);// Don't do that, **msg is not intialize => UB
    strcpy(**msg, "change"); // Don't do that => **msg has 5 bytes, and you copy 7 char => UB
    printf("\nthe msg in change after is: %s", **msg);
    third(&msg);
    //   free(**msg);
}

void test(char ** msg) {
    *msg = malloc(5 * sizeof (char));
    printf("\n the msg in test is: %s", *msg); // Don't do that, *msg is not intialize => UB

    strcpy(*msg, "test");
    printf("\nthe msg in test after is: %s\n", *msg);

    change(&msg);
    free(*msg);
}

int main(int argc, char** argv) {

    char * msg;
    test(&msg);
    printf("\nthe msg back in main is: %s", msg); //UB => you use freed variable
}

在纠正了这个UB的多个来源之后,它仍然是一个糟糕的设计:使用free函数

事实上,当你在函数内部执行malloc时,你使用指针的指针,你也修改了调用者的指针。尝试运行此并查看:

void first(char **t)
{
    *t = malloc(5*sizeof(char));
}

int main(int argc, char * argv[])
{
    char * t = NULL;
    printf("%p\n", t);
    first(&t);
    printf("%p\n", t);
    return 0;
}

它产生(demo)

(nil)
 0x995f008 (or another address)

因此,当您在test中释放时,可以释放third

中分配的内存

最后,正如评论中已经提到的,只有2颗星就足够了:

void third(char **msg) {
    free(*msg); // Free before allocate and change address stored in pointer
    *msg = malloc(6 * sizeof (char));
    strcpy(*msg, "third");
    printf("\nthe msg in third after is: %s", *msg);
}

void change(char **msg) {
    free(*msg); // Free before allocate and change address stored in pointer
    *msg = malloc(7 * sizeof (char));
    strcpy(*msg, "change");
    printf("\nthe msg in change after is: %s", *msg);
    third(msg);
}

void test(char ** msg) {
    *msg = malloc(5 * sizeof (char));

    strcpy(*msg, "test");
    printf("\nthe msg in test after is: %s\n", *msg);

    change(msg);
}

int main(int argc, char** argv) {

    char * msg;
    test(&msg);
    printf("\nthe msg back in main is: %s", msg);
    free(msg); // Deallocate memory ONLY when you don't need it anymore
    msg = NULL; // Good practice, set to NULL freed pointer to inform that no more memory are allocated
}