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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:55:19+00:00 2026-06-10T22:55:19+00:00

These are the files: //============================================================================ // Name : linearMatrix.hpp // Author : Flores, Facundo

  • 0

These are the files:

//============================================================================
// Name        : linearMatrix.hpp
// Author      : Flores, Facundo Gabriel
// Version     : 0.1
// Description : This file contains the linearMatrix class.
//===========================================================================

#ifndef LINEARMATRIX_HPP_
#define LINEARMATRIX_HPP_

class linearMatrix
{
public:
    //Constructor
    linearMatrix(const int Width, const int Heigth);

    //Destructor
    ~linearMatrix();

    //We read a matrix file
    void Read_File_Matrix(const char *Path);

    //We write a matrix file
    void Write_File_Matrix(const char *Path);

    //Generate a diagonal dominant matrix
    void Generate_Diagonal_Dominant(void);

    //Generate a random matrix
    void Generate_Random_Matrix(void);

    //Copy a float *vector
    void Copy_Vector(const float *V, const int Width, const int Heigth);

    //Show a little vector
    void Show_Little_Matrix(void);

private:
    int _Width, _Height; // Width and Height
    float* myVector;

    //Aux function for testing
    bool Test_Sizes(const int Width, const int Heigth);
};



#endif /* LINEARMATRIX_HPP_ */


//============================================================================
// Name        : linearMatrix.cpp
// Author      : Flores, Facundo Gabriel
// Version     : 0.1
// Description : This file contains the linearMatrix implementation class.
// A linear matrix is a vector that represents a matrix.
// We will read this kind of classes from files, because
// they are generally large. But linearMatrix can represent
// a 1D vector if Height is 1.
//============================================================================



#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
using std::bad_alloc;

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <new>

#include "linearMatrix.hpp"


#define FALSE 0
#define TRUE 1

//Test if W and H are correct
bool linearMatrix::Test_Sizes(const int W, const int H)
{
    if(W < 1 || W > 32500)
        return FALSE;
    if(H < 1 || H> 32500)
        return FALSE;
    return TRUE;
}

// We set the Width and Height parameters
linearMatrix::linearMatrix(const int Width, const int Height)
{
    if(Test_Sizes(Width, Height))
    {
        _Width = Width;
        _Height = Height;
        try{
            myVector = new float[_Width * _Height];
        }
        catch(bad_alloc &ex)
        {
            cerr << "Exception:" << ex.what();
            exit(1);
        }
    }
    else
    {
        cout<<"Bad sizes!"<<endl;
        exit(1);
    }

}

linearMatrix::~linearMatrix()
{
    delete [] myVector;
}

//We set a random vector using its Width and Height
void linearMatrix::Generate_Random_Matrix(void)
{
    srand(time(NULL));
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            // We are putting a number between 0 and 99
            myVector[i * _Width + _Height] = rand() % 100;
        }
}

//We set a diagonal dominant matrix
void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[i * _Width + j] = 32500;
            else
                myVector[i * _Width + j] = 0.5;
        }
}

//We copy V into myVector
void linearMatrix::Copy_Vector(const float *V, const int Width, const int Heigth)
{
    if(Width != _Width || Heigth != _Height)
    {
        cout<<"Different sizes, we cannot copy vector V"<<endl;
        exit(1);
    }
    else
    {
        for(int i = 0; i < _Width; i++)
            for(int j = 0; j < _Height; j++)
            {
                myVector[i * _Width + j] = V[i * Width + j];
            }
    }
}

//We show a little matrix. Assume H < 11 and W < 11
void linearMatrix::Show_Little_Matrix(void)
{
    cout.precision(5);
    if(_Height < 11 && _Width < 11)
    {
        for(int i = 0; i < _Width; i++)
        {
            for(int j = 0; j < _Height; j++)
            {
                cout<<myVector[i * _Width + j]<<" ";
            }
            cout << endl;
        }
    }
    else
    {
        cout << "I can show only little matrices in the console " << endl;
    }
}

void linearMatrix::Write_File_Matrix(const char *Path)
{
    FILE *pFile = fopen(Path, "wb");
    fwrite(&_Height, sizeof(int), 1, pFile);
    fwrite(&_Width, sizeof(int), 1, pFile);
    fwrite(myVector, sizeof(float), _Height * _Width, pFile);
    fclose(pFile);
}

void linearMatrix::Read_File_Matrix(const char *Path)
{
    FILE *pFile = fopen(Path, "rb");
    if(pFile == NULL){
        cout << "Cannot read :" << Path << endl;
        exit(1);
    }
    fread(&_Height, sizeof(int), 1, pFile);
    fread(&_Width, sizeof(int), 1, pFile);

    try{
        myVector = new float[_Width * _Height];
    }
    catch(bad_alloc &ex)
    {
        cout << "Exception:" << ex.what();
        exit(1);
    }
    fread(myVector, sizeof(double), _Height * _Width, pFile);
}

And I’m trying to use this class with the following main.cpp:

//============================================================================
// Name        : main.cpp
// Author      : Flores, Facundo Gabriel
// Version     : 0.1
// Description : This file is a test.
//===========================================================================
#include <iostream>
#include "linearMatrix.hpp"

int main(void)
{
    linearMatrix Matrix2(3,1);

    Matrix2.Generate_Diagonal_Dominant();
    Matrix2.Show_Little_Matrix();

    return 0;
}

So the error is:

*** glibc detected *** /.../linearMatrix/Debug/linearMatrix: free(): invalid next size (fast): 0x00000000007f1010 ***

I don’t know how to use valgrind yet. But why is the error there? The program shows me the correct output, but when the destructor is “executed” the error appears while freeing myVector. How to solve it?

  • 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-10T22:55:20+00:00Added an answer on June 10, 2026 at 10:55 pm

    Here’s one error

    void linearMatrix::Generate_Diagonal_Dominant(void)
    {
        for(int i = 0; i < _Width; i++)
            for(int j = 0; j < _Height; j++)
            {
                if(i == j) //Diagonal item
                    myVector[i * _Width + j] = 32500;
                else
                    myVector[i * _Width + j] = 0.5;
            }
    }
    

    should be

    void linearMatrix::Generate_Diagonal_Dominant(void)
    {
        for(int i = 0; i < _Width; i++)
            for(int j = 0; j < _Height; j++)
            {
                if(i == j) //Diagonal item
                    myVector[j * _Width + i] = 32500;
                else
                    myVector[j * _Width + i] = 0.5;
            }
    }
    

    In other words you have i and j the wrong way round when you calculate the offset (or maybe you should use i * _Height + j). You seem to have made the same mistake throughout.

    You have other errors, such as not following the rule of three, as chris mentioned.

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

Sidebar

Related Questions

master branch has these files and folders (simplified): C:\Local\TickZoom\Project>ls file.txt name.txt public public branch
Suppose a particular command generates few files (I dont know the name of these
I have list similar to this: m=[['qw','wew','23','C:/xyz/s.wav'],['qw','wew','23','C:/xyz/s2.wav'],['qw','wew','23','C:/xyz/s1.wav']] Now I want to these files win=wave.open(m[0][3],'rb')
I have two php files: main.class.php form.class.php in these files there are two class
I have these files in a directory: y y1 y2 y3 Running this command:
I was sent a zip file containing 40 files with the same name. I
I am using file_put_contents($file, $data); function for putting contents in file, because these files
have different files with same name, in different directories. In these files there are
I have a webforms site where users upload files, the file name etc is
I have different CSV files and these files have some raw data Computer Name,Computer

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.