2013-03-19 73 views
0

我一直在研究散列表。我不斷收到分段錯誤。我一直在試圖調試它,但它總是指向addOrder有分段錯誤。即使在更改代碼時也會出現分段錯誤

我哪裏錯了?還有什麼其他方式可以實現我的代碼來檢查任何角落情況?

struct order 
{ 
    int id; 
    char side; 
    int quantity; 
    double price; 
}; 

struct onode 
{ 
    struct order* data; 
    struct onode* next; 
    struct onode* prev; 
}; 

/* 
* Create a new instance of struct hashStorage and return it. It sets the size of the 
* table to be of length "size" which will be the number of the entries in the hash. It takes also an 
* argument to specify the format of the order printed to an output stream. If myHash parameter 
* is NULL then this means that the hash should be a linked list. When myHash is NULL the size 
* parameter should be ignored. 
*/ 

struct hashStorage* createHash(int size, int (*myHash)(int),void(*printOrder)(struct order *, FILE *)) 
{ 
    struct hashStorage* hashList = (struct hashStorage*) malloc(sizeof(struct hashStorage)); 

    if(myHash == NULL) 
    { 
     hashList->size = 1; 
     hashList->table = (struct onode**) calloc(1, sizeof(struct onode*)); 
    } 

    if(myHash != NULL) 
    { 
     hashList->table = (struct onode**) calloc(size, sizeof(struct onode*)); 
     hashList->size = size; 
    } 
    hashList->funcHash = myHash; 
    hashList->printItem = printOrder;   
    return hashList; 
} 

/* 
* Add an order to the hash structure. Remember that you should copy the data before 
* adding it to the hash as data can be modified (hint: look at newNode in list). 
* It returns the new onode.    
*/ 
struct onode* addOrder(struct hashStorage* hash, struct order* data) 
{ 
    int addIndex; 
    struct onode* dataList = newNode(data); 
    addIndex = hash -> funcHash(getOrderId(data)); 
    pushNode(&(hash->table[addIndex]), dataList); 
    return dataList;        
} 
+3

你知道什麼特定的線路導致分段故障嗎? – nairdaen 2013-03-19 05:27:12

+1

fyi你錯過了/在第一個評論 – 2013-03-19 05:28:29

+0

沒有它只是說addOrder argv有垃圾值 – Maddy 2013-03-19 05:28:59

回答

0

我建議你看一看GHashTable代碼glib庫,它有一個很好的註釋代碼,您將能夠理解。

希望它有幫助!