gpt4 book ai didi

c++ - 如何释放连续内存块的一部分?

转载 作者:行者123 更新时间:2023-11-30 00:44:02 29 4
gpt4 key购买 nike


假设我有一个结构 MyStruct 并且我想像这样分配一个“大”内存块:

std::size_t memory_chunk_1_size = 10;
MyStruct * memory_chunk_1 = reinterpret_cast <MyStruct *> (new char[memory_chunk_1_size * sizeof(MyStruct)]);

并且由于“任意原因”,我想将这 block 内存“拆分”成两个较小的 block ;移动数据、调整“动态数组”的大小、取消分配/分配/重新分配内存等。

所以我这样做:

std::size_t memory_chunk_2_size = 5;              // to remember how many elements there are in this chunk;
MyStruct * memory_chunk_2 = &memory_chunk_1[5]; // points to the 6th element of memory_chunk_1;

memory_chunk_1_size = 5; // to remember how many elements there are in this chunk;
memory_chunk_1 = memory_chunk_1; // nothing changes still points to the 1st element.

不幸的是,当我尝试释放内存时,我遇到了一个错误:

// release memory from the 2nd chunk
for (int i = 0; i < memory_chunk_2_size ; i++)
{
memory_chunk_2[i].~MyStruct();
}
delete[] reinterpret_cast <char *> (memory_chunk_2); // deallocates memory from both "memory_chunk_2" and "memory_chunk_1"

// release memory from the 1st chunk
for (int i = 0; i < memory_chunk_1_size ; i++)
{
memory_chunk_1[i].~MyStruct(); // Throws exception.
}
delete[] reinterpret_cast <char *> (memory_chunk_1); // Throws exception. This part of the memory was already dealocated.

如何只删除选定数量的元素(以解决此错误)?

可编译示例:

#include <iostream>

using namespace std;

struct MyStruct
{
int first;
int * second;

void print()
{
cout << "- first: " << first << endl;
cout << "- second: " << *second << endl;
cout << endl;
}

MyStruct() :
first(-1), second(new int(-1))
{
cout << "constructor #1" << endl;
print();
}

MyStruct(int ini_first, int ini_second) :
first(ini_first), second(new int(ini_second))
{
cout << "constructor #2" << endl;
print();
}

~MyStruct()
{
cout << "destructor" << endl;
print();

delete second;
}
};

int main()
{
// memory chunk #1:
std::size_t memory_chunk_1_size = 10;
MyStruct * memory_chunk_1 = reinterpret_cast <MyStruct *> (new char[memory_chunk_1_size * sizeof(MyStruct)]);

// initialize:
for (int i = 0; i < memory_chunk_1_size; i++)
{
new (&memory_chunk_1[i]) MyStruct(i,i);
}

// ...
// Somewhere here I decided I want to have two smaller chunks of memory instead of one big,
// but i don't want to move data nor reallocate the memory:

std::size_t memory_chunk_2_size = 5; // to remember how many elements there are in this chunk;
MyStruct * memory_chunk_2 = &memory_chunk_1[5]; // points to the 6th element of memory_chunk_1;

memory_chunk_1_size = 5; // to remember how many elements there are in this chunk;
memory_chunk_1 = memory_chunk_1; // nothing changes still points to the 1st element.

// ...
// some time later i want to free memory:

// release memory from the 2nd chunk
for (int i = 0; i < memory_chunk_2_size ; i++)
{
memory_chunk_2[i].~MyStruct();
}
delete[] reinterpret_cast <char *> (memory_chunk_2); // deallocates memory from both "memory_chunk_2" and "memory_chunk_1"

// release memory from the 1st chunk
for (int i = 0; i < memory_chunk_1_size ; i++)
{
memory_chunk_1[i].~MyStruct(); // Throws exception.
}
delete[] reinterpret_cast <char *> (memory_chunk_1); // Throws exception. This part of the memory was already dealocated.

// exit:
return 0;
}

最佳答案

C++ 编程语言不支持这种选择性释放,而且可能永远不会得到支持。

如果您打算取消分配内存的各个部分,则首先需要单独分配这些单独的部分。

有可能特定的操作系统或平台可能支持这种行为,但它会使用特定于操作系统的系统调用,而不是通过 C++ 标准语言语法。

关于c++ - 如何释放连续内存块的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291038/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com