User Tools

Site Tools


cpp:exception

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:exception [2022/05/23 03:52] gthanoscpp:exception [2023/05/15 14:01] (current) gthanos
Line 11: Line 11:
 class Vector { class Vector {
   int *array;   int *array;
-  int size;+  long size;
      
 public: public:
-  Vector(int length=0);+  Vector(long length=0);
   ~Vector();   ~Vector();
   int &valueAt(int pos) const;  // returns a reference to element at position pos   int &valueAt(int pos) const;  // returns a reference to element at position pos
Line 22: Line 22:
 <code cpp Vector.cpp> <code cpp Vector.cpp>
 #include "Vector.hpp" #include "Vector.hpp"
-Vector::Vector(int length) {+Vector::Vector(long length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 46: Line 46:
 </code> </code>
  
-Αν και η παραπάνω διαδικασία δεν είναι λανθασμένη, έχει το βασικό μειονέκτημα ότι θα πρέπει να τερματίσουμε το πρόγραμμα, ακόμη και εάν ο λόγος αποτυχίας είναι ότι ο χρήστης της κλάσης επέτρεψε το πέρασμα αρνητικής τιμής ως όρισμα στον κατασκευαστή. Ο λόγος είναι ότι στην περίπτωση που αποτύχει η δέσμευση της μνήμης, λόγω λανθασμένου ορίσματος, ο κατασκευαστής της κλάσης **Vector** επιστρέφει ένα αντικείμενο το οποίο δεν είναι σωστά αρχικοποιημένο.+Αν και η παραπάνω διαδικασία δεν είναι λανθασμένη, έχει το βασικό μειονέκτημα ότι θα πρέπει να τερματίσουμε το πρόγραμμα. Ο λόγος είναι ότι στην περίπτωση που αποτύχει η δέσμευση της μνήμης, λόγω λανθασμένου ορίσματος, ο κατασκευαστής της κλάσης **Vector** επιστρέφει ένα αντικείμενο το οποίο δεν είναι σωστά αρχικοποιημένο.
  
 <code cpp VectorUse.cpp> <code cpp VectorUse.cpp>
Line 52: Line 52:
  
 int main() { int main() {
-  int size;+  long size;
   cout << "Enter verctor size: ";   cout << "Enter verctor size: ";
   cin >> size;   cin >> size;
Line 71: Line 71:
 class Vector { class Vector {
   int *array;   int *array;
-  int size;+  long size;
      
 public: public:
-  Vector(int length=0);+  Vector(long length=0);
   ~Vector();   ~Vector();
   int &valueAt(int pos) const;  // returns a reference to element at position pos   int &valueAt(int pos) const;  // returns a reference to element at position pos
Line 84: Line 84:
 #include "Vector.hpp" #include "Vector.hpp"
  
-Vector::Vector(int length) {+Vector::Vector(long length) {
   size = length;   size = length;
   array = new int[size];   array = new int[size];
Line 108: Line 108:
  
 int main() { int main() {
-  int size; +  long size; 
-  do { +   
-    cout << "Enter verctor size: "; +  cout << "Enter verctor size: "; 
-    cin >> size; +  cin >> size; 
-    try { +  try { 
-      Vector v(size); +    Vector v(size); 
-    } catch(std::bad_alloc ex) { +  } catch(std::bad_alloc ex) { 
-      if(size<=0) { +    std::cout << "Allocation failure!\n"; 
-        cout << "Vector size should be a positive integerRetry...\n"; +    exit(-1); 
-        continue; +  
-      } +
-      exit(-1); +
-    +
-    for(int i=0; i<size; i++) +
-    v.valueAt(i) = 100-1; +
-  } while(size<1);  +
 } }
 </code> </code>
cpp/exception.1653277939.txt.gz · Last modified: 2022/05/23 03:52 by gthanos