I'm trying resize a char array, i've followed: Resizing a char[] at run time
then:
i've done something like this:
// this crashes in runtime:
const long SIZE_X = 1048576;
char* Buffsz = new char(sizeof(char));
for(int i = 0; i < (SIZE_X - 2); i++)
{
Buffsz[i] = 'a';
if(realloc(Buffsz, sizeof(char) * i) == NULL) // autoallocate memory
cout << "Failled to reallocate memory!" << endl;
}
but if I do:
// this works without problems.
const long SIZE_X = 1048576;
char* ABuffsz = new char[SIZE_X];
for(int i = 0; i < (SIZE_X - 2); i++)
{
ABuffsz[i] = 'a';
}
cout << "End success! len: " << strlen(ABuffsz) << endl;
For me this should be fine, but if it's wrong, how i can auto allocate memory?
P.S: I know about of use std::vector but i want use this if its possible.