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 8862923
In Process

The Archive Base Latest Questions

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

I gave this a class Histogram and i am dynamically allocating memory for my

  • 0

I gave this a class Histogram and i am dynamically allocating memory for my class. I have issues with the destructor. The error is:

Expression:_BLOCK_TYPE_IS_VALID_(pHead->pBlockUse)

What i am doing wrong?

histogram.h

 #ifndef HISTOGRAM_H
    #define HISTOGRAM_H
    #include<iostream>
    class Histogram
    {
    private:
        int** matrix;
        int lines;  
        double* normalizedArray;
        int *values;
        void SortMatrix();      
    public:
        Histogram(int elements[], int elementsNr);
        Histogram(int** matrix, int lines);
        void Normalize();
        void PrintNormalized();
        void PrintDenormalized();
        void PrintValues();
        void PrintNormalizedArray();
        int* GetValues() const {return values;} 
        double* GetNormalizedArray() const {return normalizedArray;}
        int GetLines() const {return lines;}
        double CalculateD1(Histogram histo);
        double CalculateD2(Histogram histo);
        double CalculateIntersection(Histogram hist);
        ~Histogram(){
        delete []matrix;
        delete []normalizedArray;
        delete []values;
        }
    };
    #endif
histogram.cpp

     #include<math.h>
#include"histogram.h"
using namespace std;
Histogram::Histogram(int** m, int l)
{
    lines=l;
    normalizedArray=NULL;
    values=NULL;
    matrix=new int*[lines];
    for(int i=0;i<lines;i++)
    {
        matrix[i]=new int[2];
    }
    for(int i=0;i<lines;i++)
    {
        matrix[i][0]=m[i][0];
        matrix[i][1]=m[i][1];
    }

    SortMatrix();
    //save the values
    values=new int[lines];
    for(int i=0;i<lines;i++)
    {
        values[i]=matrix[i][0];
    }
}

Histogram::Histogram(int elements[], int elementsNr)
{
    lines=0;
    normalizedArray=NULL;
    //initialize matrix : elementrNr lines and 2 columns
    matrix=new int*[elementsNr];
    for(int i=0;i<elementsNr;i++)
    {
        matrix[i]=new int[2];
        matrix[i][0]=INT_MIN;
        matrix[i][1]=INT_MIN;
    }
    //search each element from the array in the matrix
    bool found=false;
    for(int i=0;i<elementsNr;i++)
    {
        found=false;
        for(int j=0;j<elementsNr;j++)
        {
            //the element was found in the matrix ( on the first column )
            if(matrix[j][0] == elements[i])
            {
                matrix[j][1]++;
                found=true;
                break;
            }
        }
        if(!found)
        {
            matrix[lines][0]=elements[i];
            matrix[lines][1]=1;
            lines++;
        }
    }
    SortMatrix();
    //save the values
    values=new int[lines];
    for(int i=0;i<lines;i++)
    {
        values[i]=matrix[i][0];
    }

}
void Histogram::SortMatrix()
{

    for(int i=0;i<lines;i++)
    {
        for(int j=0;j<lines-1;j++)
        {
            if(matrix[j][0]>matrix[j+1][0])
            {
                int temp = matrix[j+1][0];
                matrix[j+1][0] = matrix[j][0];
                matrix[j][0] = temp;
            }
        }
    }
}

void Histogram::PrintDenormalized()
{
    for(int i=0;i<lines;i++)
    {
        cout<<matrix[i][0]<<" : " <<matrix[i][1]<<endl;
    }

}
void Histogram::PrintNormalized()
{
    for(int i=0;i<lines;i++)
    {
        cout<<matrix[i][0]<<" : "<<normalizedArray[i]<<endl;
    }
}

void Histogram::PrintValues()
{
    for(int i=0;i<lines;i++)
    {
        cout<<values[i]<<endl;
    }
}
void Histogram::PrintNormalizedArray()
{
    for(int i=0;i<lines;i++)
    {
        cout<<normalizedArray[i]<<endl;
    }
}

void Histogram::Normalize()
{
    int N=0;
    normalizedArray=new double[lines];
    for(int i=0;i<lines;i++)
    {
        N+=matrix[i][1];
    }
    for(int i=0;i<lines;i++)
    {
        normalizedArray[i]=static_cast<double>(matrix[i][1])/N;
    }
}

double Histogram::CalculateD1(Histogram histo)
{
    //the two histograms must have the same values
    int* values2 = histo.GetValues();
    int lines2 = histo.GetLines();
    if(lines!=lines2)
    {
        return -1;
    }
    for(int i=0;i<lines;i++)
    {
        if(values[i]!=values2[i])
        {
            return -1;
        }
    }

    //if we got this far the two histograms have the same values, so we can calculate the distance
    double* normalizedArray2=histo.GetNormalizedArray();
    double dist=0.0;
    for(int i=0;i<lines;i++)
    {
        dist +=  abs(normalizedArray[i]-normalizedArray2[i]);
    }
    return dist;
}

double Histogram::CalculateD2(Histogram histo)
{
    //the two histograms must have the same values
    int* values2 = histo.GetValues();
    int lines2 = histo.GetLines();
    if(lines!=lines2)
    {
        return -1;
    }
    for(int i=0;i<lines;i++)
    {
        if(values[i]!=values2[i])
        {
            return -1;
        }
    }

    //if we got this far the two histograms have the same values, so we can calculate the distance
    double* normalizedArray2=histo.GetNormalizedArray();
    double dist=0.0;
    for(int i=0;i<lines;i++)
    {
        dist +=  pow(normalizedArray[i]-normalizedArray2[i], 2);
    }
    return sqrt(dist);
}

double Histogram::CalculateIntersection(Histogram histo)
{
    //the two histograms must have the same values
    int* values2 = histo.GetValues();
    int lines2 = histo.GetLines();
    if(lines!=lines2)
    {
        return -1;
    }
    for(int i=0;i<lines;i++)
    {
        if(values[i]!=values2[i])
        {
            return -1;
        }
    }

    //if we got this far the two histograms have the same values, so we can calculate the intersection
    double* normalizedArray2=histo.GetNormalizedArray();
    double v1=0.0;
    double v2=0.0;
    for(int i=0;i<lines;i++)
    {
        v1 += normalizedArray[i] < normalizedArray2[i] ? normalizedArray[i] : normalizedArray2[i];
        v2 += normalizedArray[i];
    }
    return v1/v2;
}
  • 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:50:52+00:00Added an answer on June 14, 2026 at 3:50 pm

    Neither constructor initializes normalizedArray. This means that the call to delete[] normalizedArray in the destructor will be operating on an unitialized pointer. To correct, initialize normalizedArray in each of the constructors to NULL. Calling delete[] (or delete) on a NULL pointer is safe.

    As Histogram has dynamically allocated members you need to either prevent copying:

    class Histogram
    {
        Histogram(const Histogram&);
        Histogram& operator=(const Histogram&);
    };
    

    or correctly implement the copy constructor and assignment operator. See What is The Rule of Three?
    Instances of Histogram will be copied if any of the following functions are being called:

    double CalculateD1(Histogram histo);         // Pass by const reference instead
    double CalculateD2(Histogram histo);         // if the functions do not modify
    double CalculateIntersection(Histogram hist) // their argument.
    

    If the copy constructor and assignment operator are not implemented for classes that have dynamically allocated memory then two instances of the class will end up pointing to the same dynamically allocated memory after a copy operation. When one of the two instances is destructed it leaves the other instance with dangling pointers (pointers to memory that is no longer valid). Any attempt to use these is undefined behaviour.


    If this is not an exercise use std::vector<>s instead. It handles dynamically allocated memory for you:

    std::vector<std::vector<int>> matrix;
    std::vector<double> normalizedArray;
    std::vector<int> values;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've this class and it gave me an error name 'ScriptManager is not declared'
Teacher in class gave this formula w = x**2 + y**2 - z**2 and
I have touched on this question here , where Christopher gave an answer to
The pure virtual destructor in base class should have a definition. Otherwise compiler will
My Professor gave us this class and told us that it won't compile. He
I have a model like this class Canvas include Mongoid::Document field :name referenced_in :hero
In Jquery Template website they gave this example.( http://api.jquery.com/jQuery.template/ ) <script> var movies =
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
I desined a button in Blend, which gave me this xaml in after the
This StackOverflow question gave me food for thought on what is a good structure

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.