简体   繁体   English

在c中释放指针后如何保存指针地址

[英]how save pointer address after free the pointer in c

I have written a code which at first creates a memory alloc and save a string in an other pointer. 我编写了一个代码,该代码首先创建一个内存分配并将一个字符串保存在另一个指针中。 According to the code, the value must be kept in an other address after free but it gives an error "munmap_chunk(): invalid pointer". 根据代码,该值必须在释放后保留在另一个地址中,但是会出现错误“ munmap_chunk():无效指针”。

My Code is : 我的代码是:

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

int main()
{
static char *server_alg;
char *test;
char *test = (char*) malloc(30*sizeof(char));
server_alg = "A";
strcpy(test, server_alg);
printf("server_alg addr = %u \n", &*server_alg);
printf("server_alg value = %u \n", server_alg);
printf("SERVER_ALGO addr = %d \n", *server_alg);
free(server_alg);
server_alg=NULL;
printf("           nulled          \n");
printf("server_alg addr = %u \n", &*server_alg);
printf("server_alg value = %u \n", server_alg);
printf("SERVER_ALGO addr = %u \n", test);
printf("SERVER_ALGO value = %u \n", *test);
return 0;
}

Is it wrong? 这是错的吗?

Thx for your helps 感谢您的帮助

You are freeing server_alg , but you didn't allocate any memory there. 您正在释放server_alg ,但未在其中分配任何内存。 Instead, you assigned a string literal to it, so it's pointing to a read-only location in your program's binary: 相反,您为其分配了字符串文字,因此它指向程序二进制文件中的只读位置:

server_alg = "A";

After this, you copy from that pointer to test : 之后,从该指针复制以test

strcpy(test, server_alg);

This is correct, as you properly allocated memory for test here: 这是正确的,因为您在此处正确分配了用于test内存:

char *test = (char*) malloc(30*sizeof(char));

Then, however, you try to free it while it is still pointing to "A" in your binary: 但是,然后尝试释放它,但仍指向二进制文件中的"A"

free(server_alg);

Instead, try freeing test , because that is pointing to the memory you allocated: 相反,请尝试释放test ,因为它指向您分配的内存:

free(test);
test=NULL;

Furthermore, there's an issue with redeclaration here: 此外,这里还有一个重新声明的问题:

char *test;
char *test = (char*) malloc(30*sizeof(char));

You're defining test twice, best just remove that first line. 您要定义两次test ,最好只删除第一行。

Last but not least, I'd change the prints in the end to: 最后但并非最不重要的一点是,我最终将打印内容更改为:

printf("server_alg addr = %p \n", server_alg);    // 00D87B30 (or something similar)
printf("server_alg value = %s \n", server_alg);   // A
printf("SERVER_ALGO addr = %p \n", test);         // 00000000
//printf("SERVER_ALGO value = %u \n", *test);

%s is the specifier that lets you print a string, and %p is the one for pointers. %s是使您可以打印字符串的说明符, %p是用于指针的说明符。 I commented out that last print because it would crash the program as test is freed and set to a null pointer now, so we can't access its content. 我注释了最后一个打印内容,因为它会在释放test并将它设置为空指针时使程序崩溃,因此我们无法访问其内容。

On another note, when you want to copy a string to the heap (be it from a string literal or from a different place on the stack heap), strdup can be used to do that. 另一个要注意的是,当您要将字符串复制到堆(从字符串文字或从堆栈堆的其他位置复制)时,可以使用strdup做到这一点。 It allocates the appropriate amount of memory so you don't have to worry about that. 它分配了适当的内存量,因此您不必担心。 Call it like this: 这样称呼它:

char *test = strdup("A");

When you're done with it, you free it by calling free(test); 完成后,您可以通过调用free(test);释放它free(test); , just like with memory allocated by malloc . ,就像malloc分配的malloc

following statements are the issues here free(server_alg); 以下语句是这里的问题free(server_alg);

you can use free() only when if you allocate memory using one of malloc(), calloc() or realloc() as you have not allocated memory free(server_alg); 仅当使用malloc(),calloc()或realloc()中的一种来分配内存时,才可以使用free(),因为您还没有分配内存free(server_alg); is wrong, it will lead to memory dump 是错误的,会导致内存转储

and we should never try to use pointer once we do free() on it. 一旦对它执行free(),就永远不要尝试使用指针。

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

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