简体   繁体   English

删除多个字符指针

[英]Deleting multiple char pointers

I'm having trouble deleting these two pointers: 我在删除以下两个指针时遇到问题:

int *p = new int;
    char * string1 = new char[20];
    char * string2 = new char[25];
    strcpy(string1, "Sheldon");

    string2 = string1;
    delete p;
    delete[] string2; //this works without the next line
    delete[] string1; //when I add this, it fails at string2 = string1

I'm using the memory leak detection with 我正在使用内存泄漏检测

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

When I run he program without "delete[] string1," it gives me "{66} normal block at 0x0075E300, 25 bytes long." 当我运行没有“ delete [] string1”的程序时,它给我“ {66}正常块,位于0x0075E300,长度为25个字节”。 So "delete[] string2" is deleting string1. 因此,“ delete [] string2”正在删除string1。 Which doesn't make sense to me, but I'm guessing it has to do with the assignment. 这对我来说没有意义,但我猜想这与作业有关。 I tried looking up stuff about it, but nothing has worked. 我尝试查找有关此内容的信息,但没有任何效果。

The code fails because you're deleting the same pointer twice, which results in undefined behavior (see here ). 代码失败,因为您两次删除了相同的指针,这导致未定义的行为 (请参见此处 )。 After the statement string2 = string1; 在语句之后string2 = string1; the pointers string1 and string2 hold the same address, and you can no longer access the address stored in string2 before the assginment, which also results in a memory leak . 指针string1string2拥有相同的地址,并且在组合之前您不能再访问存储在string2的地址,这也会导致内存泄漏

If you wanted to copy string1 to string2 , you shold use strncpy(string2, string1, 20) (see documentation ) in which case the pointers themselves remain unchanged and the deallocation code you provided is valid. 如果要将string1复制到string2 ,请使用strncpy(string2, string1, 20) (请参阅文档 ),在这种情况下,指针本身保持不变,并且您提供的解除分配代码有效。

You will have to delete the memory assigned to string2 before being assigned string1 . 在分配给string1之前,您必须删除分配给string2的内存。

AFter asssignment, string2 and string1 are one and same. 后asssignment, string2string1是一个相同的。 Check below code for reference: 检查以下代码以供参考:

int *p = new int;
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, "Sheldon");
delete[] string2; //this would prevent memory leak
string2 = string1;
delete p;

delete[] string1; //now you are releasing the memory held by string2 too.

As you already identified, the problem in your code is the assignment. 正如您已经确定的那样,代码中的问题是分配。 After string2 = string1; string2 = string1; , both string2 and string1 point to the same memory location. string2string1指向相同的存储位置。 Therefore, when you call delete[] string2 you are free the memory pointed by string1 . 因此,当您调用delete[] string2 ,将释放string1指向的内存。 Then you call delete[] string1 and you get undefined behaviour as the memory to which the pointer points has already been freed. 然后,调用delete[] string1 ,由于指针所指向的内存已被释放,您将得到未定义的行为 That is, you are free twice the same pointer. 也就是说,您有两次相同的指针空闲。

Also, your code contains a memory leak because you are not de-allocating the initial memory reserved for string2 . 另外,您的代码包含内存泄漏,因为您没有取消分配为string2保留的初始内存。 What you should do is : 您应该做的是:

    delete[] string2;  // release the memory for string2
    string2 = string1; // string1, string2 point to the same memory area
    delete[] string1; // release the memory for string1
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, "Sheldon");

string2 = string1;
delete p;
delete[] string2; //this works without the next line
delete[] string1; //when I add this, it fails at string2 = string1

Three problem: 三个问题:

  1. The original char array which the original variable string2 pointed to would never be deleted. 原始变量string2指向的原始char数组永远不会删除。 (memory leak) (内存泄漏)
  2. By delete[] string2; 通过delete[] string2; the original char array which string1 pointed to would be deleted. string1指向的原始char数组将被删除。 (recycle unintended memory) (回收意外的内存)
  3. By delete[] string1; 通过delete[] string1; after delete[] string2; delete[] string2; , the memory that already deleted, would be deleted again.(fail) ,已经删除的内存将再次删除。(失败)

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

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