堆栈溢出问题与链表的数组

时间:2012-05-26 23:58:13

标签: c++

首先,我的程序是创建一个名称哈希表,其中collison由链接处理。 所以我创建了一个Linked List数组。问题是我的数组非常大88801准确。我想这就是为什么我得到一个堆栈超过流量,但我不确定。我的代码如下。有人试图告诉我让数组成为链表的一组指针 IE放置LinkedList * hashbucket1 [88801],这删除了堆栈溢出问题但导致其他链接错误。猜猜我想弄清楚什么是最好的方式来实现一系列链接字符串列表。如果我在正确的轨道上,它的东西很小,我请指出它或告诉我废弃这个并重新开始考虑一个给定的路径。如果我需要链接或压缩我的其他文件,请告诉我,我可以。

#include <iostream>
#include "LinkedList.h"
#include <string>
#include <fstream>

using namespace std;

 unsigned long djb2(string& str) 
 { 
 unsigned long hash = 5381; 

  for(string::iterator it = str.begin();it!=str.end();it++)  
    hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */ 

 return hash; 
 }

  static unsigned long sdbm(string& str)
{
    unsigned long hash = 0;
    int c;

   for(string::iterator it = str.begin();it!=str.end();it++)
        hash = *it + (hash << 6) + (hash << 16) - hash;

    return hash;
  }


  void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize);

    int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801];
LinkedList HashList2[88801];
bool invalidcommand;
do{
    do{
        cout << "MENU" << endl;
        cout << "(I)nsert new entry" << endl;
        cout << "(S)earch" << endl;
        cout << "(D)elete Entry" << endl;
        cout << "(C)reate Hast Table" << endl;
        cout << "(L)og File" << endl;
        cout << "(Q)uit and Exit Program" << endl;

        cin >> command;

    if(command == 'I'){
        insert(HashList1, HashList2, listSize);
    }else if(command == 'S'){
  //            search(HashList1, HashList2, listSize);
    }else if(command == 'D'){

    }else if(command == 'L'){ 
    //  save(HashList1, HashList2, listSize);
    }else if(command == 'C'){
    //  load(HashList1, HashList2, listSize);
    }
}while(command != 'Q');

return 0;
    };

    void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize){

string lastName;
bool invalidID;
//Person newPerson;
do
{
    invalidID = false;
    cout << "Please enter Last Name: ";
    cin >> lastName;
    while(cin.fail())
    {
        cin.clear();
        cin.ignore();
    }
}while(invalidID);
//newPerson.setLastName(lastName);
int hashBucket1;
int hashBucket2;
hashBucket1 = djb2(lastName) % listSize;
hashBucket2 = sdbm(lastName) % listSize;
LinkedList bucket1;
if(!HashList1[hashBucket1].find(lastName)){     
    //HashList1[hashBucket1].insert(newPerson);
    HashList1[hashBucket1].insert(lastName);

}else{
    if(!bucket1.find(lastName)){

        HashList1[hashBucket1].insert(lastName);
    }else{
    cout << "List already contains an entry with the last name " << lastName << endl;
    }
}
LinkedList bucket2;
if(HashList2[hashBucket2].find(lastName)){
//  bucket2.insert(newPerson);
}else{
    if(!bucket2.find(lastName)){
        //bucket2.insert(newPerson);
    }else{
    cout << "List already contains an entry with the last name " << lastName << endl;
    }
}
   }

1 个答案:

答案 0 :(得分:2)

正如您所怀疑的那样,您的问题是您的堆栈空间不足。你不应该在堆栈上放置这么大的对象,这是一种有限且宝贵的资源。相反,您可以使用指针并在堆上动态分配它们。

有问题的代码在这里:

int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801]; // << this
LinkedList HashList2[88801]; // << and this

还要注意LinkedList* hashbucket1[88801]是一个指针数组,而不是指向数组的指针。你想要的是:

LinkedList (*HashList1)[88801]

或使用typedef使其更具可读性:

typedef LinkedList LinkedListArray[88801];
LinkedListArray* HashList1;

但到那时最好只做:

LinkedList *HashList1 = new LinkedList[88801];