expression must have a constant value - second subscript value in field

#define NULL 0
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int n, m;
char *field(NULL);
field = new int[n][m];

delete []field;
return 0;
}
What on earth are you trying to do?
Maybe it's a test for prospective junior progammers? Spot all the errors?
Given the title of this post, I think the issue you are asking about is the fact that C++ only supports a single array dimension for dynamic allocation.

You can allocate an array of constant sized arrays, though.

1
2
3
4
5
6
7
8
void Test(int n)
{
    const int c = 10;

    int (*field2)[c] = new int[n][c];

    delete [] field2;
}


int** does not work in this case.

If you need to define 2 or more dynamic dimensions, you could allocate a one dimensional array and then calculate the offset yourself. In this case I would define a little array class with a SetAt(int, x, int y), etc methods (you can get operator[] to behave like a normal array, but I don't think it's worth the extra effort).

Andy

P.S. I had to check the syntax for the array pointer; I would use the array class myself.
Last edited on
closed account (zb0S216C)
First, Don't define NULL twice. It's already defined when you include <iostream>. If you're using VC++ 2010, it's recommended that you use nullptr. Second, you declare 2 variables, n and m, which remain uninitialized (you should get a warning from your compiler regarding this) which you use later on. Thirdly, you create a 2-dimensional array of characters. Since you release the resources allocated by field as soon as they're allocated, you waste time and resources.

Andy, line 5 of your code gives me error. int (*field2)[c] is a pointer to an array of c integers. A pointer to an array cannot directly allocate resources. Instead, it must point to an array that's allocating resources. Also, you never check if n is a valid integer.

Wazzak
Last edited on
@Framework

A belated reply! I made a note to check this and get back to go then mislaid the note...

What compiler are you using? I compiled and ran the little fragment with vc++ 2008 and gcc 4.4 ok.

Andy

P.S. I follow the lead of text books and other sample code in omitting error handling code in small fragents.
Last edited on
closed account (zb0S216C)
andywestken wrote:
What compiler are you using?

I'm using Visual C++ 2010. Since it supports some C++0x features, it makes sense to keep updated, right?

Wazzak
True.

Are the new feature on by default?

Some companies, esp. in the financial sector, like to avoid "cutting edge" technology; they let everyone else sort out the teething troubles before switching to the new version. So I'm trying to keep coding in C++ from coding in C++0x well separated for the time being.

I have gcc installed, as well as Visual Studio 2008, so I can explore bits of C++0x with that.

By the way, I've never used the trick in the fragment I posted in real code, or even though of doing so. I have just played about with it, after I sat the syntax mentioned somewhere on the web.

But it seems pretty useless as you cannot pass (e.g.) a int *()[2] via a char** param. So I'm left wondering why this particular syntax works. The behaviour of Vsual C++ 2010 suggests that maybe it's a bit of a mistake that it does.

Andy
closed account (zb0S216C)
anywestken wrote:
I have gcc installed, as well as Visual Studio 2008, so I can explore bits of C++0x with that.

Doesn't the latest GCC release support some C++0x features? I didn't know that VC++ '08 supported C++0x; you learn something new everyday, I suppose.

Still, I never use NULL any more; my new-found null is nullptr. The best thing about it is that it eliminates all debates regarding NULL vs zero.

Wazzak
Wrong way round?

vc2008 doesn't support C++0x, but gcc does (support some of it)

It's because I (also) have gcc that I can play with C++0x features.

I agree with you about nullptr. It's a pity it's tken so long to arrive!
closed account (zb0S216C)
andywestken wrote:
vc2008 doesn't support C++0x, but gcc does (support some of it)

Apologies, I misinterpreted your post.

Wazzak
Topic archived. No new replies allowed.