• Welcome to Valhalla Legends Archive.
 

Better than recursing on main - for Mephisto

Started by iago, April 26, 2004, 03:15 PM

Previous topic - Next topic

Mephisto

Arta, do you get that error when you try to declare an array with a non-constant value/expression?

dxoigmn

Quote from: iago on April 27, 2004, 09:18 AM
Quote from: Mephisto on April 27, 2004, 09:13 AM
I thought you could only use a constant value/expression when declaring arrays...
Perhaps argc/argv are special?  I have no idea why it works :)


Here's something else interesting that I noticed:
int main()
{
       main();
       return 0;
}

This will not compile on Linux.  I get the error:
iago@laptop:~/Projects/C$ gcc -Wall test2.cpp
/tmp/cc8BvbGZ.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
iago@laptop:~/Projects/C$


gcc doesn't like *.cpp files.  You need to use g++ or rename test2.cpp to test2.c.

MyndFyre

Quote from: Mephisto on April 27, 2004, 04:02 PM
Arta, do you get that error when you try to declare an array with a non-constant value/expression?

I'm somewhat confused (perhaps due to my C newbletness), but I was under the impression that you *could* create variable-size arrays.  Vis-a-vis:



int main()
{
  cout << "Enter a value:" << endl;
  int num = 0;
  cin >> num;

  int *array = malloc(num * sizeof(int));
  if (array == NULL)
   return 1;

  //...
}


and then you would effectively have an array with the size that you wanted.  I can't remember if it can be cast or not...  It seems like it should be able to, by setting an array pointer to the variable of the array itself.

Like I said though, I'm a C newblet, so I could be off.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

K

C99 says you can do this:


int x = 10;

int Foo[x];


you could do it like so:

int x = 10;
int *Foo = malloc(x * sizeof(int));
// ...
free(Foo);


the difference is primarily that you have to free() the second, and where they are allocated.

iago

Quote from: K on April 27, 2004, 07:36 PM
C99 says you can do this:


int x = 10;

int Foo[x];


you could do it like so:

int x = 10;
int *Foo = malloc(x * sizeof(int));
// ...
free(Foo);


the difference is primarily that you have to free() the second, and where they are allocated.

Also, you can return the second, it's on the heap, but you can't return the first one, it's on the stack and will get broken.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Arta

#20
Quote from: Mephisto on April 27, 2004, 04:02 PM
Arta, do you get that error when you try to declare an array with a non-constant value/expression?

Looks that way, yes. I get the same errors with:



int main(int argc, char **argv)
{
   int x = 10;

   int y[x];

   return 0;
}



Perhaps there's a compiler option somewhere that controls this?

Edit: perhaps not - see ms-help://ms.vscc/MS.MSDNVS/vccore/html/C2057.htm:

Quote
expected constant expression

The context requires a constant expression. The following sample generates C2057:

// C2057.cpp
int i;
// use the line below to resolve the error
// const int i = 8;

int b;   // C2057

int main() {
}


Mephisto

That's what I thought.  I could've sworn you could never use a non-constant value when declaring your array, though I don't see why you couldn't, it will always know how much memory to allocate.  Perhaps some compilers now violate this?  Or the standards have changed...?