Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:32:27+00:00 2026-06-14T15:32:27+00:00

I use malloc or new in class to get variable, and then i get

  • 0

I use malloc or new in class to get variable, and then i get a SIGABRT, I test the malloc and new in other cpp files, it works well. Can you tell me the reason 😛
error occurs in two lines:(in function Trie::Insert(char*))

int* pN = new int;

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

others are correct

all code:

#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(  ){}

trie.cpp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:32:28+00:00Added an answer on June 14, 2026 at 3:32 pm

    You get that error because of the stack/heap is corrupt. In the constructor, there’s an error in the for loop:

    `Trie::Trie(){
    …

    for( int i=0; i<CHARSIZE+1; i++ ){ ***// should not +1, just i < CHARSIZE*** 
    
        this->pRoot->child[ i ] = NULL; 
    
    }`
    

    When the heap is corrupted, in debug version, an exception will occur at next memory allocation because of heap verification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the use of malloc and free when we have new and delete
When shall i use malloc instead of normal array definition in C? I can't
If we can use pointers and malloc to create and use arrays, why does
I recently learnt how can we use multiple source files with header files to
In C++, it's standard to always use new over malloc() . However, in this
Possible Duplicate: C++: why is new needed? Why cant I use malloc to allocate
I am trying to use a malloc of short, something like typedef union _SOME_STRUCT_
I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *
I've never used malloc to store more than values but I have to use
I am a big fan of the _malloca but I can't use it with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.