我有两种代码变体:
第一:
void PrintMem(const int* memarr,const size_t size) {
for (size_t index = 0; index < size; ++index) {
std::cout << '<'<<(index+1)<<"> "s<<*(memarr + index) << std::endl;
}
}
void FillMem(int* memarr, const size_t size) {
srand(time(0));
for (size_t index = 0; index < size; ++index) {
*(memarr + index) = rand() % 100;
}
}
int main() {
const int size_iter = 10000000;
int n = 30;
int* ptr = nullptr;
int size = size_iter;
for (int i = 1; i <= n; ++i) {
size = size_iter * i;
if (i == 1) {
ptr = (int*)malloc(size * sizeof(int));
}
else {
ptr = (int*)realloc(ptr, size * sizeof(int));
}
if (ptr == nullptr) {
printf("memory allocation error\n");
break;
}
std::cout << '[' << i << ']';
printf(" address: %p", (void*)ptr);
std::cout << ", size: "s << size;
std::cout << " *********************" << std::endl;
FillMem(ptr, size);
//PrintMem(ptr, size);
}
if (ptr != nullptr) {
free(ptr);
}
}
第二:
void PrintMem(const int* memarr,const size_t size) {
for (size_t index = 0; index < size; ++index) {
std::cout << '<'<<(index+1)<<"> "s<<*(memarr + index) << std::endl;
}
}
void FillMem(int* memarr, const size_t size) {
srand(time(0));
for (size_t index = 0; index < size; ++index) {
*(memarr + index) = rand() % 100;
}
}
int main() {
const int size_iter = 10000000;
int n = 30;
int* ptr = nullptr;
int size = size_iter;
for (int i = 1; i <= n; ++i) {
size = size_iter * i;
int* new_ptr = nullptr;
if (i == 1) {
new_ptr = (int*)malloc(size * sizeof(int));
}
else {
new_ptr = (int*)realloc(ptr, size * sizeof(int));
}
if (new_ptr == nullptr) {
printf("memory allocation error\n");
break;
}
ptr = new_ptr;
std::cout << '[' << i << ']';
printf(" address: %p", (void*)ptr);
std::cout << ", size: "s << size;
std::cout << " *********************" << std::endl;
FillMem(ptr, size);
//PrintMem(ptr, size);
}
if (ptr != nullptr) {
free(ptr);
}
}
释放数组内存的正确方法是什么?
if (ptr != nullptr) {
free(ptr);
}
或:
if (ptr != nullptr) {
for (int i = 0; i < size; ++i) {
free((ptr + i));
}
free(ptr);
}
我尝试了两种变体。
我认为带有
int* new_ptr
的第二个变体会更好,因为它至少会保留上一次内存调整大小的迭代。
我只需要知道如何优化它,如果只释放是正确的
ptr
还是我需要释放每个内存块?
每个数组实例仅调用
malloc()
/realloc()
一次,因此您只需调用 free()
一次即可释放数组。 不要尝试free()
单个元素。 每成功
free()
/malloc()
1 个realloc()
。
此外,在致电
nullptr
之前,您无需检查 free()
。
此外,如果
realloc()
确实失败,则您将泄漏之前的数组。您需要在重新分配 realloc()
变量之前检查是否存在 ptr
失败。试试这个:
void PrintMem(const int* memarr, const size_t size) {
for (size_t index = 0; index < size; ++index) {
std::cout << '<' << (index+1) << "> " << memarr[index] << std::endl;
}
}
void FillMem(int* memarr, const size_t size) {
for (size_t index = 0; index < size; ++index) {
memarr[index] = rand() % 100;
}
}
int main() {
srand(time(0));
const int size_iter = 10000000;
int n = 30;
int* ptr = nullptr;
int size = size_iter;
for (int i = 1; i <= n; ++i) {
size = size_iter * i;
if (i == 1) {
ptr = (int*) malloc(size * sizeof(int));
if (ptr == nullptr) {
printf("memory allocation error\n");
break;
}
}
else {
int *new_ptr = (int*) realloc(ptr, size * sizeof(int));
if (new_ptr == nullptr) {
printf("memory reallocation error\n");
break;
}
ptr = new_ptr;
}
std::cout << '[' << i << ']';
printf(" address: %p", static_cast<void*>(ptr));
std::cout << ", size: " << size;
std::cout << " *********************" << std::endl;
FillMem(ptr, size);
//PrintMem(ptr, size);
}
free(ptr);
}