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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:13:35+00:00 2026-05-29T06:13:35+00:00

Suppose I have a grid of squared defined like so in a class: Square

  • 0

Suppose I have a grid of squared defined like so in a class:

Square (* grid)[];

This, oddly, seems to compile fine. I would think it would error because the compiler doesn’t know how big the array is?
Anyways,
it means it is a pointer to an array. Then to initialize it, I do:

grid(new Square[width * height])

This isn’t accepted by the compiler, because the new statement returns a pointer to squares rather than a pointer to an array of squares. It makes sense that it does that. Now, is there a simple way to accomplish what I’m asking, other than just declaring Square ** grid and looping through it and doing separate allocations for each column of the 2D array?

  • 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-29T06:13:36+00:00Added an answer on May 29, 2026 at 6:13 am
    Square (* grid)[];
    

    This, oddly, seems to compile fine. I would think it would error because the compiler doesn’t know how big the array is?

    That’s declaring a pointer to an array, not an array; it’s fine to declare a pointer to any incomplete type, including an array of unknown size. However, it’s quite an unusual thing to do, and not what you want for a dynamic array.

    Now, is there a simple way to accomplish what I’m asking?

    The easiest dynamic array to use is:

    std::vector<Square> grid;
    

    initialised as

    grid(width * height)
    

    If you really want to manage the memory yourself, then change your pointer-to-array to a pointer-to-object:

    Square * grid;
    

    initialised as

    grid(new Square[width * height])
    

    A pointer can point to either a single object, or the start of an array; if it does point to an array, then you can use [] on it just like with a non-dynamic array. Make sure you deallocate it (delete [] grid;) once you’ve finished with it.

    If you want a 2-dimensional array, it’s often easiest to use a 1-dimensional array, and wrap the necessary arithmetic in an accessor function:

    Square & get_square(size_t row, size_t col) {
        return grid[row * width + col];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have a table of numbers like this (we can assume it is
Suppose I have PathGeometry , consisting of lines , like this (the rectangle ==
Suppose I have a grid with some row definitions, and a child control in
Suppose I have the following (shortened for simplicity): jQuery(#grid).jqGrid({ ... ondblClickRow: function() { //
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
in my application i have this code: <Grid Name=BaseGrid> <Grid.Resources> <XmlDataProvider x:Name=ScenesXmlName x:Key=ScenesXml XPath=person
Suppose you have an NxN grid, and you want to visit every cell exactly
I have a simple rich:panel . I would like it to behave like one
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}

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.