Solved

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

Posted on 2000-02-14
4
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 163
sumHeights2  challenge 7 89
changeXy challenge 13 68
Path of Workbook 3 61
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

25 Experts available now in Live!

Get 1:1 Help Now