tags:

views:

359

answers:

6

I need to resize a char array[size] to char array[new_size] at runtime.

How can I do this?

+2  A: 

You have to allocate a new array and copy the contents of the existing array to it. You can't simply make the existing array larger

Matt
I have to declare the char array[] in the class header, but I need to build it according the length of a word, I am doing the hang up game...
HoNgOuRu
@HoNgOuRu : put this in your question - it's important.
MSalters
+17  A: 

If you were using std::vector<char> rather than arrays, then the feature you want would be just another method on the type.

Steve Gilham
+1  A: 

Something like this:

class HangUpGame {
    char *palabra;
    size_t palabra_size;

    public:
        HangUpGame(){
            palabra = new char[DEFAULT_SIZE];
            palabra_size = DEFAULT_SIZE;
        }
        virtual ~HangUpGame(){
            delete [] palabra;
        }

        void Resize(size_t newSize){
            //Allocate new array and copy in data
            char *newArray = new char[newSize];
            memcpy(newArray, palabra, palabra_size);

            //Delete old array
            delete [] palabra;

            //Swap pointers and new size
            palabra = newArray;
            palabra_size = newSize;
        }
};

In response to the comments on other answers, the best way to do this actually would be to use an STL container. But anyway, if you prefer to use arrays, it's quite easy to swap the current array with a bigger one (internally the STL containers will do exactly that anyway).

Lck
Can you improve your answer so that it actually compiles please?
quamrana
Well, the version before did compile... depending on where you wrote it. Anyway, the new code compiles fine and should work correctly.
Lck
This code has several major bugs in it. Please look up the shallow copy problem. Please read this for the corrections you need: http://stackoverflow.com/questions/255612/c-dynamically-allocating-an-array-of-objects/255744#255744
Martin York
A: 

Delete the old array, if any, and then allocate a new one:

char* array = (char*)malloc(sizeof(char) * number_of_chars_in_word);
Cecil Has a Name
thanks for all the answers, but my problem is that I cannot change the array name cause its defined in the object, I have to define it with a size, but I don't have one before I load a word, and this happens in the constructor. in the class definition I have,string palabra;char palocu[size_of_palabra];(but at this point I dont have size_of_palabra, cause it will be loaded in the constructor, that's my problem...I need this cause I have to access "palocu" at a later time from the main class...
HoNgOuRu
Like we've been saying, use a `std::vector`.
GMan
@HoNgOuRu: Please show us the code. You can probably get away with a char* palocu; member which is `new`ed in the constructor and `delete`d in the destructor. But you may still be better off with `std::vector`.
quamrana
+5  A: 

assuming char array[size] was malloc'ed....you can use realloc

example (taken from OpenBSD's man page):

newsize = size + 50;
if ((newp = realloc(p, newsize)) == NULL) {
   free(p);
   p = NULL;
   size = 0;
   return (NULL);
}
p = newp;
size = newsize;
Los
Ew. [15 chars]
GMan
@GMan: that's very standard, even idomatic. I mean, `std::vector` does it or something equivalent, you just don't have to look at it, and if coding it by hand, you'd be well advised to wrap it up too.
dmckee
Of course. It's more abstracted in a vector. A vector calls an allocator, and that allocator can do whatever it needs.
GMan
Though available in C++, malloc is not the standard way of allocating memory in C++. If the questioner was asking about C then this would be a valid answer but the questioner is asking about C++.
Martin York
Yes, using a STL container like vector should be used but that's not what he asked. Read the question in his post.
Los
A: 

ok, thanks for all the answers, I fixed my problem just by creating a new space for the new char array throwght a pointer... thanks

HoNgOuRu
No, no, no. Use a `std::vector`. Why would you do otherwise? You're reinventing the wheel, and it'll probably be buggy at that.
GMan
@GMan: Yes, and will probably leak memory.
quamrana
...or maybe a `std::string`?
Jefromi
It's not homework, is it?
Cecil Has a Name
@GMan, @quamrana: Using a STD container like vector would be ideal, unless his data structure is exposed through a lib (ie...a C program needs to use it). Just because he's using a char pointer array does not mean it'll leak memory (as long as all cases are considered).@Cecil Has a Name: This better not be some kids high school homework. ;)
Los