Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array in c++

 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

int n;
int a[n];

Shouldn't the code above give me a compile time error?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic