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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:10:18+00:00 2026-06-14T13:10:18+00:00

(Note: I’m currently learning C++, so if there is a better way to do

  • 0

(Note: I’m currently learning C++, so if there is a better way to do what I am doing, explanations would be helpful.)

I am making Conway’s Game of Life in C/C++ and have the following code:

bool ** previous;
bool ** current;

void init() {
    previous = new bool*[width];
    current = new bool*[width];
    for (int i =0; i < width; i++) {
        previous[i] = new bool[height];
        current[i] = new bool[height];
    }
}

The reason for the dynamic arrays is that the width and height are given by the user at runtime. If I had one-dimensional arrays, I could do the following:

bool * previous;
bool * current;

void () {
    bool * temp = current;
    current = previous;
    previous = temp;
}

However, this approach does not work as hoped with the two-dimensional array. As it is technically an array of arrays, do I have to swap each sub-array pointer individually? Is there a better way to go about defining multi-dimensional arrays and swapping them?

Edit: I have yet to really use any C++ specific features, so if I can do this in pure C, I’d prefer that.

  • 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-06-14T13:10:19+00:00Added an answer on June 14, 2026 at 1:10 pm

    There’s no real reason here to bother with an array of arrays. It’s a ton more work to maintain and destroy properly, especially as you’ve avoided using the Standard Library containers that would handle a lot of the memory management for you.

    It’s usually easier to allocate width * height cells in a single array and reference them like cell[x + width * y] where x is your column, y is your row.

    Do keep in mind that in the particular case of this problem what you want is an array of bits and not an array of booleans. C++ does have a special case container for a bitset that is worth using: std::vector<bool>.

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

Sidebar

Related Questions

(Note: This is not a question about what is the best way with code
Note: Currently using Perforce as a CM tool. I currently do several debug releases
Note: There is a very similar question here . Bear with me, however; my
Note: If you think of a better title/question, feel free to suggest it. I
Note: I know there are a lot of similar questions on SO. However, I
Note: I'm not asking if there is a maximum, this question has been asked
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
Note: I am not asking how to use Google Code's SVN repo as a
note: i only want to move 1 shape at a time Circle.prototype.create = function(){
Note: Not sure if this is the right stack, please tell if I should

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.