473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Static array in function

Hi,
just wondering, why does it work when I create a static array with size unknown at compile time inside a function? I.e. small example:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. void cpy(int a[], unsigned n){
  4.     int b[n];
  5.     std::copy(a, a+n, b);
  6.     std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
  7. }
  8.  
  9. int main(int argc, char **argv){
  10.     unsigned n;
  11.     std::cin>>n;
  12.     int a[5]={1,2,3,4,5};
  13.     cpy(a, n);
  14.     return 0;
  15. }
  16.  
The code runs fine, the compiler (gcc 4.5.1 / icpc 11.1) does not complain (though if I change int a[5] to int a[n] in main, the code does not compile as expected).
Is it possibly legal because n is known when local storage is allocated on the stack when the function is called? (Though if I add std::cin>>n; before int b[n] the code still compiles, which is surprising).

Thanks
Apr 27 '11 #1

✓ answered by horace1

in your case
Expand|Select|Wrap|Line Numbers
  1. void cpy(int a[], unsigned n){
  2.     int b[n];
  3.     std::copy(a, a+n, b);
  4.     std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
  5. }
  6.  
b[] is an automactic local array with memory being allocated at run time on the stack
if it was static, e.g.
Expand|Select|Wrap|Line Numbers
  1. void cpy(int a[], unsigned n){
  2.     static int b[n];
  3.     std::copy(a, a+n, b);
  4.     std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
  5. }
  6.  
memory for static variables is allocated by the compiler at compile time and hence the compiler has to know the size of the array to allocate.
with gcc the above code gives
Expand|Select|Wrap|Line Numbers
  1. xxx.cpp||In function 'void cpy(int*, unsigned int)':|
  2. xxx.cpp|4|error: storage size of 'b' isn't constant|
  3. ||=== Build finished: 1 errors, 0 warnings ===|
  4.  

2 3770
horace1
1,510 Expert 1GB
in your case
Expand|Select|Wrap|Line Numbers
  1. void cpy(int a[], unsigned n){
  2.     int b[n];
  3.     std::copy(a, a+n, b);
  4.     std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
  5. }
  6.  
b[] is an automactic local array with memory being allocated at run time on the stack
if it was static, e.g.
Expand|Select|Wrap|Line Numbers
  1. void cpy(int a[], unsigned n){
  2.     static int b[n];
  3.     std::copy(a, a+n, b);
  4.     std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
  5. }
  6.  
memory for static variables is allocated by the compiler at compile time and hence the compiler has to know the size of the array to allocate.
with gcc the above code gives
Expand|Select|Wrap|Line Numbers
  1. xxx.cpp||In function 'void cpy(int*, unsigned int)':|
  2. xxx.cpp|4|error: storage size of 'b' isn't constant|
  3. ||=== Build finished: 1 errors, 0 warnings ===|
  4.  
Apr 29 '11 #2
Ok, makes sense, thanks for the explanation!
Apr 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rahul Joshi | last post by:
Hi, I want to create a static array whose size is also a const member of the class. Something like: // A.h class A { private: static int size = 0; static int array;
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
11
by: dis_is_eagle | last post by:
hi...i am new to c programming....please explain me as to why should an character array be declared as static...thanx...eric
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
1
by: Vol | last post by:
Hi, Group, I want to initialize a 2D static array in function 'foo ()', where 'foo' is called by another function a lot of times. I list my implementation below but I think there are better...
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
32
by: mdh | last post by:
Hi all, When I try and initialize this static array, as in: void itoa_recursively(int n, char *s){ static char *p = s; it fails, yet this:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.