2

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.

1
  • 1
    realloc allocates a new block. But in your code Buffsz keeps pointing to the old block that just got freed. Also, your code allocates one char, writes to the first element, reallocates it to zero chars, writes to the second element (that doesn't exist), reallocates to one char, writes to the third element (that doesn't exist), ... Commented Nov 25, 2016 at 3:50

3 Answers 3

6

realloc() is used to resize C-style arrays that were allocated with malloc(). realloc() cannot be used in C++ code, to resize C++ objects that were instantiated in dynamic scope with new.

There is no equivalent for realloc() in C++. In C++, the simplest way to resize an existing array is: a new array must be constructed in dynamic scope with new [], the values from the existing array std::copy-ed to the new array, and then the old array delete[]-ed.

That's a lot of work. This will involve a lot of unnecessary default-construction and copy-assignment (and if your classes do not have default constructors, you're stuck). Using placement-new and manual copy/move constructors it is possible to optimize some of this work. But it is a lot of work. And that's why you should use std::vector. It does all this for you, and it does this correctly.

There's nothing wrong with using new and delete yourself, as you would like to do. This is good learning experience, in order to gain some understanding how to properly manage dynamically-scoped objects. Fully understanding how low level dynamic scoping works is valuable knowledge to have. However, at some point all of this gets real old, and non-trivial tasks, involving dynamically-scoped objects, become tedious and error prone, and a major time-burner.

At that point, the only way to retain what's left of one's sanity is to start using C++ library containers, which take care of all the grunge work for you. In conclusion: use std::vector, and let it do all this work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

I did not know about that, i thought it was possible, i haven't choice but to use std::vector. thanks a lot.
3

You cannot use realloc() on pointer allocated memory with new operator. You could use this function to resize a pointer

char* Resize(char*& old,long int length,long int resize_to)
{
    char* new_ptr;
    new_ptr = new char[ resize_to ];
    long int least = ( length < resize_to ) ? length : resize_to;
    for(long int i = 0;i < least ; ++i)
        new_ptr [i] = old[i];
    delete [] old;
    old = nullptr;
    return new_ptr;
}

This Might work and don't forget to delete[] that reallocated pointer after it is no longer needed or you'll have to worry about a memory leak

Though you might want to us std::string or std::vector or create your own dynamically expanding Stack class.

Comments

1

To use realloc the memory must have been allocated with malloc or other function in that family.

The simplest way to deal with strings of char in C++ is to use std::string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.