| |

VerySource

 Forgot password?
 Register
Search
View: 736|Reply: 8

How do I free up memory for a dynamically created string array? ?

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-25 21:40:01
| Show all posts |Read mode
char ** Buffer = new char * [1000];


Use delete Buffer; or delete [] Buffer;
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-15 21:45:01
| Show all posts
delete [] Buffer is right, your misunderstanding is that after you think that after release,
Buffer value should correspond to a "released" state

But in the centuries, as a pointer, it was represented as an integer (or something similar),
This integer indicates the address (physical address or virtual address) of the variable or array in memory.
After the content of the pointed address is released, this piece of memory can no longer be accessed unless
Another pointer points to it.

But your pointer variable is not like this, it still retains this integer, corresponding to a certain address,
But you do n’t need to worry, the memory pointed to has already been released correctly.

The good style is that after you delete [] buf ;, you write buf = 0, which can avoid some
Unnecessary confusion
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-15 22:15:02
| Show all posts
The sentence above is not accurate. The last sentence of the second paragraph should be: After the content of the address pointed to is released,
This piece of memory can no longer be accessed unless it is reallocated and another pointer points to it.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-16 00:15:01
| Show all posts
Thank youverance, use the task manager to monitor the memory, use Buffer to store the contents of the read text file, read the 10MB file, delete [] Buffer; Buffer = NULL; and find that the memory has not been released yet.

Why is that?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-16 07:45:01
| Show all posts
BCB's TStringList is very inefficient, very obvious when the file is large
Dump content into Buffer


TStringList * List = new TStringList ();
List-> LoadFromFile ("Dir.txt");
int NCount = List-> Count;
char ** Buffer = new char * [NCount];

    for (int i = 0; i <NCount; i ++)
    {
     int len ​​= NetList-> Strings [i] .Length ();
     * Buffer = new char [len + 1];
     strcpy (* Buffer, List-> Strings [i] .c_str ());
     ** Buffer ++;
    }
//

....
//
delete List; // this can be released
delete [] Buffer; // This doesn't seem to work
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-2-22 08:45:02
| Show all posts
Your delete simply returns memory from your program to the system heap management function.
The task monitor monitors the memory requested by the system heap management function from OS.
This is 2 yards.
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-8 15:30:02
| Show all posts
Whatsuperandysaid might be a possibility. In this case, there is nothing for you.

There is another possibility:
Is it because after you have allocated 1000 pointers, you use these thousand pointers to point to a thousand dynamically allocated memory areas,
And before releasing the pointer array, you didn't release this thousand memory areas, causing a leak? ? ?
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-12 21:30:02
| Show all posts
char ** Buffer = new char * [100];
for (int i = 0; i <100; i ++) {
delete Buffer [i];
}

delete Buffer;
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-10 09:30:01
| Show all posts
Thanks togoodjianli, the test was successful
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list