2013-06-19 35 views
0

如果我們使用任何原始數據類型但不適用於ADT,它將起作用即使所有拷貝構造函數「>>」「< <」「=」運算符都被重載,並且複製構造函數寫你可以看到下面感謝每一個代碼提前在動態模板陣列中使用ADT時程序崩潰

void main(){ 

    Array <Item> c;//It will work if we use Any Permitive Data Type but not Working for ADT Even though all copy constructor/>> << operators are overloaded 

Item obj(334,"Mango Juice",90,"Drinks",10); 

c.insertAtStart(obj); 
c.insertAtStart(obj);/////The program Crashes Here!! 
c.insertAtStart(obj); 


c.PrintArray(); 
cout<<endl;` 


////while Array.h is given below 
` 
#ifndef H_ARRAY 
#define H_ARRAY 

#include<iostream> 
using namespace std; 

template<class T> 
class Array 
{ 
private: 
    T *a; 
    int size;    // total size 
    int length_used;  // used size 
public: 

    Array():a(NULL),size(0),length_used(0){} 

    void insertAtStart(T val){ 

    if(isEmpty()){ 
     a=new T(); 
    a[0]=val; 

    length_used++; 
    size++; 
    cout<<"Pehli condition"<<endl; 
    } 

    else{ 

     if(size>length_used){ 
      shiftRight(); 
     a[0]=val; 
     length_used++; 
      cout<<"jab size bara ho length_used"<<endl; 
     } 
     else if(size==length_used){ 

     cout<<"jab size or length_used braber ho jao 3rd condiot"<<endl;  
     resizeByOne(); 

     shiftRight(); 
     a[0]=val; 
     length_used++; 

     } 

    } 
} 




void insertAtEnd(T val){ 

    if(isEmpty()){ 
     a=new T; 
    a[0]=val; 

    length_used++; 
    size++; 
    } 

    else{ 

     if(size>length_used){ 
      a[length_used+1]=val; 
     length_used++; 

     } 
     else if(size==length_used){ 
     resizeByOne(); 
     a[length_used]=val; 
     length_used++; 
     } 

    } 
} 





void deleteFromStart(){ 

    if(isEmpty()){ 
     cout<<"Container is Empty"<<endl; 
    } 

    else{ 

     a[0]=='\0'; 

     shiftLeft(); 
     size--; 
     length_used--; 

    } 
} 



void deleteFromEnd(){ 

    if(isEmpty()){ 
     cout<<"Container is Empty"<<endl; 
    } 

    else{ 

     a[length_used]='\0'; 

     length_used--; 
     size--; 


    } 
} 


void PrintArray(){ 

    for(int i=0;i<size;i++) 
     cout<<a[i]<<endl; 


} 












////////////////////Helper functions/////////////////////// 

    bool isEmpty(){ 
    if(a=='\0') 
     return 1; 
    return 0; 
    } 


void shiftRight(){ 

    int tempIterator=size; 
    for(int i=tempIterator-1;i>=0;i--) 
     a[i]=a[i-1]; 
    } 


void shiftLeft(){ 

    int tempIterator=length_used; 
    for(int i=0;i<size;i++) 

     a[i]=a[i+1]; 
    a[0]=NULL; 


} 




void resizeByOne(){ 

     T *temp=new T[size+1]; 

     for(int i=0;i<length_used;i++) 
      temp[i]=a[i]; 

     a=NULL; 
     delete []a; 
     a=temp; 
     size++; 
    } 

}; 

#endif` 
+0

三個濫用'Item'的可疑規則,但沒有它的定義是不可能確定的。 – hmjd

+0

'void main()'不正確,'main'必須返回'int'。除此之外,這個問題包含很多不需要的代碼,並且缺少一些重要的代碼。大部分'Array'代碼是未使用的(爲什麼要粘貼它?),而'Item'類型的定義缺失。格式化也可以幫助閱讀代碼,因爲縮進在您的程序中不一致。 –

回答

1
void shiftRight() 
{ 

    int tempIterator=size; 
    for(int i=tempIterator-1;i>=0;i--) 
    a[i]=a[i-1]; 
} 

最後一次迭代導致a[0] = a[-1]這可能會導致訪問衝突,嘗試i>0作爲結束條件。在這種情況下訪問衝突非常棘手。如果內存位於a[-1](例如某些數據在那裏分配),則不會發生異常/崩潰。以不確定的方式發生異常。

BTW

a=NULL; 
delete []a; 

resizeByOne()方法。它不會導致任何異常(刪除是安全的),但肯定會導致內存泄漏。

+0

我解決了它,但後來,但謝謝親愛的問題是,當我使用「int」它分配較少的內存,這就是爲什麼程序沒有崩潰。但是謝謝 –