Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 这个代码是安全的内存泄漏?_C_Memory Leaks_Malloc - Fatal编程技术网

C 这个代码是安全的内存泄漏?

C 这个代码是安全的内存泄漏?,c,memory-leaks,malloc,C,Memory Leaks,Malloc,我做了一个函数,这做了一个操作,在写了变量之后,我的问题是: 这个密码安全吗 有一个例子: #include <stdlib.h> void test(char **out){ char *f = (char *) malloc(10); f = "123456"; *out = f; return; } int main(void){ char *A = NULL; test(&A); free(A); r

我做了一个函数,这做了一个操作,在写了变量之后,我的问题是: 这个密码安全吗

有一个例子:

#include <stdlib.h>

void test(char **out){
    char *f = (char *) malloc(10);
    f = "123456";
    *out = f;
    return;
}

int main(void){
    char *A = NULL;
    test(&A);
    free(A);
    return;
}
#包括
无效测试(字符**输出){
char*f=(char*)malloc(10);
f=“123456”;
*out=f;
返回;
}
内部主(空){
char*A=NULL;
测试(A);
免费(A);
返回;
}
它会泄漏


使用strcpy压顶字符串

仔细查看
test()
定义:

void test(char **out){
    char *f = (char *) malloc(10);
    f = "123456";
    *out = f;
    return;
}
特别是这两行:

char *f = (char *) malloc(10);
f = "123456";
这段代码所做的只是将malloc-ed指针替换为字符串文本的指针,因为您的程序中存在内存泄漏(即,您丢失了从
malloc()
call获得的原始指针),并且调用
free()
(在
main()
中)在这种情况下,实际上是未定义的行为。

问题在于:

void test(char **out){
    char *f = (char *) malloc(10);
    f = "123456";
    *out = f;
    return;
}
malloc行在堆上分配10字节的内存。所以在这个阶段,f的地址将在malloc抓取内存块的地方。为了便于论证,我们将说这是0x10000

然后为f指定一个文本字符串的地址

下面的代码打印出正在发生的事情

#include <stdlib.h>

void test(char **out){
   char *f = (char *) malloc(10);
   printf("f after malloc = %p\n", f);
   f = "123456";
   printf("f after re-assignment = %p\n", f);
   *out = f;
   return;
}

int main(void){
   char *A = NULL;
   test(&A);
   free(A);
   return;
}
#包括
无效测试(字符**输出){
char*f=(char*)malloc(10);
printf(“malloc=%p\n”,f之后的f);
f=“123456”;
printf(“重新分配后的f=%p\n”,f);
*out=f;
返回;
}
内部主(空){
char*A=NULL;
测试(A);
免费(A);
返回;
}
这里有一些在C中处理字符串的替代方法

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

static  char* b = "test 2"; 

void test2(char **out){
   *out = b;
}

const char* test3(){
   return "test 3";
}

void test4(char **out){
   *out = (char *) malloc(10);
   strcpy(*out, "test 4");
}

int main(void){
   char *A = NULL;
   char *B = NULL;
   char *C = NULL;

   /* Assign A to the address of a 'global' string */
   test2(&A);
   printf("A is now: %s\n", A);
   /* Don't free this */

   /* return a static string */
   B = test3();
   printf("B is now: %s\n", B);

   /* allocate memory on heap and make a copy of data from a source to that memory location */
   test4(&C);
   printf("C is now: %s\n", C);
   free(C);  /* only C is allocated on heap so free this one */
}
#包括
#包括
#包括
静态字符*b=“测试2”;
无效测试2(字符**输出){
*out=b;
}
常量字符*test3(){
返回“测试3”;
}
无效测试4(字符**输出){
*out=(char*)malloc(10);
strcpy(*输出,“测试4”);
}
内部主(空){
char*A=NULL;
char*B=NULL;
char*C=NULL;
/*将分配给“全局”字符串的地址*/
测试2(&A);
printf(“A现在是:%s\n”,A);
/*不要释放这个*/
/*返回一个静态字符串*/
B=test3();
printf(“B现在是:%s\n”,B);
/*在堆上分配内存,并将数据从源复制到该内存位置*/
测试4&C;
printf(“C现在是:%s\n”,C);
free(C);/*堆上只分配了C,所以释放这个*/
}

f=“123456”是内存泄漏。更改为strcpy(f,“123456”)你认为这行代码是什么正在做什么?设置该strncpy以防止缓冲区溢出