| | |
Memory allocation (checking for success)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!
c++ Syntax (Toggle Plain Text)
//'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!
c++ Syntax (Toggle Plain Text)
//'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; }
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:
Well, and what do you want to do if you can't grow up your bank accounts pool? Declare bankrupt
...
If you want to get 0 in that case use another new variant:
c++ Syntax (Toggle Plain Text)
#include <new> ... Type* p = new(nothrow) Type[...]; if (p == 0) { // it's as good as lost // Now we're done for! }
... Last edited by ArkM; Nov 30th, 2008 at 3:04 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: CString to Integer / Double
- Next Thread: Problem with copy constructor.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






