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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:29:26+00:00 2026-06-01T15:29:26+00:00

I have the following class: template <typename T> class matrix { private: int _n;

  • 0

I have the following class:

template <typename T>
class matrix
{
private:
    int _n;
    T* array;
public:
    matrix(): _n(0) 
    {
        array = new T[_n * _n];
    }
    matrix(int n): _n(n)
    {
        if( n < 0 )
            throw "Invalid array size!";
        array = new T[_n * _n];
    }
    ~matrix()
    {
        delete[] array;
    }
    void Set(const int x, const int y,const T val)
    {
        if( ( x<0 || x>_n ) && ( y<0 || y>_n) )
            throw "Invalid index";
        array[x*_n + y] = val;
    }
    T& Get(const int x, const int y)
    {
        if( ( x<0 || x>_n ) && ( y<0 || y>_n) )
            throw "Invalid index";
        return array[x*_n + y];
    }
};

and using it this way:

matrix<int> k(5);
k.Set(5,5,6);
cout<<k.Get(5,5);

The problem is I get a heap corruption error when calling Set.
What am I doing wrong? (my guess is it’s the way I access they array elements)

  • 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-01T15:29:27+00:00Added an answer on June 1, 2026 at 3:29 pm

    A 5 element array can be accessed at the indexes 0-4. You’re passing 5 for x and y, which results in an invalid index when accessing array.

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

Sidebar

Related Questions

I have the following structure: template <class T> struct Array{ int lenght; T *
I have the following pattern: template <int a, int b> class MyClass { public:
I have the following small code: template <typename T> class V { public: T
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have the following class: template <typename T> class Fixed2DContainer { T* ptr; public:
Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following template method declared in my interface: class IObjectFactory { public:
I have the following function as the constructor for a class: template<typename T> void

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.