Solved

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

Posted on 2000-02-14
4
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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