Memory allocation (checking for success)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Resizing of an array.

 
0
  #1
Nov 30th, 2008
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!

  1. //'s' is the variable for the size.
  2. int Bank::grow(int s = 10){
  3.  
  4. int sizeOld = size; //used to save the size for future comparison
  5. if(savings != NULL){
  6. //create a new account object array
  7. Account *newSavingsArray = new Account[arraySize + s]
  8.  
  9. //copy all data from old array to new
  10. for ( int i = 0; i < arraySize; i++ ){
  11. newSavingsArray[i] = savings[i];
  12. }
  13.  
  14. delete savings;
  15. arraySize = arraySize + s;
  16. savings = newSavingsArray;
  17. }
  18.  
  19. else if (savings == NULL){
  20. //recreate the savings object with size 's'
  21. Account *savings = new Account[s]
  22. arraySize = s;
  23. }
  24.  
  25.  
  26. return arraySize;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Resizing of an array.

 
0
  #2
Nov 30th, 2008
Originally Posted by sid78669 View Post
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!

  1. //'s' is the variable for the size.
  2. int Bank::grow(int s = 10){
  3.  
  4. int sizeOld = size; //used to save the size for future comparison
  5. if(savings != NULL){
  6. //create a new account object array
  7. Account *newSavingsArray = new Account[arraySize + s]
  8.  
  9. //copy all data from old array to new
  10. for ( int i = 0; i < arraySize; i++ ){
  11. newSavingsArray[i] = savings[i];
  12. }
  13.  
  14. delete savings;
  15. arraySize = arraySize + s;
  16. savings = newSavingsArray;
  17. }
  18.  
  19. else if (savings == NULL){
  20. //recreate the savings object with size 's'
  21. Account *savings = new Account[s]
  22. arraySize = s;
  23. }
  24.  
  25.  
  26. return arraySize;
  27. }
How do i know if the memory allocation was successful or not? do i check the size? how do i check the size then?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Memory allocation (checking for success)

 
0
  #3
Nov 30th, 2008
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:
  1. #include <new>
  2. ...
  3. Type* p = new(nothrow) Type[...];
  4. if (p == 0) { // it's as good as lost
  5. // Now we're done for!
  6. }
Well, and what do you want to do if you can't grow up your bank accounts pool? Declare bankrupt ...
Last edited by ArkM; Nov 30th, 2008 at 3:04 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC