Solved

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

Posted on 2000-02-14
4
207 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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
why cant`t you declare an array of datatype[x][y]? Whay does it cause an error ?
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now