세그 먼테이션이 떨어집니다.

nayana의 이미지

알고리즘을 공부하다가 예제를 활용하고 있는데...
세그 먼테이션이 떨어 집니다.

#include <cstdio>
#include <cstring>

#include "HashTable.h"

#define MAXBUKETSIZE  1024

void Insert( char* ID, int key );
void Search( HashEntry< char*, int >* entry );
void Delete( HashEntry< char*, int >* entry, HashTable< char*, int > table, char* key );
unsigned long int Hash( char* k );

int  main( void )
{
	HashTable< char*, int >  table( MAXBUKETSIZE, Hash );
    HashEntry< char*, int >* entry;
	HashEntry< char*, int >* entry1;
	
	table.Insert( "test1", 11 );
	Insert( "test1", 11 );
	table.Insert( "test2", 21 );
	Insert( "test2", 21 );
	
	entry = table.Find( "test1" );
	Search( entry );
	entry1 = table.Find( "test2" );
	Search( entry1 );

	Delete( entry, table, "test1" );
	Delete( entry1, table, "test2" ); // 이부분에 세그먼테이션

	return 0;
}

//------------------------------------------------------------------------------------------------------------------
unsigned long int Hash( char* k )
//------------------------------------------------------------------------------------------------------------------
{
	unsigned long int iSum = 0;
	int iLen               = strlen( k );

	for ( int iN = 0; iN < iLen; ++iN )
		iSum += ( ( iN + 1 ) * k[ iN ] );

    return iSum;
}	

//------------------------------------------------------------------------------------------------------------------
void Insert( char* ID, int key )
//------------------------------------------------------------------------------------------------------------------
{
	printf( "Inserted     : ( %s, %d )\n", ID, key );
}

//------------------------------------------------------------------------------------------------------------------
void Search( HashEntry< char*, int >* entry )
//------------------------------------------------------------------------------------------------------------------
{
	if ( entry == 0 )	printf( "key not found\n" );
	else				printf( "Data Found   : ( %s, %d )\n", entry->m_key, entry->m_data );
}

//------------------------------------------------------------------------------------------------------------------
void Delete( HashEntry< char*, int >* entry, HashTable< char*, int > table, char* key )
//------------------------------------------------------------------------------------------------------------------
{
	if ( entry == 0 )	printf( "key not found\n" );
	else				printf( "Data Removed : ( %s, %d )\n", entry->m_key, entry->m_data );
		
	table.Remove( key );
}

Array.h

#ifndef ARRAY_H
#define ARRAY_H

#include <stdio.h>

template<class Datatype>
class Array
{
public:
//------------------------------------------------------------------------------------------------------------------
    Array( int p_size )
//------------------------------------------------------------------------------------------------------------------
    {
        m_array = new Datatype[p_size];
		m_size  = p_size;
    }

//------------------------------------------------------------------------------------------------------------------
    ~Array()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_array != 0 )	delete[] m_array;

        m_array = 0;
    }

//------------------------------------------------------------------------------------------------------------------
	void Resize( int p_size )
//------------------------------------------------------------------------------------------------------------------
    {
        Datatype* newarray = new Datatype[p_size];

        if( newarray == 0	) return;

        int min;
        if( p_size < m_size )	min = p_size;
        else					min = m_size;

        int index;
        for( index = 0; index < min; index++ )
            newarray[index] = m_array[index];

        m_size = p_size;

        if( m_array != 0 )	delete[] m_array;

		m_array = newarray;
    }

//------------------------------------------------------------------------------------------------------------------
    Datatype& operator[] ( int p_index )
//------------------------------------------------------------------------------------------------------------------
    {
        return m_array[p_index];
    }

//------------------------------------------------------------------------------------------------------------------
    void Insert( Datatype p_item, int p_index )
//------------------------------------------------------------------------------------------------------------------
    {
        int index;
        for( index = m_size - 1; index > p_index; index-- )
            m_array[index] = m_array[index - 1];

        m_array[p_index] = p_item;
    }

//------------------------------------------------------------------------------------------------------------------
    void Remove( int p_index )
//------------------------------------------------------------------------------------------------------------------
    {
        int index;

        for( index = p_index + 1; index < m_size; index++ )
            m_array[index - 1] = m_array[index];
    }

//------------------------------------------------------------------------------------------------------------------
    int Size()
//------------------------------------------------------------------------------------------------------------------
    {
        return m_size;
    }

//------------------------------------------------------------------------------------------------------------------
    operator Datatype* ()
//------------------------------------------------------------------------------------------------------------------
    {
        return m_array;
    }

//------------------------------------------------------------------------------------------------------------------
    bool WriteFile( const char* p_filename )
//------------------------------------------------------------------------------------------------------------------
    {
        FILE* outfile = 0;
        int   written = 0;

        outfile = fopen( p_filename, "wb" );

        if( outfile == 0 )	return false;

        written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
        fclose( outfile );

        if( written != m_size )	return false;

        return true;
    }
    
//------------------------------------------------------------------------------------------------------------------
    bool ReadFile( const char* p_filename )
//------------------------------------------------------------------------------------------------------------------
    {
        FILE* infile = 0;
        int   read   = 0;

        infile = fopen( p_filename, "rb" );

		if( infile == 0 )	return false;

        read = fread( m_array, sizeof( Datatype ), m_size, infile );
        fclose( infile );

        if( read != m_size )   return false;

        return true;
    }

    Datatype* m_array;
    int       m_size;
};

#endif

DLinkedList.h

#ifndef DLINKEDLIST_H
#define DLINKEDLIST_H


template<class Datatype> class DListNode;
template<class Datatype> class DLinkedList;
template<class Datatype> class DListIterator;

//==================================================================================================================
template<class Datatype>
class DListNode
{
public:
	DListNode()  { };
	~DListNode() { };

//------------------------------------------------------------------------------------------------------------------
    void Delink()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_previous != 0 )
            m_previous->m_next = m_next;

        if( m_next != 0 )
            m_next->m_previous = m_previous;
    }

//------------------------------------------------------------------------------------------------------------------
    void InsertAfter( Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {

        DListNode<Datatype>* newnode = new DListNode<Datatype>;
        newnode->m_data              = p_data;
		newnode->m_next              = m_next;
        newnode->m_previous          = this;

		if( m_next != 0 )
            m_next->m_previous = newnode;

		m_next = newnode;
    }


//------------------------------------------------------------------------------------------------------------------
    void InsertBefore( Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        
        DListNode<Datatype>* newnode = new DListNode<Datatype>;
        newnode->m_data = p_data;

        
        newnode->m_next     = this;
        newnode->m_previous = m_previous;

        
        if( m_previous != 0 )
            m_previous->m_next = newnode;

        
        m_previous = newnode;
    }

	Datatype			 m_data;
    DListNode<Datatype>* m_next;
    DListNode<Datatype>* m_previous;


};

//==================================================================================================================
template<class Datatype>
class DLinkedList
{
public:

//------------------------------------------------------------------------------------------------------------------
    DLinkedList()
//------------------------------------------------------------------------------------------------------------------
    {
        m_head = 0;
        m_tail = 0;
        m_count = 0;
    }

//------------------------------------------------------------------------------------------------------------------
    ~DLinkedList()
//------------------------------------------------------------------------------------------------------------------
    {
        DListNode<Datatype>* node = m_head;
        DListNode<Datatype>* next;

        while( node != 0 )
        {
        
            next = node->m_next;
			
			delete node;
			node = next;
        }
    }

//------------------------------------------------------------------------------------------------------------------
    void Append( Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_head == 0 )
        {
            m_head = m_tail    = new DListNode<Datatype>;
            m_head->m_data     = p_data;
            m_head->m_next     = 0;
            m_head->m_previous = 0;
        }
        else
        {
		    m_tail->InsertAfter( p_data );
            m_tail = m_tail->m_next;
        }
        m_count++;
    }

//------------------------------------------------------------------------------------------------------------------
    void Prepend( Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_head == 0 )
        {
            m_head = m_tail    = new DListNode<Datatype>;
            m_head->m_data     = p_data;
            m_head->m_next     = 0;
            m_head->m_previous = 0;
        }
        else
        {
            
            m_head->InsertBefore( p_data );
            m_head = m_head->m_previous;
        }
        m_count++;
    }

//------------------------------------------------------------------------------------------------------------------
    void RemoveHead()
//------------------------------------------------------------------------------------------------------------------
    {
        DListNode<Datatype>* node = 0;

        if( m_head != 0 )
        {

            node = m_head->m_next;

			delete m_head;
            m_head = node;

			if( m_head == 0 )	m_tail = 0;
            else				m_head->m_previous = 0;

            m_count--;
        }
    }

//------------------------------------------------------------------------------------------------------------------
    void RemoveTail()
//------------------------------------------------------------------------------------------------------------------
    {
        DListNode<Datatype>* node = 0;

        if( m_tail != 0 )
        {
            
            node = m_tail->m_previous;

            delete m_tail;
            m_tail = node;

            if( m_tail == 0 )	m_head = 0;
            else				m_tail->m_next = 0;

            m_count--;
        }
    }

//------------------------------------------------------------------------------------------------------------------
    void InsertAfter( DListIterator<Datatype>& p_iterator, Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        if( p_iterator.m_node != 0 )
        {
            
            p_iterator.m_node->InsertAfter( p_data );

            if( p_iterator.m_node == m_tail )	m_tail = m_tail->m_next;

			m_count++;
        }
        else	Append( p_data );
        
    }

//------------------------------------------------------------------------------------------------------------------
    void InsertBefore( DListIterator<Datatype>& p_iterator, Datatype p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        if( p_iterator.m_node != 0 )
        {
            
            p_iterator.m_node->InsertBefore( p_data );

             if( p_iterator.m_node == m_head )	m_head = m_head->m_previous;

			 m_count++;
        }
        else	Prepend( p_data );
        
    }

//------------------------------------------------------------------------------------------------------------------
    void Remove( DListIterator<Datatype>& p_iterator )
//------------------------------------------------------------------------------------------------------------------
    {
        DListNode<Datatype>* node;

        if( p_iterator.m_node == 0 )	return;

		node = p_iterator.m_node;

        if( node == m_head )      m_head = m_head->m_next;
        else if( node == m_tail ) m_tail = m_tail->m_previous;
        
		p_iterator.Forth();

        node->Delink();
        delete node;

        if( m_head == 0 )    m_tail = 0;
        
        m_count--;
    }

//------------------------------------------------------------------------------------------------------------------
    DListIterator<Datatype> GetIterator()
//------------------------------------------------------------------------------------------------------------------
    {
        return DListIterator<Datatype>( this, m_head );
    }

//------------------------------------------------------------------------------------------------------------------
    int Size()
//------------------------------------------------------------------------------------------------------------------
    {
        return m_count;
    }

//------------------------------------------------------------------------------------------------------------------
    bool SaveToDisk( char* p_filename )
//------------------------------------------------------------------------------------------------------------------
    {
        FILE* outfile = 0;
        DListNode<Datatype>* itr = m_head;

        outfile = fopen( p_filename, "wb" );

        if( outfile == 0 )       return false;

        fwrite( &m_count, sizeof( int ), 1, outfile );

        while( itr != 0 )
        {
            fwrite( &(itr->m_data), sizeof( Datatype ), 1, outfile );
            itr = itr->m_next;
        }

        fclose( outfile );

		return true;
    }


//------------------------------------------------------------------------------------------------------------------
    bool ReadFromDisk( char* p_filename )
//------------------------------------------------------------------------------------------------------------------
    {
        FILE* infile = 0;
        Datatype buffer;
        int count = 0;

        
        infile = fopen( p_filename, "rb" );

         if( infile == 0 )  return false;

        fread( &count, sizeof( int ), 1, infile );

        while( count != 0 )
        {
            fread( &buffer, sizeof( Datatype ), 1, infile );
            Append( buffer );
            count--;
        }

        fclose( infile );

        return true;
    }


    DListNode<Datatype>* m_head;
    DListNode<Datatype>* m_tail;
    int					 m_count;
};

//==================================================================================================================
template<class Datatype>
class DListIterator
{
public:
//------------------------------------------------------------------------------------------------------------------
    DListIterator( DLinkedList<Datatype>* p_list = 0, DListNode<Datatype>* p_node = 0 )
//------------------------------------------------------------------------------------------------------------------
    {
        m_list = p_list;
        m_node = p_node;
    }

//------------------------------------------------------------------------------------------------------------------
    void Start()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_list != 0 ) m_node = m_list->m_head;
    }

//------------------------------------------------------------------------------------------------------------------
    void End()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_list != 0 ) m_node = m_list->m_tail;
    }

//------------------------------------------------------------------------------------------------------------------
    void Forth()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_node != 0 ) m_node = m_node->m_next;
    }

//------------------------------------------------------------------------------------------------------------------
    void Back()
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_node != 0 ) m_node = m_node->m_previous;
    }

//------------------------------------------------------------------------------------------------------------------
    Datatype& Item()
//------------------------------------------------------------------------------------------------------------------
    {
        return m_node->m_data;
    }

//------------------------------------------------------------------------------------------------------------------
    bool Valid()
//------------------------------------------------------------------------------------------------------------------
    {
        return (m_node != 0);
    }

//------------------------------------------------------------------------------------------------------------------
    bool operator==( DListIterator<Datatype>& p_rhs )
//------------------------------------------------------------------------------------------------------------------
    {
        if( m_node == p_rhs.m_node && m_list == p_rhs.m_list )    return true;
        
        return false;
    }

    DListNode<Datatype>*   m_node;
    DLinkedList<Datatype>* m_list;
};

#endif

HashTable.h

#ifndef HASHTABLE_H
#define HASHTABLE_H

#include "DLinkedList.h"
#include "Array.h"

//==================================================================================================================
template< class KeyType, class DataType >
class HashEntry
{
public:
    KeyType  m_key;
    DataType m_data;
};
//==================================================================================================================

//==================================================================================================================
template< class KeyType, class DataType >
class HashTable
{
public:
	typedef HashEntry<KeyType, DataType> Entry;

//------------------------------------------------------------------------------------------------------------------
    HashTable( int p_size, unsigned long int (*p_hash)(KeyType) )
        : m_table( p_size )
//------------------------------------------------------------------------------------------------------------------
    {
        m_size  = p_size;
        m_hash  = p_hash;
        m_count = 0;
    }
//------------------------------------------------------------------------------------------------------------------
    void Insert( KeyType p_key, DataType p_data )
//------------------------------------------------------------------------------------------------------------------
    {
        Entry entry;
        entry.m_data = p_data;
        entry.m_key  = p_key;

        int index = m_hash( p_key ) % m_size;

        m_table[index].Append( entry );
        m_count++;
    }

//------------------------------------------------------------------------------------------------------------------
    Entry* Find( KeyType p_key )
//------------------------------------------------------------------------------------------------------------------
    {
        int index = m_hash( p_key ) % m_size;

        DListIterator<Entry> itr = m_table[index].GetIterator();

        while( itr.Valid() )
        {
            if( !strncmp( itr.Item().m_key, p_key, strlen( p_key ) ) )	return &(itr.Item());
           
			itr.Forth();
        }

        return 0;
    }

//------------------------------------------------------------------------------------------------------------------
    bool Remove( KeyType p_key )
//------------------------------------------------------------------------------------------------------------------
    {
        int index = m_hash( p_key ) % m_size;
		DListIterator<Entry> itr = m_table[index].GetIterator();

		while( itr.Valid() )
        {
			if( !strncmp( itr.Item().m_key, p_key, strlen( p_key ) ) )
            {
                m_table[index].Remove( itr );
                m_count--;
                return true;
            }
            itr.Forth();
        }

        return false;
    }

//------------------------------------------------------------------------------------------------------------------
    int Count()
//------------------------------------------------------------------------------------------------------------------
    {
        return m_count;
    }

    int							  m_size;
	int						      m_count;
	Array< DLinkedList< Entry > > m_table;

    unsigned long int (*m_hash)(KeyType);
};

#endif
nayana의 이미지

확인해보니 복사 생성자 문제 였더군요^^
일단
첫번째 방법

Quote:
//------------------------------------------------------------------------------------------------------------------
void Delete( HashEntry< char*, int >* entry, HashTable< char*, int > table, char* key )
//------------------------------------------------------------------------------------------------------------------
{
if ( entry == 0 ) printf( "key not found\n" );
else printf( "Data Removed : ( %s, %d )\n", entry->m_key, entry->m_data );

table.Remove( key );
}

void Delete( HashEntry< char*, int >* entry, HashTable< char*, int >& table, char* key );

두번째 방법

Quote:
Delete( entry, table, "test1" );
Delete( entry1, table, "test2" );

Delete( entry, &table, "test1" );
Delete( entry1, &table, "test2" );
void Delete( HashEntry< char*, int >* entry, HashTable< char*, int >* table, char* key );
{
if ( entry == 0 ) printf( "key not found\n" );
else printf( "Data Removed : ( %s, %d )\n", entry->m_key, entry->m_data );

table->Remove( key );
}

세번째 방법
복사 생성자를 하나 만든것 입니다.
HashTable( const HashTable& hashtable )
: m_table( hashtable.m_size )
{
m_size = hashtable.m_size;
m_hash = hashtable.m_hash;
m_count = 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.