简体   繁体   English

当没有指针指向char指针的记忆时会发生什么?

[英]What happens to the momory of a char pointer when no pointer is being pointed at it?

And by that I mean something like this. 我的意思是这样的。

int8_t *pt1 = malloc(sizeof(int8_t) * 10);
int8_t *pt2 = malloc(sizeof(int8_t) * 10);

pt1 = pt2;

Here, pt1 has a memory assigned. 此处,pt1已分配了一个内存。 But now it is being pointed at pt2. 但是现在它指向pt2。 Then what happens to the memory that was assigned to pt1? 那么分配给pt1的内存会怎样? And is there a way to free it even if you did not save its pointer value? 并且即使您没有保存其指针值,也有一种释放它的方法吗?

The memory still belongs to your application, and you no longer have the address stored to free it. 内存仍然属于您的应用程序,并且您不再存储要释放它的地址。 This is the root of all memory leaks. 这是所有内存泄漏的根源。

Your question has 2 parts: 您的问题分为2部分:

  1. what happens to the memory that was assigned to pt1? 分配给pt1的内存会怎样?

    The memory pt1 was pointing becomes inaccessible - although the memory is still allocated. pt1指向的内存变得不可访问-尽管仍在分配内存。 On the other hand, both pt1 and pt2 point to the same memory location. 另一方面, pt1pt2指向相同的存储位置。 This causes the program to occupy more memory than it needs and uses (ie- the memory location pt1 was initially pointing - that location can't be used anymore, but still occupied by the program) - which is named as memory leak . 这导致程序占用了超出其需要和使用的内存(即pt1最初指向的内存位置-该位置不再可用,但仍被程序占用)-称为内存泄漏

  2. is there a way to free it even if you did not save its pointer value? 有没有办法释放它,即使您没有保存其指针值?

    Before doing pt1 = pt2; 在做pt1 = pt2;之前pt1 = pt2; , you should call the free() function like free(pt1); ,您应该像free(pt1);一样调用free()函数free(pt1); - which will de-allocate the memory location pointed by pt1 . -将取消分配pt1指向的内存位置。

the memory is still occupied by the application unless until you free the memory allocation using the free() function or memory leak occurs. 除非您使用free()函数释放内存分配,否则内存将被应用程序占用,否则会发生内存泄漏。 for above question you need to write like 对于上述问题,您需要写像

  int8_t *pt1 = malloc(sizeof(int8_t) * 10);
  int8_t *pt2 = malloc(sizeof(int8_t) * 10);

  pt1 = pt2;
  free(pt2);

which will free the memory of pt2 . 这将释放pt2的内存。

In java this is taken care by Garbage Collector 在Java中,垃圾收集器会注意这一点

The memory which is allocated by an application, in especially Linux like system, belongs to the application as long as application is running. 只要应用程序正在运行,由应用程序分配的内存(特别是在类似Linux的系统中)就属于该应用程序。 So whatever memory your process has allocated will be taken from application's address space and for better memory utilization, application is expected to free the block if it no longer wants to use it. 因此,无论您的进程分配了什么内存,都将从应用程序的地址空间中获取,并且为了提高内存利用率,如果应用程序不再希望使用该块,则可以释放该块。 So, if an application keeps allocating memory without freeing, at one point of time, system may go out of address space and process will be killed for lack of memory. 因此,如果应用程序一直在分配内存而不释放空间,则在某个时间点,系统可能会耗尽地址空间,并且由于缺少内存,进程将被杀死。 But, if application is terminated without freeing any memory block, that memory block is owned by parent process and cleanup routine is run to free up that memory block. 但是,如果在不释放任何内存块的情况下终止了应用程序,则该内存块归父进程所有,并且运行清除例程以释放该内存块。

It's called a Lost Heap-Dynamic Variable and It Causes a Memory-Leak 这称为丢失的堆动态变量,它会导致内存泄漏

Lost Heap-Dynamic Variable 失堆动态变量

A lost heap-dynamic variable is an allocated heap-dynamic variable that is no longer accessible to the user program. 丢失的堆动态变量是分配的堆动态变量,用户程序不再可以访问该堆动态变量。 Such variables are often called garbage , because they are not useful for their original purpose, and they also cannot be reallocated for some new use in the program. 此类变量通常被称为垃圾 ,因为它们对于其原始目的没有用,并且也无法为程序中的某些新用途重新分配它们。 Lost heap-dynamic variables are most often created by the following sequence of operations: 丢失的堆动态变量通常是由以下操作序列造成的:

  1. Pointer pt1 is set to point to a newly created heap-dynamic variable. 指针pt1设置为指向新创建的堆动态变量。
  2. pt1 is later set to point to another newly created heap-dynamic variable. 稍后将pt1设置为指向另一个新创建的堆动态变量。

The first heap-dynamic variable is now inaccessible , or lost. 现在,第一个堆动态变量不可访问或丢失。 This is sometimes called memory leakage . 有时称为内存泄漏 Memory leakage is a problem, regardless of whether the language uses implicit or explicit deallocation. 内存泄漏是一个问题,无论该语言使用隐式还是显式释放。

Source: Concepts of Programming Languages 10th - Robert W. Sebesta 资料来源:《编程语言概念》第10期-Robert W. Sebesta

The OS will free the memory if you don't free it. 如果您不释放内存,则操作系统将释放内存。 BTW: shared_ptr will free the memory if no one use it. 顺便说一句:shared_ptr将释放内存,如果没有人使用它。 http://www.boost.org/doc/libs/1_64_0/libs/smart_ptr/shared_ptr.htm http://www.boost.org/doc/libs/1_64_0/libs/smart_ptr/shared_ptr.htm

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

相关问题 将 float 指针类型转换为 char 指针时会发生什么? - What happens when float pointer is typecasted to char pointer? 当将整数指针转换为char指针时,实际发生了什么? - What actually happens when a pointer to integer is cast to a pointer to char? 打印一个char指针......会发生什么? - printing a char pointer … what happens? 当您键入将整数值转换为char指针时会发生什么? - What happens when you type cast an integer value into a char pointer? 当我们将值重新分配给 char 指针时,内存会发生什么? - What happens with memory when we reassign value to char pointer? 当 long 变量的地址存储在 char 指针中时会发生什么? - What happens when address of a long variable is store in char pointer? char指针初始化相比非指针? 怎么了 - char pointer initialization compared to non pointer? What happens 当一个指针作为指向另一个函数内部的指针的指针传递时会发生什么? - what happens when a pointer is passed as a pointer to a pointer inside another function? 当我们进行 (char *) 转换以将 integer 数据存储到 char 指针时会发生什么? - What happens when we do (char *) casting to store integer data into char pointer? 文件指针指向的地址是什么? - what is the address pointed by file pointer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM