aspose file tools*
The moose likes C / C++ and the fly likes array in c++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "array in c++" Watch "array in c++" New topic
Author

array in c++

Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Hi,

int n;
int a[n];

Shouldn't the code above give me a compile time error?


I see wind mills
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You are inadvertently using a feature from the C99 standard called variable length arrays. GCC supports this feature in default mode, so presumably that's the compiler you're using. Use the -pedantic switch to warn about this, or, for example, g++ -std=c++98 to turn this into an error.


[Jess in Action][AskingGoodQuestions]
Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Ernest Friedman-Hill wrote:You are inadvertently using a feature from the C99 standard called variable length arrays. GCC supports this feature in default mode, so presumably that's the compiler you're using. Use the -pedantic switch to warn about this, or, for example, g++ -std=c++98 to turn this into an error.


Yeah, thanks. One of my students came up with this code and for a moment I thought I did not know what I was talking about .

So, behind the scenes, what does this code do? The array in this case must be dynamically alocated right? Does the compile translate it to a malloc function or something?

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't know, actually. GNU compilers have always offered alloca() which allocates dynamic memory on the stack; I assumed it used that function -- although the scope rules are a little different, so maybe it's a modified version of it.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

While EFF's response addresses your query in full, please be aware that your (simplified?) snippet of code does not initialize 'n'. As a result, the code's behaviour is undefined. If anything, 'n' should be a size_t (not an int) and it should be initialized to a value greater than zero.

Also, please be aware that the array in

is different from the array in


In the latter, you have your "regular" array whereas in the former, you have a VLA. As EFF already pointed out, all of this applies to C and not C++.

Since you are using C++, I'd suggest you encourage your students to use "std::vector<int>" rather than arrays. See http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1

best,
- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Anand Hariharan wrote:While EFF's response addresses your query in full, please be aware that your (simplified?) snippet of code does not initialize 'n'. As a result, the code's behaviour is undefined. If anything, 'n' should be a size_t (not an int) and it should be initialized to a value greater than zero.

Also, please be aware that the array in

is different from the array in


In the latter, you have your "regular" array whereas in the former, you have a VLA. As EFF already pointed out, all of this applies to C and not C++.

Since you are using C++, I'd suggest you encourage your students to use "std::vector<int>" rather than arrays. See http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1

best,
- Anand


Yeah, actually the whole code includes a scanf command to initialize n based on an input given at runtime, which looked strange for me at first.

Thanks.
 
 
subject: array in c++