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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:25:35+00:00 2026-05-27T15:25:35+00:00

Can someone please point out what I am doing wrong in the following code?

  • 0

Can someone please point out what I am doing wrong in the following code?

int* a = NULL;   
int* b = NULL;   
a = new int[map->mapSize.width];
b = new int[map->mapSize.height];

layer->tileGids = new int[a][b];

Here’s what the code uses:

typedef struct _size {
    int width, height;
} size;

class Map {
    size mapSize;
}

class Layer {
    int * tileGids;
}

EDIT: Compiler-Errors (in line 6 of the first bit of code):

  • error: expression in new-declarator must have integral or enumeration type|
  • error: ‘b’ cannot appear in a constant-expression|

Solution:

I have decided to accept lightalchemist’s answer. In essence, what works for me is use a vector instead of the array. Vector manages the memory for you and hence is a lot easier to deal with.

  • 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-27T15:25:35+00:00Added an answer on May 27, 2026 at 3:25 pm

    Firstly, your variables “a” and “b” are pointers. Your code:
    layer->tileGids = new int[a][b]
    is the root cause of the problem.

    I’m trying to guess your intention here and I think what you are trying to do is make layer.tileGids a 2 dimension array to reference a “grid” of size (mapSize.Width, mapSize.height) so that you can refer to each “cell” in the grid using layer.tileGids[x][y].

    If you are indeed trying to create a 2 dimension array, there are 2 methods to do it.

    Method 1:

    class Layer {
    int ** tileGids; // NOTE the "**" to indicate tileGids is a pointer to pointer i.e. 2D array.
    }
    

    To initialize it:

    int width = map->mapSize.width;
    int height = map->mapSize.height;
    layer.tileGids = new int*[width]; // NOTE the "int*" to indicate tileGids is a new array of pointers to int.
    for (int i = 0; i < width; i++) // Initialize each element in layer.tileGids[] to be a pointer to int.
    {
      layer.tileGids[i] = new int[height];
    }
    

    Now you can access the items in layer.tileGids using:

    int value = layer.tileGids[x][y] // where 0 <= x < width and 0 <= y < height
    

    To deallocate this data structure, similar to how you allocate it, you need to deallocate each dynamically allocated array in each “row”:

    for (int i = 0; i < width; i++)
        {
          delete [] layer.tileGids[i]; // Deallocate each row.
        }
    
    delete [] layer.tileGids; // Deallocate "array" to the pointers itself.
    

    Method 2:

    Now another easier, less messy method (avoid pointers) is to use the C++ vector class. You need to make the following changes:

    #include <vector>
    class Layer {
      vector<vector<int> > tileGids; // Note the space at "<int> >".
    }
    

    To initialize:

    int width = map->mapSize.width;
    int height = map->mapSize.height;
    layer.tileGids = vector<vector<int> >(width, vector<int>(height, 0));  // Initialize all entries to 0.
    

    To access the elements:

    int value = layer.tileGids[x][y]; // Where 0 <= x < width and 0 <= y < height
    

    Note that for the second method using vectors, you do not have to do any memory cleanup as is required in the first method because the vector will automatically take care of it. However, because a vector can grow dynamically i.e. you can add items to it, you lose the safety of having a fixed size array i.e. someone could accidentally increase the size of your grid if you use the vector method but if he tries to do that when you intialized it using the first method above an error will occur and you will immediately know that something is wrong.

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

Sidebar

Related Questions

it's assembler right? can someone please point out the progression that we've had in
Could someone please point out a site where I can find an algorithm to
Can you someone please point in me in a direction, sample code or an
Can someone please point out some books or online resources which explain in detail
Can someone please point me to the easiest way to have a timer in
Can someone please point me to articles or books that discusses different programming paradigm
Can someone please point me to instructions for installing the latest Indy10 in Delphi
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Can someone please confirm that the following is a bug with PHP 5.2.13: (thank

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.