Şimdi Ara

bad_alloc sorunu c++

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
1 Misafir - 1 Masaüstü
5 sn
4
Cevap
0
Favori
855
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • int pointer arrayi tanımladığımda bad alloc hatası alıyorum. Hatanın ne olduğunu araştırmama rağmen bulamadım, bunu düzeltmenin yolu nedir?



  • kodun ilgili kismini yollarsaniz daha iyi yardimci olabiliriz...
  • 	GradeForm temp; 
    int currentGrade;

    for(int i=0; i<courseNumber; i++) {

    if( courses[i].courseId == courseId){

    for( int z=0; z<courses[i].numberOfExams; z++){
    if(courses[i].examsAndGrades[z].formId == formId){
    cout<<"already exist"<< endl;
    return;
    }
    }

    temp.formGrades = new int[courses[i].numberOfStudents]; // SORUNLU OLAN YER

    for(int j=0; j<courses[i].numberOfStudents; j++){
    cout<<"Enter grade for student " << courses[i].students[j].stuId<<": ";
    cin>>currentGrade;
    temp.formGrades[j]=currentGrade;
    }
    if(courses[i].numberOfStudents==0)
    cout<<"No student is taking Course "<<courseId<<endl;

    delete [] courses[i].examsAndGrades[courses[i].numberOfExams].formGrades;
    courses[i].examsAndGrades[courses[i].numberOfExams] = temp;

    courses[i].numberOfExams++;

    cout<< "Gradeform "<< formId<< " has been added to Course "<< courseId<<endl;
    return;
    }
    }
    cout<<"Course" << courseId << " does not exist"<<endl;
    }


    sorun tam olarak bu kodda mı onu da bilmiyorum aslında, çünkü program artık hiçbir şekilde pointer arrayi tanımlamama izin vermiyor.




  • new Operator (C++)
    ......
    If unsuccessful, new returns zero or throws an exception; see The new and delete Operators for more information. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument.

    ......

    The class describes an exception thrown to indicate that an allocation request did not succeed.


    class bad_alloc : public exception {
    bad_alloc(const char *_Message);
    bad_alloc();
    virtual ~bad_alloc();
    };


    Remarks
    An instance of bad_alloc can be constructed from a message or from a message string with no memory allocation.

    The value returned by what is an implementation-defined C string. None of the member functions throw any exceptions.

    Requirements
    Header: <new>

    Namespace: std

    Example
    Copy Code
    // bad_alloc.cpp
    // compile with: /EHsc
    #include<new>
    #include<iostream>
    using namespace std;

    int main() {
    char* ptr;
    try {
    ptr = new char[(~unsigned int((int)0)/2) - 1];
    delete[] ptr;
    }
    catch( bad_alloc &ba) {
    cout << ba.what( ) << endl;
    }
    }


    Kaynak : MSDN 2008 Express

    courses.numberOfStudents in değerinin o satırda ne olduğuna dikkat edin.
    courses bir sınıf ise sınıf kurucularında numberOfStudents e mantıklı bir atama yapmak gerek.




  • Yapay Zeka’dan İlgili Konular
    Daha Fazla Göster
    
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.