0

クラスで malloc または new を使用して変数を取得し、SIGABRT を取得します。malloc と new を他の cpp ファイルでテストすると、うまく機能します。理由を教えてください:Pエラーが2行で発生します:(in function Trie::Insert(char*))

int* pN = new int;

PNODE node = (PNODE)malloc(sizeof(struct NODE));

他は正しい

すべてのコード:

#define CHARSIZE 26
#include<assert.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct NODE {
    char key;
    struct NODE* child[ CHARSIZE ];
}* PNODE,*TRIE;

class Trie{
public:
    Trie();
    void Insert( char* sData );
    void Show( );
    void ShowTrie( PNODE root );
    void Delete( struct NODE* pNode );
    struct NODE* Search( char* sData );
    void DeleteTrie();
    ~Trie();
private:
    PNODE pRoot;
    static char colls[];
};
char Trie::colls[] = "abcdefghijklmnopqrstuvwxyz ";
Trie::Trie(){
    //root create
    this->pRoot = NULL;
    this->pRoot = (PNODE)malloc(sizeof(struct NODE));
    this->pRoot->key = ' ';
    for( int i=0; i<CHARSIZE+1; i++ ){
        this->pRoot->child[ i ] = NULL;
    }
}
void Trie::Insert( char* sData ){
    //stick
    if( sData==NULL || *sData == '\0' ){
        return;
    }
    PNODE p = this->pRoot;

    char* pData = sData;
    //same error sigabrt ginal
    int* pN = new int;
    //still error
    //PNODE node = (PNODE)malloc(sizeof(struct NODE)); 
    while( *pData!='\0' ){
        //如果对应位置的指针为空
        if( p->child[ *pData-'a' ]==NULL ){
            //make new Node
            PNODE node = (PNODE)malloc(sizeof(struct NODE));

            node->key = *pData;
            int i = 0;
            while( i < CHARSIZE ){
                node->child[i] = NULL;
                i++;
            }
            p->child[*pData-'a'] = node;
        }

        p = p->child[ *pData-'a' ];
        pData++;
    }
}
void Trie::Show( ){
    ShowTrie( this->pRoot );
}
void Trie::ShowTrie( PNODE root ){
    if( root==NULL ){
        return;
    }else{
        cout<<root<<endl;
        //cout<<root->key<<"    ";
        for( int i=0; i<CHARSIZE; i++ ){
            ShowTrie( root->child[i] );
        }
    }
}
void Trie::Delete( struct NODE* pNode ){

}
struct NODE* Search( char* sData ){ 


    return NULL; 

}
Trie::~Trie(  ){}

トライ.cpp

4

1 に答える 1