How to allocate more memory for an array

Dec 9, 2011 at 3:09am
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 Dec 9, 2011 at 3:12am
Dec 9, 2011 at 3:14am
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.
Dec 9, 2011 at 3:22am
You could also look up realloc
Dec 9, 2011 at 6:10am
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)
Dec 9, 2011 at 10:05am
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 Dec 9, 2011 at 10:08am
Dec 9, 2011 at 6:24pm
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 Dec 9, 2011 at 9:25pm
Dec 9, 2011 at 7:29pm
How do you use this function in the main () ;
Dec 9, 2011 at 8:52pm
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.
Dec 9, 2011 at 9:22pm
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 Dec 9, 2011 at 9:23pm
Dec 9, 2011 at 9:32pm
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
Dec 9, 2011 at 9:34pm
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.
Dec 10, 2011 at 10:03am
Hi , TAZO that means i have to create the pointer object of the class and assign the return class by the function resize() ;
Dec 10, 2011 at 8:26pm
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 Dec 12, 2011 at 4:59pm
Topic archived. No new replies allowed.