In a program I allocate a huge multidimensional array, do some number-crunching, then only the first part of that array is of further interest and I’d like to free just a part of the array and continue to work with the data in the first part. I tried using realloc, but I am not sure whether this is the correct way, given I must preserve the data in the array and preferably avoid copying of that chunk in memory.
#include <cstring>
#include <cassert>
#include <iostream>
using namespace std;
void FillArrayThenTruncate(char* my_array, const int old_size, int* new_size);
int main() {
const int initial_size = 1024*1024*1024;
char* my_array = static_cast<char*>(malloc(initial_size));
assert(my_array);
int new_size;
FillArrayThenTruncate(my_array, initial_size, &new_size);
for(int i = 0; i < new_size; ++i) cout << static_cast<int>(my_array[i]) << endl;
}
void FillArrayThenTruncate(char* my_array, const int old_size, int* new_size) {
//do something with my_array:
memset(my_array, 0, old_size);
for(int i = 0; i < 10; ++i) my_array[i] = i % 3;
//cut the initial array
*new_size = 10;
void* new_array = realloc(my_array, *new_size);
cout << "Old array pointer: " << static_cast<void*>(my_array) << endl;
cout << "New array pointer: " << new_array << endl;
my_array = static_cast<char*>(new_array);
assert(my_array != NULL);
}
UPDATE:
* Please do not bother to suggest to use STL. The question is about C array.
* Thanks to “R Samuel Klatchko” for pointing out the bug in the code above.
Yes, if you allocate with
malloc, you can resize withrealloc.That said,
reallocis allowed to move your memory so you should be prepared for that:Note that if you are growing memory, the above snippet is dangerous as realloc can fail and return NULL in which case you will have lost your original pointer to my_array. But for shrinking memory it should always work.