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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:30:28+00:00 2026-05-14T21:30:28+00:00

I have something I need a 2D array for, but for better cache performance,

  • 0

I have something I need a 2D array for, but for better cache performance, I’d rather have it actually be a normal array. Here’s the idea I had but I don’t know if it’s a terrible idea:

const int XWIDTH = 10, YWIDTH = 10;
int main(){
    int * tempInts = new int[XWIDTH * YWIDTH];
    int ** ints = new int*[XWIDTH];
    for(int i=0; i<XWIDTH; i++){
        ints[i] = &tempInts[i*YWIDTH];
    }
    // do things with ints
    delete[] ints[0];
    delete[] ints;
    return 0;
}

So the idea is that instead of newing a bunch of arrays (and having them placed in different places in memory), I just point to an array I made all at once.

The reason for the delete[] (int*) ints; is because I’m actually doing this in a class and it would save [trivial amounts of] memory to not save the original pointer.

Just wondering if there’s any reasons this is a horrible idea. Or if there’s an easier/better way. The goal is to be able to access the array as ints[x][y] rather than ints[x*YWIDTH+y].

EDIT: A simple benchmark suggests that my way is faster without the optimizer, but gcc may optimize better on the simple way for some reason.

http://pastebin.com/YDRuLuXv

If you compile with gcc -O0, the best should be stack, then mine, then normal. If you compile with X_MAX set to large value and Y_MAX set to a small value, and use gcc -O3, mine and stack should be really really fast, but the normal one won’t be. If you make X_MAX small and Y_MAX big, the normal way should win (even over the stack method for some reason).

  • 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-14T21:30:29+00:00Added an answer on May 14, 2026 at 9:30 pm

    The problem with that kind of approach is that it is error prone. I would tell yout to wrap the allocation and access to individual elements os your 2D array in a class.

    class Array2D
    {
    private:
        /* Pointer necessary for the choosen implementation */
    public:
        Array2D(unsigned int dim1, unsigned int dim2);
        ~Array2D() /* Needed, since you will be allocation memory for this class */
        double operator()(unsigned int x, unsigned int y);
    }
    

    In that case, if you ever feel the need of changing the allocation, you can only change the methods implementation, keeping the interface intact. The rest of your code would actually use only the operator() and the constructor. It will help you, also, to prevent memory leaking.

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

Sidebar

Ask A Question

Stats

  • Questions 427k
  • Answers 427k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For anyone who encounters this problem using Hibernate ...... creating… May 15, 2026 at 12:58 pm
  • Editorial Team
    Editorial Team added an answer The newly created task is actually a SharePoint ListItem. ExtendedProperties… May 15, 2026 at 12:58 pm
  • Editorial Team
    Editorial Team added an answer Here is the code to make this work. [UIView beginAnimations:@"animateText"… May 15, 2026 at 12:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.