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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:06:37+00:00 2026-06-16T06:06:37+00:00

I’ve got a game that has a grid, which is a 2D array. This

  • 0

I’ve got a game that has a grid, which is a 2D array. This array is filled with a struct of information.

struct GridCell
{
    uint mCellID;
    Vector2 mPosition;
    uint mLevel;
    int mCellType;
};

class Grid
{
public:
    Grid();
    ~Grid()

protected:
    // Heap
    GridCell[][] mGridCells;

    // Dynamic
    GridCell*[][] mGridCells;
};

Keeping in mind that there can be many Grids in memory at once, and that those gridcell can range from very small to very large:
Is this better suited to be a heap of gridcells or dynamic (pointer) gridcells?

From what I understand:
Heap cells will take up a large chunk of memory
Dynamic cells still will, but it will be of pointers, not the entire struct. However, this could lead to fragmented memory?

I’m not sure of which one is the best for this scenario, and I probably don’t fully understand the differences between the two either. Help?

  • 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-16T06:06:38+00:00Added an answer on June 16, 2026 at 6:06 am

    Types in C++ have a fixed size. It’s not true that your GridCells will range from very small to very large. They’ll be one or the other. sizeof(GridCell) is fixed. They might each point to objects of different sizes, but those objects are not part of the size of the GridCell.

    Either way, both methods you propose will have exactly the same amount of GridCells in memory, so you save nothing by using pointers. In fact, the dynamically allocated approach will use more memory because you’re also storing pointers to each cell. The only way the pointer approach could be more memory efficient is if you used single GridCell objects to represent more than one cell in the Grid. That is, some of the pointers would be the same.

    What this really comes down to is which one is more manageable and the answer is always “the method without the pointers”. It means your Grid objects will automatically manage the construction and destruction of the GridCells without you having to care about it, helping you avoid memory leaks. If you use the pointer method, you will have to loop through your arrays in the constructor of Grid, doing new GridCell() for each element. You’ll also need to do the same in the destructor, doing delete on each cell you dynamically allocated. This is a pain, especially when it’s unnecessary.

    In the cases where pointers are required, smart pointers are much more preferable.

    Furthermore, you may even be better off using a std::array<std::array<GridCell, N> M> for your fixed sized arrays. It encapsulates the array for you, allowing you to use it as you would any other Container from the standard library.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have an array which has BIG numbers and small numbers in it. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.