Constant reassignment of character strings leads to memory leak?

I have two questions:

Q1. Character pointers are used to indicate the location in which a given string is stored. If we keep reassigning the string, will this lead to a memory leak?

On a Linux system, I see:

$ cat chk.c #include <stdio.h> #define VP (void *) int main() { char *str; str = "ABC"; printf("str = %p points to %s\n", VP str, str); str = "CBA"; printf("str = %p points to %s\n", VP str, str); return 0; } $ cc chk.c && ./a.out str = 0x8048490 points to ABC str = 0x80484ab points to CBA $ 

Q2. What is the maximum length of a string that can be assigned as above?

+4
source share
4 answers

Can your test code memory leak? No. You assign constant lines already in your program, so no additional memory allocation occurs.

Memory leaks are associated with forgotten calls like malloc () or calls that internally perform operations like mallocs () that you may not be aware of. Beware of functions returning a pointer to memory ... e.g. strdup (). These, as a rule, should not be thread safe or memory leaks, if not both. Functions like snprintf () are better when the caller provides both a memory buffer and a maximum size. These functions do not leak.

Maximum line length: There is no artificial limitation other than the available memory. The memory on the stack can be limited by various restrictions (char can_be_too_big [1_000_000]), but the memory from malloc () is not. Malloc memory is a question of how much free memory you have (char * ok = malloc (1_000_000). Local size_t provides the maximum memory for allocation in theory, but in practice it is much smaller.

+2
source

Memory leaks prevail only when we allocate memory using malloc/realloc/calloc and forget free . In the above example, there is no where we allocate memory for ourselves, therefore no memory loses AFAIK.

+1
source

OK, to be more specific, usually what happens (OS dependent, but AFAIK, it’s universal, maybe somewhere in the specification) is that somewhere in the command set of your executable file there are lines “ABC” and "CBA" - they are built into your program. When you do str="ABC" , you say: "I want this line pointer to point to an address in my compiled program containing the line ABC." That's why there is a difference between “strings” at run time and “string literals” if you see this in the documentation anywhere. Since you did not allocate space for your literal - the compiler baked it into your program - you do not need to free up space for it.

In any case, when your process is unloaded, the OS frees this resource as a natural side effect of unloading your program. In fact, in general, it is impossible to leak after the program is due to the OS freeing up any resources that you forgot, even disgusting leaks, when you exit the program. (this is not entirely true - you can force another program that does not download to leak if you linked the library material, but this is close enough). This is just one of those things that the OS cares about.

+1
source

when you select the memory for somePointer p and without freeing the memory or without pointing other pointers to this memory, if you change the value of p, then a situation will occur in this memory leak.

(eg)

 char* p = malloc(sizeof(char)*n); char* q= "ABC"; 

Then, if you have appointed,

p = d;

Then a memory leak will occur. If you are not using memory allocation, then there will be no memory leak.

and

char * q = "ABC";

In this statement, q will automatically indicate a permanent location. Therefore, the q value cannot be changed. (For instance)

 char* q = "ABC"; q[1] = 'b'; 

These instructions will result in a segmentation error.

MoreReference:

ErrorOnModifyingValue

DynamicMemoryAllocation

0
source

All Articles