Since you told the compiler what x would be used for with the // preprocessor directive (//matrix-size) it knows what to do with it when you give x to it.
Name:
Anonymous2010-09-14 8:42
It can likely allocate stack space anyway in the function unlike C, thus this is allowed.
Name:
Anonymous2010-09-14 8:52
I guess I could maybe put in a few lines with sizeof at the end of the program to output whether the array is actually x places long, or if it just assigned some random value to x on compile.
Use a language where you can count on the compiler to not silently do the wrong thing.
Name:
Anonymous2010-09-14 9:29
>> 5: I have no idea if you're trying to be funny or what. I *am* learning C++, and I'm wondering why my compiler is letting me do this thing that would get me shot if I did it in C.
>> 6: No, that would be the smart and logical thing to do. This is school, so we're using C++ or failing.
Name:
Sensei2010-09-14 9:30
>>4
This is the wrong approach to learning about the language and your compiler. Drafts of the C++ standard are available for free, which you can read to find out if variable length arrays are permitted in vanilla C++ (they're not). GCC has documentation that lists its extensions to C++, all of which are enabled by default (variable length array support is one of them, see http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html).
>>4
sizeof with arrays will likely only work if the array is not variable length. You'd probably just get back the size of a pointer.
Name:
Anonymous2010-09-14 14:52
>>1
No, this should not work. Most likely, your code isn't even being compiled. For example, this is in a file that isn't added to your makefile, etc... You didn't paste the whole function, so we can't tell if this is in main() or somewhere else.
>>14
Yes it's valid C. It's not valid C for creating a dynamic array. That's the important difference. The value of x has to be known at compile time, or the compiler will (a) give an error if it's a decent compiler or (b) compile with a random value for x (Typically whatever value is in that RAM block at that very moment, which can lead to hilarious results)
I'm afraid g++ is giving me the (b) option and I'm not happy with it, which is why I made the thread.
Name:
Anonymous2010-09-15 5:39
Incidentally, while I'm at it: I've had people complain when I include conio.h to use the getch() function. Is there some problem with this that I don't know?
>>18,19 >>1-kun, please read >>8. GCC certainly does not just "use a random value" for x. You will not learn anything by making up answers and assuming they are true. You can use the GCC options -std=c++98 -pedantic to compile according to the C++98 standard and disallow GNU extensions. People don't like conio.h because it's not standard.
>>21
Didn't know about -std=c++98 -pedantic, thank you. I definitely prefer my compiler to read all code as strictly as possible.
RE conio.h not being standard, I thought that was the entire point of libraries, to import non-standard functionality. Have I completely misunderstood something important?
[m]Just a gentle reminder that I took some pains at the last OOPSLA to try to
remind everyone that Smalltalk is not only NOT its syntax or the class
library, it is not even about classes. I'm sorry that I long ago coined the
term "objects" for this topic because it gets many people to focus on the
lesser idea.
The big idea is "messaging" -- that is what the kernal of Smalltalk/Squeak
is all about (and it's something that was never quite completed in our
Xerox PARC phase). [m]
>>29
Indeed that is the point of libraries. The important consideration is this: do you really need getch, or is cin good enough? Avoiding unnecessary libraries is always best. If you really do need special terminal functions, note that there are both the conio and ncurses libraries. conio is more of a Windows thing while ncurses is more of a *nix thing.
>>46
The remake was better because they had access to better technologies, and the industry was more mature when it was made. I forgot which one I was talking about.