Solved

building an array on heap ends in failure (const expected)

Posted on 2000-02-14
4
210 Views
Last Modified: 2013-11-20
When building an array of 2 dimensions on heap it causes an error: const expected and not able for initializing array of dimension (0); source code: struct pixel {char R,G,B} and source code in the application: pixel *pPixel = new pixel[var1][var2];//var1 and var2 are ints.
0
Comment
Question by:alexott
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
inpras earned 50 total points
ID: 2518257
Hi see here
CPP file
#include <iostream.h>
#include "pixel.h"
void main()
{
      int var1 =10, var2 = 10;
      pixel **pPixel;
      *pPixel = new pixel[var1];//var1 and var2 are ints.
      for (int i = 0; i<=var1; i++)
      {
            pPixel[i] = new pixel[var2];
      }
}
..h file
struct pixel {char R,G,B;
};

Hope this helps
inpras
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2518287
Hi alexott,

this is from MSDN about new operator:

-----------------------
All but the leftmost array dimensions must be constant expressions that evaluate to positive values; the leftmost array dimension can be any expression that evaluates to a positive value.
-----------------------

ZOPPO
0
 

Author Comment

by:alexott
ID: 2521754
why cant`t you declare an array of datatype[x][y]? Whay does it cause an error ?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2521878
Hi alexott,

You can declare an array of datatype[x][y] as long as 'y' is of type const.

I think this is because C/C++ does implement an array as follows:

type array[x][y][z][...] is interpreted as an array of x pointers to blocks of size (sizeof(type)*y*z*...). Now, if y, z... are not const, the compiler cannot determine the size of the blocks pointed to, so each time allocating such an array would mean the system has to store the size of the blocks at runtime and with any subscription access use this stored, dynamic size. This would result in a distinct decrease of performance and optimation possibilities.

hope that helps,

ZOPPO
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question