Thread: Segmentation Fault with Vectors~

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Angry Segmentation Fault with Vectors~

    I'm attempting to create a Hash Table using vectors for open hashing. The issue is the program runs just fine in NetBeans IDE, however when run on the older unix based test server, it seg faults. I've ran through GDB numerous times, and here's what I've found:

    Program received signal SIGSEGV, Segmentation fault.0x001c538b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
    (gdb) where
    #0 0x001c538b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
    #1 0x08049841 in __gnu_cxx::new_allocator<std::string>::construct (this=0x804fffc, __p=0x4, __val=...)
    at /usr/include/c++/4.6/ext/new_allocator.h:108
    #2 0x080493f8 in std::vector<std::string, std::allocator<std::string> >:ush_back (this=0x804fffc,
    __x=...) at /usr/include/c++/4.6/bits/stl_vector.h:830
    #3 0x08048e74 in main () at openhash.cpp:65
    Here's the piece of the code that's acting up:

    Code:
    int main() {
    
    
    
        vector< vector<string> > hashTable; //initialize hash table, 100 buckets, using vectors to chain strings.
        vector<string> buffer;
        hashTable.resize(100);
        for (int j = 0; j < 100; j++)
            hashTable[j].resize(15);
    
    
        string buff;
        int key;
        int collisions = 0;
        
        while (cin >> buff) {
            buffer.push_back(buff);
        }
        int placeholder =0;
        while(placeholder < buffer.size())
        {
            key = hashKey(buff);
            
            if(hashTable[key].empty())
            {
                (hashTable[key]).push_back(buffer[placeholder]);
                
                placeholder++;
            }
            else {
                
                (hashTable[key]).push_back(buffer[placeholder]);                   //line 65 in full program//
                
                collisions++;
                placeholder++;
            }
            
            
        }
    If anyone can offer some insight into the problem, I would be forever grateful!

    Thank You very much.

    ~ALex~
    Last edited by alexhollis; 02-27-2015 at 12:52 AM. Reason: readability of code

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    By looking at your stack trace, the problem is in your hash function which you have not provided - we have no idea how your hash function works.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is the value of key at the time of the crash?

    Jim

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Maybe it's because of placeholder. Try using std::vector::at to check bounds?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually the value of placeholder is being checked in the while statement, however key is never checked.

    Jim

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Maybe it's because of placeholder. Try using std::vector::at to check bounds?
    I second this. Use .at() to check that you don't do out-of-bounds. If you still can't find it, provide your hash function and a set of inputs that reproduces the issues.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by jimblumberg View Post
    Actually the value of placeholder is being checked in the while statement, however key is never checked.

    Jim
    Truth.

    You're right, the while-loop will automatically break. The sentiment is the same though. It's most likely "key".

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Where's the code for the hashKey() function? How does it calculate its return value? If it's actually generating hashtable keys, they would tend to be random and evenly distributed through the range of the type that contains the hash value, unless you are constraining it to a narrower range. Is your vector big enough to handle the range of values that hashKey() produces?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault ?
    By jigamuffin in forum C Programming
    Replies: 3
    Last Post: 05-01-2012, 02:31 AM
  3. segmentation fault
    By kirbyiwaki in forum C Programming
    Replies: 2
    Last Post: 11-06-2008, 10:13 AM
  4. segmentation fault
    By incognito54 in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:04 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread