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

  • Home
  • SEARCH
  • 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 7443487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:20:12+00:00 2026-05-29T11:20:12+00:00

In a multi-dimensional array where the second dimension will be of known sizes (albeit

  • 0

In a multi-dimensional array where the second dimension will be of known sizes (albeit different per each first dimension), would it be faster performance-wise for the actual building of these arrays to hardcode:

int* number[1000];

int secondDim[1];
number[0] = secondDim;

int secondDimTwo[2];
number[1] = secondDim;

etc. etc. 1,000 times (I know, I know)

or dynamically allocate each second dimension:

for(int i = 0; i < 1000; i++)
    number[i] = new int[i+1];

Just trying to wrap my head around a concept here.

  • 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-29T11:20:13+00:00Added an answer on May 29, 2026 at 11:20 am

    As a general rule you can assume, that stack allocations will be faster. Just remember that stack has limited capacity, and overuse of large stack-allocated arrays might lead to, wait for it… stack overflow!

    Anyways, your question is valid, but the solutions I’ve seen so far are extremely limited. The thing is, you can easily create utility type, that will act as triangular matrix, and then it’s up to specific use case whether you’ll store it on the stack or heap. Observe:

    namespace meta
    {
        template <size_t N>
        struct sum
        {
            static const int value = (N + 1) * N / 2;
        };
    }
    
    template <size_t Size>
    struct MultiArray
    {
        // actual buffer
        int numbers[ meta::sum<Size>::value ];
    
        // run-time indexing
        int* getArray(size_t dimensions)
        {
            // get sum of (dimensions-1)
            size_t index = (dimensions * (dimensions-1)) >> 1;
            return &numbers[index];
        }
    
        // compile-time indexing
        template <size_t dimensions>
        int* getArray()
        {
            size_t index = meta::sum<dimensions - 1>::value ;
            return &numbers[ index ];
        }
    
        int* operator[](size_t index)
        {
            return getArray(index);
        }
    };
    

    Now it’s up to you where to store it.

    MultiArray<1000> storedOnStack;
    MultiArray<1000>* storedOnHeap = new MultiArray<1000>();
    

    You have to accessors to get to the inner arrays:

    int* runTimeResolvedArray = storedOnStack.getArray(10);
    int* compileTimeResolvedArray = storedOnStack.getArray<10>();
    int* runTimeResolvedArray2 = storedOnStack[10];
    storedOnStack[10][0] = 666;
    

    Hope this helps!

    EDIT:
    I also must say that I don’t like the term “stack allocation”. It’s misleading. Stack allocation is essentially just an increase in stack pointer register. So, if you “allocate” 100 bytes on stack the pointer will be increased by 100 bytes. But if you allocate 100 bytes on heap, then it gets complicated – current allocator has to find suitable empty memory space, update alloc map and so on, so on.

    If it’s one time allocation – go ahead and do it on the heap, the overhead of dynamic allocation won’t be noticable. But if do it many times per second, then opt for stack allocation. Also, stack arrays can be potentially faster to access, because stack contents are more likely to be in cache. But obviously really huge arrays won’t fit cache. So, the anwser is: profile.

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

Sidebar

Related Questions

I have a multi-dimensional array and what i want is in each second degree
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I'm using a multi dimensional array in php, I would like to know how
Given a multi-dimensional array that you don't necessarily know the structure of; how would
can someone help me please? I would like to order this multi dimensional array
I am playing around with multidimensional array of unequal second dimension size. Lets assume
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have a multi dimensional array. The only actual values (other than other arrays)
If I have a multi dimensional array in PHP like so... [0] => Array
I have an multi-dimensional array that I want to send to a PHP script

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.