LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2009, 03:54 PM   #1
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Rep: Reputation: 15
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.

Thanks
 
Old 12-07-2009, 04:03 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.

Last edited by tuxdev; 12-07-2009 at 04:06 PM.
 
Old 12-07-2009, 04:21 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 12-07-2009 at 04:23 PM.
 
Old 12-07-2009, 04:37 PM   #4
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
Okay, I know now that the topic should goes down to compiler's standard level.

Thanks for your quick reply.
 
Old 12-08-2009, 11:39 AM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by tuxdev View Post
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.
 
Old 12-09-2009, 06:08 AM   #6
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
I will get back to this asap.
Thanks for replies.
 
Old 12-13-2009, 05:00 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
A problem in declaring an array in C... pittopitto Programming 10 08-03-2006 02:28 AM
default array size for variable in gcc compiler mlaich Programming 13 01-04-2006 05:40 PM
Segmentation fault after declaring a large array. oulevon Programming 6 11-08-2005 02:41 AM
C++: Trouble declaring dynamic array Hady Programming 7 11-01-2005 01:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration