Click to See Complete Forum and Search --> : C++ and Arrays


TaeShadow
11-06-2000, 09:38 PM
Here's a short program. Should it work?

#include <iostream.h>
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
int array[number];
cout << "The size of the array is"
<< sizeof(array)
<< " bytes.";
return 0;
}


It shouldn't, should it? Yet, g++ allows me to do this kind of thing. I thought standard arrays have to be declared with a constant size. What's going on?


Tae

binaryDigit
11-06-2000, 09:56 PM
g++ let you do that. never let me do it.


------------------
http://home.earthlink.net/~pebice/philLinux.html (http://home.earthlink.net/~pebice)


[This message has been edited by binaryDigit (edited 06 November 2000).]

TaeShadow
11-06-2000, 10:14 PM
I'm trying the exact same program in both g++ and VC++. VC++ doesn't even let me compile, and g++ works fine.


Tae

kmj
11-07-2000, 09:21 AM
Are you compiling it with whatever flag makes is ANSI compatible?

Sterling
11-07-2000, 09:22 AM
Does the program actually run without segfaulting? And what happens when you use -Wall as a command-line option?

If it really does do this, and it isn't mentioned in the documentation, write the gcc maintainers about it. (Be sure to include version number) This is definitely not good behavior, AFAIK...

------------------
-Sterling
-This post made with the Lizard! (http://www.mozilla.org)

Gweedo
11-07-2000, 09:57 AM
It segfaults if you enter a negative number otherwise it will run. Damn....

manual_overide
11-08-2000, 03:37 AM
that works for you??

you should have to include vector.h and delcare it as a vector.

TetsuoII
11-09-2000, 05:25 AM
This will work:
#include <iostream.h>
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
int* pArray = new int[number];
return 0;
}

You won't get the size of the array, but you already know this from number. You should also check that number>0.

T.

TaeShadow
11-09-2000, 10:01 AM
Yes, I know that that will work. The problem is that the other code works too!

Tae

TaeShadow
11-09-2000, 10:04 AM
Ok, check this out:
http://gcc.gnu.org/onlinedocs/gcc_4.html#SEC81

Sterling
11-11-2000, 01:22 PM
Ah. This would be one of the sanity-improving GNU extensions to the language. I'm betting that if you use ANSI compatibility flags, that causes an error.

------------------
-Sterling
-This post made with the Lizard! (http://www.mozilla.org)

Mandrel
11-13-2000, 02:03 AM
I have been programming in C++ for about 3 years, and g++ is the first compiler I've found that can compile a program with a static array like that. I figured that out in class earlier in the year, and was totally amazed.

Usually, with a static array, the memory is allocated right when the program is started, unlike a dynamic array, where the memory is allocated when 'new' or 'malloc' is called. The way I always figured it was since the memory for a static array was declared when the program started, it needed to know how big the array was so it could allocate the correct amount of memory. Maybe g++ has found a way around this. If so, more power to 'em!

TaeShadow
11-13-2000, 02:47 AM
It's nice not having to fuss with pointers, but I think I'd rather they stuck with the standards.


Tae

Sterling
11-13-2000, 04:55 PM
TaeShadow - As I think I mentioned earlier, there's flags one can set to enforce ANSI compatibility.

------------------
-Sterling
-This post made with the Lizard! (http://www.mozilla.org)

bakerb
11-14-2000, 01:06 PM
I tried all of the following with the above code:

g++ t.cc
g++ -ansi t.cc
g++ -Wall t.cc
g++ -ansi -Wall t.cc

none of them busted me, and all of them ran. I am very dissapointed.

I tried it on VC++ and it busted me. I hate it that VC++ got something that g++ didn't. It is in fact a dark day for all of us.

Addendum:
I also tried loading the array with stuff and printing it out. That worked too, and that's NOT ansi standard .


------------------
Save the whales. That way, you can collect and trade them with your friends!

[This message has been edited by bakerb (edited 14 November 2000).]

BrianDrozd
11-14-2000, 01:29 PM
Originally posted by bakerb:
I tried all of the following with the above code:

g++ t.cc
g++ -ansi t.cc
g++ -Wall t.cc
g++ -ansi -Wall t.cc

none of them busted me, and all of them ran. I am very dissapointed.

I tried it on VC++ and it busted me. I hate it that VC++ got something that g++ didn't. It is in fact a dark day for all of us.

Addendum:
I also tried loading the array with stuff and printing it out. That worked too, and that's NOT ansi standard .




The flag to enforce ANSI standards is, I beleive,

-traditional

Give that a try.

Strike
11-14-2000, 02:20 PM
also try the -pedantic compiler flag

TaeShadow
11-14-2000, 11:25 PM
I tried it with the pedantic option and it did not let me compile. That's reassuring, I think.


Tae