I have a very similar issue. I am also doing and assignment and needed a function grow. This was to increase the size of the object array Savings by s, if it was not NULL, or make its size s if it was null. In both the case it was to check if the memory allocation was successful. This is where i get stuck. Any suggestions on how to get the size and compare it with the old one, other than ofcourse creating a for loop! LOL!

//'s' is the variable for the size.
int Bank::grow(int s = 10){
		
    int sizeOld = size; //used to save the size for future comparison
    if(savings != NULL){
        //create a new account object array
        Account *newSavingsArray = new Account[arraySize + s]
        
        //copy all data from old array to new
        for ( int i = 0; i < arraySize; i++ ){
              newSavingsArray[i] = savings[i];
            }
         
        delete savings;
        arraySize = arraySize + s;
        savings = newSavingsArray;
        } 
    
        else if (savings == NULL){
            //recreate the savings object with size 's'
            Account *savings = new Account[s]
            arraySize = s;
        }


	return arraySize;
	}

I have a very similar issue. I am also doing and assignment and needed a function grow. This was to increase the size of the object array Savings by s, if it was not NULL, or make its size s if it was null. In both the case it was to check if the memory allocation was successful. This is where i get stuck. Any suggestions on how to get the size and compare it with the old one, other than ofcourse creating a for loop! LOL!

//'s' is the variable for the size.
int Bank::grow(int s = 10){
		
    int sizeOld = size; //used to save the size for future comparison
    if(savings != NULL){
        //create a new account object array
        Account *newSavingsArray = new Account[arraySize + s]
        
        //copy all data from old array to new
        for ( int i = 0; i < arraySize; i++ ){
              newSavingsArray[i] = savings[i];
            }
         
        delete savings;
        arraySize = arraySize + s;
        savings = newSavingsArray;
        } 
    
        else if (savings == NULL){
            //recreate the savings object with size 's'
            Account *savings = new Account[s]
            arraySize = s;
        }


	return arraySize;
	}

How do i know if the memory allocation was successful or not? do i check the size? how do i check the size then?

In modern C++ the global default operator new never returns 0. If it's impossible to obtain sufficient storage then std::bad_alloc exception raised. Of course you can catch this exception but as usually the best choice is to abort the program.

If you want to get 0 in that case use another new variant:

#include <new>
...
Type* p = new(nothrow) Type[...];
if (p == 0) { // it's as good as lost
// Now we're done for!
}

Well, and what do you want to do if you can't grow up your bank accounts pool? Declare bankrupt ;)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.