?
Solved

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

Posted on 2000-02-14
4
Medium Priority
?
214 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Suggested Courses

777 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