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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:55:57+00:00 2026-05-25T00:55:57+00:00

I have a matrix declared like int **matrix , and I know that the

  • 0

I have a matrix declared like int **matrix, and I know that the proper way to pass it to a function to allocate memory should be like this:

void AllocMat(int ***mat, int size);

But now I need to delete these memory in another function and am not sure about what to pass:

void DeallocMat(int **mat, int size);

or

void DeallocMat(int ***mat, int size);

I think the second one should be right, but neither way gives me segmentation fault as I tried.

  • 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-25T00:55:58+00:00Added an answer on May 25, 2026 at 12:55 am

    The question is tagged C++, and yet the answers only use the C subset…

    Well, first of all, I would recommend against the whole thing. Create a class that encapsulates your matrix and allocate it in a single block, offer operator()(int,int) to gain access to the elements…

    But back to the problem. In C++ you should use references rather than pointers to allow the function to change the argument, so your original allocate signature should be:

    void AllocMat(int **&mat, int size);
    

    And call it like:

    int **matrix = 0;
    AllocMat( matrix, 5 );
    

    Or better, just return the pointer:

    int **AllocMat( int size );
    int **matrix = AllocMat( 5 );
    

    For the deallocation function, since you don’t need to modify the outer pointer, you can just use:

    void DeallocMat( int**mat, int size ); // size might be required to release the 
                                           // internal pointers
    

    Now, for a sketch of the C++ solution:

    template <typename T>                   // no need to limit this to int
    class square_matrix {
       const unsigned size;
       T * data;
    public:
       square_matrix( unsigned size ) : size(size), data( new T[size*size]() ) {}
       square_matrix( matrix const & m ) : size( m.size ), data( new T[m.size*m.size] ) {
          std::copy( m.data, m.data+size*size, data );
       }
       ~matrix() {
          delete [] data;
       }
       T const & operator()( unsigned x, unsigned y ) const {
          // optional range check and throw exception
          return data[ x + y*size ];
       }
       void set( unsigned x, unsigned y, T const & value ) {
          // optional range check and throw exception
          data[ x + y*size ] = value;
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix in R that represents the joint probability mass function (pmf)
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix in SQL reporting and I would like it to print
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
In OpenGL, one often writes code like this: glPushMatrix(); // modify the current matrix
I have an abstract class A that define abstract methods. This means that, for
Is there a way to reset variables declared as static within a function? The
I have 2 matrix structs means equal data but have different form like these:
I have a matrix class that takes the row and column reference type that
I have a class to parse a matrix that keeps the result in an

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.