简体   繁体   中英

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".

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. 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 :

strcpy(test, server_alg);

This is correct, as you properly allocated memory for test here:

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

Then, however, you try to free it while it is still pointing to "A" in your binary:

free(server_alg);

Instead, try freeing test , because that is pointing to the memory you allocated:

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.

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. 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.

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. 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); , just like with memory allocated by malloc .

following statements are the issues here 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); is wrong, it will lead to memory dump

and we should never try to use pointer once we do free() on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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