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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:01:15+00:00 2026-05-23T15:01:15+00:00

I want to design a C++ class for multidimensional arrays. By multidimensional I mean

  • 0

I want to design a C++ class for multidimensional arrays. By multidimensional I mean 1d, 2d, 3d, etc. The class supports element by element addition and scalar multiplication operators. Assume A and B are instances of the class (of the same size & dimension). I’d like to use the objects in expressions like this:

C = A * 2 + B

The thing I’m wondered about is how to manage the memory. In the above expression, A * 2 will create a temporary object of the class which is later added to B. Anyways, the temporary object is garbage after the addition is done. I wrote the following class and it works nicely but I am pretty sure it leaks the memory. Now my questions are,

  1. How can I fix the memory problem? What is the best way to design the class?
  2. Is that possible to allocate the required memory on the stack instead of the heap?

    class XArray
    {
        int num_dim;
        int *dims;
        int *index_helper;
        int table_size;
        double *table;
    
    
    public:
        XArray(const int n, const int *d):num_dim(n), dims(d)
        {
            int size = 1;
            for (int i = 0; i < n; i++) {
                size *= d[i];
            }
            table_size = size;
            table = new double[size];
            index_helper = new int[n];
        };
    
        ~XArray()
        {
            delete[] table;
            delete[] index_helper;
        };
    
        int dim(int d)
        {
            return dims[d];
        }
    
        double& operator()(int i)
        {
            index_helper[0] = i;
            return get_helper(1, index_helper);
        }
    
        double& operator()(int i, int j)
        {
            index_helper[0] = i;
            index_helper[1] = j;
            return get_helper(2, index_helper);
        }
    
        double& operator()(int i, int j, int k)
        {
            index_helper[0] = i;
            index_helper[1] = j;
            index_helper[2] = k;
            return get_helper(3, index_helper);
        }
    
        XArray operator*(double m)
        {
            XArray *xa = new XArray(num_dim, dims);
            for (int i = 0; i < table_size; i++) {
                xa->table[i] = this->table[i] * m;
            }
    
            return *xa;
        }
    
        XArray operator+(const XArray &that)
        {
            if (num_dim != that.num_dim) {
                char *msg = new char[100];
                sprintf(msg, "XArray::dimensions do not match in + operation, expected %d, found %d", num_dim, that.num_dim);
                throw msg;
            }
    
            for (int i = 0; i < num_dim; i++) {
                if (this->dims[i] != that.dims[i]) {
                    char *msg = new char[100];
                    sprintf(msg, "XArray::dimension %d not mached, %d != %d", i, dims[i], that.dims[i]);
                    throw msg;
                }
            }       
    
            XArray *xa = new XArray(num_dim, dims);
            for (int i = 0; i < table_size; i++) {
                xa->table[i] = this->table[i] + that.table[i];
            }
    
            return *xa;
        }
    
    private:
        double& get_helper(int n, int *indices)
        {
            if (n != num_dim) {
                char *msg = new char[100];
                sprintf(msg, "XArray::dimensions do not match, expected %d, found %d", num_dim, n);
                throw msg;
            }
    
            int multiplier = 1;
            int index = 0;
    
            for (int i = 0; i < n; i++) {
                if (indices[i] < 0 || indices[i] >= dims[i]) {
                    char *msg = new char[100];
                    sprintf(msg, "XArray::index %d out of range, %d not in (0, %d)", i, indices[i], dims[i]);
                    throw msg;
                }
    
                index += indices[i] * multiplier;
                multiplier *= dims[i];
            }
    
        return table[index];
    }
    

    };

  • 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-23T15:01:16+00:00Added an answer on May 23, 2026 at 3:01 pm

    This question should probably be in Code Review rather than here. But some things on the design:

    It is usually not a good idea to manage memory manually, you should prefer using existing containers instead of dynamically allocated arrays. The fact that the constructor acquires ownership of the passed in pointer is not common, and might lead to problems with user code. Currently you are disallowing some uses, like:

    int dims[3] = { 3, 4, 5 };
    XArray array( 3, dims ); // or use sizeof(dims)/sizeof(dims[0]), or other trickery
    

    Which is simpler to user code than:

    int ndims = 5;
    int *dims = new int[ndims]
    XArray array( ndims, dims );
    

    Which also means that the user must be aware that dims ownership has been acquired and that they cannot delete the pointer.

    Internally I would use std::vector to maintain the dynamic memory, as that will give you the proper semantics for free. As it is you need to implement copy constructor and assignment operator (or disable them) as the current code will not leak, but might try to double free some of the memory. Remember the rule of the three: if you provide any of copy constructor, assignment operator or destructor, you should probably provide all three.

    Your operator* and operator+ leak memory (the XArray object itself as the internal memory is actually handled by the absence of the copy constructor, but don’t think that this is a reason not to create the copy constructor, on the contrary you must create it)

    Throwing char* is allowed, but I would recommend that you don’t for different reasons. The type of the exception should give information as to what happened, else your user code will have to parse the contents of the exception to determine what went wrong. Even if you decide to use a single type, note that the same rules that are used in overload resolution do not apply in exception handling, and in particular you might have problems with conversions not taking place. This is also another potential for a memory leak in your design, as in throwing a pointer to dynamically allocated memory you delegating the responsibility of releasing the memory to your users, if the user does not release the memory, or if the user does not really care about the exception and just catches everything ( catch (...) {} ) you will leak the errors.

    It might be better if instead of throwing you asserted, if that is possible in your program (that is a design decision: how bad is the wrong size, something that can happen even if exceptional, or something that shouldn’t happen?).

    The member index_helper does not belong to the class, it is an implementation detail that is shared only by the different operator() and the get_helper() method, you should remove it from the class, and you can allocate it statically in each operator(). It is also a bit strange that you offer separate operator() for 1, 2 and 3 dimensions, while the code is generic to handle any dimensions, and also that you are actually requiring the user to know that out of the public interface, two of the operations are forbidden, which is not a great design either.

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

Sidebar

Related Questions

I want to see if anyone has a better design for a class (class
I have a question regarding class design. I want to have a class that
If I want to work with XML, I usually design a class/bunch of classes
I want to design a tab header. The html code is, <div class=tab-header> <a
Hi I want to design a class using Groovy Sql which I will be
I want to design a class that will be like a singleton in the
Similar to Does TDD mean not thinking about class design? , I am having
I want design my startup screen with progress bar. But I don't how to
That's my question, I want design all the interface of my application within an
I want to design a web page with a banner and an iframe. I

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.