C/C++ declaring an array with non-constant size variable (related to compiler)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C/C++ declaring an array with non-constant size variable (related to compiler)
I have a question regarding about declaring a new array with non-constant variable, like the following.
int main(int argc, char** argv)
{
int N = atoi(argv[1]);
int array[N];
}
As you see above, the program receives the size of the array to be created via main's argument. It's compiled successfully.
I used to program like this in C (I think its standard is some kind of C99, I am not sure about the name, sorry.) and the source file is compiled unsuccessfully.
So I guess it's about the compiler's using of standard C/C++ used in compiling process.
My question is that according to above, Is C99 (not sure, for the name) standard is abandoned already or making less popular until now? As I found on many modern linux system that I can use the code above and be able to compile successfully.
Variable length arrays are part of C99. They do not exist in C++. Use std::vector anyway instead. It's not that C99 is abandoned, it's that C and C++ are different languages.
In previous discussions, the consensus seems to be that this C99 feature is not valid according to the C++ standard but is supported in C++ by GCC and a few other compilers.
I haven't either looked it up in the C++ standard to verify that it isn't supported (but I'm confident it is not supported) nor looked it up in GCC documentation to see how the non standard support is documented.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by tuxdev
Variable length arrays are part of C99. They do not exist in C++. Use std::vector anyway instead. It's not that C99 is abandoned, it's that C and C++ are different languages.
No. the OP said it did compile in C++ but not in C.
Generally speaking, storage-classes like std::vector (and their ilk...) are considerably more versatile than anything you could cook-up by hand.
The concept is simply that the "index" is used as a key to find the value being sought. The class takes care of all the (very efficient) management of storage, locating values, iterating through values and so on. It is a superior, and generalized, solution to the problem in most cases.
Usually, you care that the values are stored, and not how they are stored...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.