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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:06:19+00:00 2026-05-24T10:06:19+00:00

I need to create a large two dimensional array of objects. I’ve read some

  • 0

I need to create a large two dimensional array of objects. I’ve read some related questions on this site and others regarding multi_array, matrix, vector, etc, but haven’t been able to put it together. If you recommend using one of those, please go ahead and translate the code below.

Some considerations:

  • The array is somewhat large (1300 x 1372).
  • I might be working with more than one of these at a time.
  • I’ll have to pass it to a function at some point.
  • Speed is a large factor.

The two approaches that I thought of were:

Pixel pixelArray[1300][1372];
for(int i=0; i<1300; i++) {
    for(int j=0; j<1372; j++) {
        pixelArray[i][j].setOn(true);
        ...
    }
}

and

Pixel* pixelArray[1300][1372];
for(int i=0; i<1300; i++) {
    for(int j=0; j<1372; j++) {
        pixelArray[i][j] = new Pixel();
        pixelArray[i][j]->setOn(true);
        ...
    }
}

What’s the right approach/syntax here?

Edit:

Several answers have assumed Pixel is small – I left out details about Pixel for convenience, but it’s not small/trivial. It has ~20 data members and ~16 member functions.

  • 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-24T10:06:20+00:00Added an answer on May 24, 2026 at 10:06 am

    Your first approach allocates everything on stack, which is otherwise fine, but leads to stack overflow when you try to allocate too much stack. The limit is usually around 8 megabytes on modern OSes, so that allocating arrays of 1300 * 1372 elements on stack is not an option.

    Your second approach allocates 1300 * 1372 elements on heap, which is a tremendous load for the allocator, which holds multiple linked lists to chunks of allocted and free memory. Also a bad idea, especially since Pixel seems to be rather small.

    What I would do is this:

    Pixel* pixelArray = new Pixel[1300 * 1372];
    for(int i=0; i<1300; i++) {
        for(int j=0; j<1372; j++) {
            pixelArray[i * 1372 + j].setOn(true);
            ...
        }
    }
    

    This way you allocate one large chunk of memory on heap. Stack is happy and so is the heap allocator.

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

Sidebar

Related Questions

I need to create a demo version of an existing large application consisting of
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a 2D int array of size 800x800. But doing so
I need to create a large table of contents for an HTML book but
i have a large user Database (13k+), and for some reason i need to
I have a very large comma-seperated text-file. I need to display this in a
I have some image processing Java code in Android that acts upon two large
I have a large NSDictionary that I need to loop through and create separate
I need to compare two lists where each list contains about 60,000 objects. what
I need to create a historical timeline starting from 1600's to the present day.

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.