开发者

Does continous reassigning of character strings lead to memory leak?

开发者 https://www.devze.com 2023-04-02 00:50 出处:网络
I have two questions: Q1. The character pointers are used to point to a location where a given string is stored. If we keep reassigning the string, does it lead to memory leak?

I have two questions:

Q1. The character pointers are used to point to a location where a given string is stored. If we keep reassigning the string, does it lead to 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?


Can your sample code memory leak? No. You are assigning constant strings already in your program so no extra memory allocation happens.

Memory leaks are from forgotten malloc() type calls, or calls that internally do mallocs() type operations that you may not be aware of. Beware of functions that return a pointer to memory... such as strdup(). Such tend to either be not be thread safe or leak memory, if not both. Better are functions like snprintf() where the caller provides both a memory buffer and a maximum size. These function's don't leak.

Maximum string length: tends to have no artificial limit except available memory. Memory in the stack may be limited by various constraints (char can_be_too_big[1_000_000]), but memory from malloc() is not. Malloc memory ias a question of how much free memory you have (char * ok = malloc(1_000_000). Your local size_t provides the maximum memory to allocate in theory, but in practice it is much smaller.


Memory leaks only prevail when we allocate memory using malloc/realloc/calloc and forget to free it. In the above example, no where the we are allocating memory our self, so no memory leaks AFAIK.


OK, to be more specific, usually what happens (OS-specific, but AFAIK, this is universal, possibly in a spec somewhere) is that somewhere in the instruction set of your executable are the strings "ABC" and "CBA" - these are embedded in your program itself. When you do str="ABC" you are saying, "I want this string pointer to point to the address in my compiled program that contains the string ABC". This is why there is a difference between "strings" at runtime and "string literals" if you see that in documentation anywhere. Since you didn't allocate space for your literal - the compiler baked it into your program - you don't have to deallocate space for it.

Anyway, when your process gets unloaded the OS frees up this resource as a natural side effect of unloading your program. In fact, in general, it is impossible to leak after a program exits because the OS will deallocate any resources you forget to, even heinous leaks, on program exit. (this is not entirely true - you can cause another program, which is not unloaded, to leak if you do linked library stuff - but it's close enough). It's just one of those things that the OS takes care of.


when you Allocate Memory for somePointer p,and without freeing the memory or without making the other pointers to point to that memory,if you change the value of p,then in this situvation memory leak occurs.

(E.g)

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

Then if you assign,

p=q;

Then there will be a memory leak. If you dont use memory allocation,Then there wont be any memory leak.

And,

char* q= "ABC";

In this statement q will be automatiacally points to a constant location. Hence q value cannot be modified. (E.g)

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

These statements will result in segmentation Fault.

MoreReference:

ErrorOnModifyingValue

DynamicMemoryAllocation

0

精彩评论

暂无评论...
验证码 换一张
取 消