How to allocate more memory for an array

I understand vectors do it for you, but as a learning experience, how would I allocate more memory for an array?

For example:
 
myClass * classArray = new myClass[25];


but when it reaches about 20, I would like 50.

Instead of just declaring 50, because when it drops to about 15, I would like to drop it back to 25.

Any help? Thanks in advance. George

Edit: I would like to avoid the C malloc() and free, and I would like to keep the name (classArray).
Last edited on
The standard practice is to create a different one of the new size, then copy the elements from the old one, then release the old array.
You could also look up realloc
Thank you, but if I created a different array, it wouldn't have the same name, unless I created a temporary array, released the first one, then recreated it and recopied.
But then I would have to make and overload a function for every class, and I was hoping there is an easier way (besides using the C libraries)
Nope, you only need to copy the array once. Later you ajust the pointer.
But I would recommend typedef std::vector<myClass> myClassArray;

But then I would have to make and overload a function for every class
I'm not sure that I follow you. ¿what do you need to overload? Still, you could use templates.
Last edited on
Hi George,
try this, I hope it meets your need. I wrote it in based on what I understood from your question. -TAZO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//main{
    //myClass a;

    //if(a.getSize() =< 15 || a.getSize() >= 20)
	//a.resize();
    // or just call the function;
    // a.resize();
    // you could also chain to each other.
    // a.resize().resize();
//}

//your initial classArray
//myClass * classArray = new myClass[25];

myClass & myClass::resize( ){
    myClass * temp = NULL;
    temp = classArray;
    if(size >= 20)
        classArray = new myClass[50];
    else if(size =< 15)
	classArray = new myClass[25];
    for(unsigned i = 0; i < size; i++)
        classArray[i] = temp[i];
    size = i;
    delete[] temp;
    return *this;
}

Last edited on
How do you use this function in the main () ;
Due to an abominable oversight in the design of C++, there is no renew operator. Therefore you will either have to allocate a new one and copy it in, use malloc instead of new, or use a vector instead of an array.
1
2
3
4
5
6
7
8
template<class T>
void ResizeArray(T* &array,int old_size,int new_size){        
        if(old_size==new_size)return;
        int n=new_size*sizeof(T);
        void* p=realloc(array,n);
        if(old_size < new_size)memmove(p,(void*)array,n);
        array=static_cast<T*>(p);
}
Last edited on
bluecoder,
How do you use this function in the main () ;


Simply, you invoke the class member method myClass & resize();
which returns reference to it-self (as i have shown in the previous post). You could invoke this function from anywhere as long as you are in the scope of myClass. -TAZO
realloc() will not work if myClass is not trivially-copyable (say, it has a string or a vector member or has a nontrivial copy constructor). This is C++, use vectors by default.
Hi , TAZO that means i have to create the pointer object of the class and assign the return class by the function resize() ;
I'm sorry bluecoder, I don't understand what you mean by saying:
assign the return class by the function resize() ;

If you are referring to the return type of the member function resize(); being myClass & what you will need to return is the "this" pointer which is already created by the run time of your program. When you have class in you program "this" pointer will point your data & method members. When you return "this" basically you are returning the location of you function member.
Now, you could also make your function returning type "void" instead of "reference to self", but then you can't chain your function.
Last edited on
Topic archived. No new replies allowed.