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

  • SEARCH
  • Home
  • 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 6696953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:07+00:00 2026-05-26T06:24:07+00:00

My program makes a frequency map of characters (which I store in , surprise

  • 0

My program makes a frequency map of characters (which I store in , surprise surprise, a Map), I am trying to copy each element from this map into a Priority Queue so that I can have a sorted copy of these values (I plan to make further use of the Q, that’s why am not sorting the map) , but whenever I try to copy these values , the program executes fine for the first two or three iterations and fails on the fourth citing an “Invalid heap” error.

I’m not sure how to proceed from here, so I am posting the code for the classes in question.

#include "srcFile.h"
#include <string>
#include <iostream>

srcFile::srcFile(std::string s_flName)
{
    // Storing the file name
    s_fileName= s_flName;
}

srcFile::srcFile()
{
    // Default constructor (never to be used)
}

srcFile::~srcFile(void)
{
}

void srcFile::dispOverallMap ()
{
    std::map<char,int>::iterator dispIterator;
dispIterator = map_charFreqDistribution.begin();
charElement *currentChar;

std::cout<<"\n Frequency distribution map \n";
while(dispIterator != map_charFreqDistribution.end())
{
    std::cout<< "Character : " << (int)dispIterator->first << " Frequency : "<< dispIterator->second<<'\n';
    currentChar = new charElement(dispIterator->first,dispIterator->second);

    Q_freqDistribution.push(*currentChar);
    dispIterator++;

    // delete currentChar;
}

while(!Q_freqDistribution.empty())
{
    std::cout<<'\n'<<"Queue Element : " << (int)Q_freqDistribution.top().ch_elementChar << " Frequency : " << Q_freqDistribution.top().i_frequency;
    Q_freqDistribution.pop();
}
}

map_charFreqDistribution has already been populated, if I remove the line
Q_freqDistribution.push(*currentChar);
Then I can verify that the Map is indeed there.

Also , both the Q and the use charElement as the template type , its nothing except the character and its frequency, along with 2 pointers to facilitate tree generation (unused upto this point)

Adding the definition of charElement on request

#pragma once
class charElement
{
public:
    // Holds the character for the element in question
char ch_elementChar;

// Holds the number of times the character appeared in the file
int i_frequency;

// Left pointer for tree
charElement* ptr_left;
// Right pointer for tree
charElement* ptr_right;

charElement(char,int);
charElement(void);
~charElement(void);
void operator=(charElement&);
};

class compareCharElt
{
public:
bool operator()(charElement &operand1,charElement &operand2)
{
    // If the frequency of op1 < op2 then return true
    if(operand1.i_frequency < operand2.i_frequency) return true;

    // If the frequency of op1 > op2 then return false
    if(operand1.i_frequency > operand2.i_frequency)return false;

    // If the frequency of op1 == op2 then return true (that is priority is indicated to be less even though frequencies are equal)
    if(operand1.i_frequency == operand2.i_frequency)return false;
}
};

Definition of Map and Queue

// The map which holds the frequency distribution of all characters in the file
    std::map<char,int> map_charFreqDistribution;
    void dispOverallMap();

// Create Q which holds character elements
std::priority_queue<charElement,std::vector<charElement>,compareCharElt>                 Q_freqDistribution;

P.S.This may be a noob question, but Is there an easier way to post blocks of code , putting 4 spaces in front of huge code chunks doesn’t seem all that efficient to me! Are pastebin links acceptable here ?

  • 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-05-26T06:24:08+00:00Added an answer on May 26, 2026 at 6:24 am

    Your vector is reallocating and invalidating your pointers. You need to use a different data structure, or an index into the vector, instead of a raw pointer. When you insert elements into a vector, then pointers to the contents become invalid.

    while(dispIterator != map_charFreqDistribution.end())
    {
        std::cout<< "Character : " << (int)dispIterator->first << " Frequency : "<< dispIterator->second<<'\n';
        currentChar = new charElement(dispIterator->first,dispIterator->second);
    
        Q_freqDistribution.push(*currentChar);
        dispIterator++;
    
        delete currentChar;
    }
    

    Completely throws people off because it’s very traditional for people to have huge problems when using new and delete directly, but there’s actually no need for it whatsoever in this code, and everything is actually done by value.

    You have two choices. Pick a structure (e.g. std::list) that does not invalidate pointers, or, allocate all charElements on the heap directly and use something like shared_ptr that cleans up for you.

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

Sidebar

Related Questions

I have this simple program to count the word frequency: #include <iostream> #include <map>
I've written an image processing program in MATLAB which makes heavy use of the
my program makes a random name that could have a-z this code makes a
Background I am working on a phonetic converter program which converts english text into
From within my master python program, I am spawning a child program with this
Some program makes ta my root directory dummy files such as -1 -2 -3
I have a program that makes use of the following method to get a
I'm developing a program that makes some floating points calculations. Is there any way
I have to build a C# program that makes CSV files and puts long
Let's say I have a java program that makes an HTTP request on a

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.