473,385 Members | 1,645 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,385 software developers and data experts.

gcc: static allocation of array with variable size

10
Hi everyone.
Is anybody able to explain to me why gcc will compile without the slightest warning the following code?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.    int dim;
  6.  
  7.    cin>> dim;
  8.    double array[dim];
  9.  
  10.    // operations on the array
  11.  
  12.    return 0;
  13. }
Feb 14 '07 #1
9 5472
sicarie
4,677 Expert Mod 4TB
Hi everyone.
Is anybody able to explain to me why gcc will compile without the slightest warning the following code?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.    int dim;
  6.  
  7.    cin>> dim;
  8.    double array[dim];
  9.  
  10.    // operations on the array
  11.  
  12.    return 0;
  13. }
Because nothing is illegal. You declare an int, read it in from the user, and then declare an array of doubles that size. If you tried to use it as the array name, I'm pretty sure gcc would get upset.
Feb 14 '07 #2
enri
10
Because nothing is illegal. You declare an int, read it in from the user, and then declare an array of doubles that size. If you tried to use it as the array name, I'm pretty sure gcc would get upset.
For what I knew, static allocation, as "double vect[dim];", requires the compiler to know the dimension of the required memory at compile time. In my piece of
code this is not the case.
Am I right?

To make things worse, the same code is not compiled by Visual C++, since the value of "dim" is not known...
Feb 14 '07 #3
horace1
1,510 Expert 1GB
as sicarie says the array defininition should be OK - however, you should have using namespace std;
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
what exactly was the error from the VC compiler?
Feb 14 '07 #4
Ganon11
3,652 Expert 2GB
VC++ and DevC++ both require that a regular array (no pointers) have a constant value of elements. Thus,

Expand|Select|Wrap|Line Numbers
  1. const int MAX_SIZE = 10;
  2. int arr1[10];
  3. double arr2[MAX_SIZE];
are both legal declarations - at compile time, the compiler knows that both arrays will have 10 elements. But you cannot create an array of variable size - so

Expand|Select|Wrap|Line Numbers
  1. int dim;
  2. cin >> dim;
  3. int arr3[dim];
is illegal, as the compiler cannot know what the user will enter. In order to implement this, a pointer must be used:

Expand|Select|Wrap|Line Numbers
  1. int dim;
  2. cin >> dim;
  3. int *arr4 = new int[dim];
Apparantly, the OPs compiler does not support this standard and allows a variable sized array to be created.
Feb 14 '07 #5
horace1
1,510 Expert 1GB
C99 compilers allow one to define a array whose size is determined at runtime. For example, my version of DEV-C++ which uses gcc accepts
Expand|Select|Wrap|Line Numbers
  1.    int i, dim;
  2.  
  3.    cin>> dim;
  4.    double array[dim];
  5.    cout << " array size " << sizeof(array) <<  endl;
  6.    for(i=0;i<10;i++) array[i]=i;
  7.    for(i=0;i<10;i++) cout << array[i] << " " ;
  8.  
have a look at
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1

one tends to forget about older pre C99 compilers - Borland C++ V5.6 gives an error message saying it requires a constant expression.
Feb 14 '07 #6
enri
10
C99 compilers allow one to define a array whose size is determined at runtime. For example, my version of DEV-C++ which uses gcc accepts
Expand|Select|Wrap|Line Numbers
  1.    int i, dim;
  2.  
  3.    cin>> dim;
  4.    double array[dim];
  5.    cout << " array size " << sizeof(array) <<  endl;
  6.    for(i=0;i<10;i++) array[i]=i;
  7.    for(i=0;i<10;i++) cout << array[i] << " " ;
  8.  
have a look at
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1

one tends to forget about older pre C99 compilers - Borland C++ V5.6 gives an error message saying it requires a constant expression.
Thank you all, very much,
that shed some light.

So, for what I understand, gcc uses C99 standard even when compiling plain C++, even if standard C++ does not support some of its features.
Feb 14 '07 #7
AdrianH
1,251 Expert 1GB
Thank you all, very much,
that shed some light.

So, for what I understand, gcc uses C99 standard even when compiling plain C++, even if standard C++ does not support some of its features.
Yeah, gcc/g+ has a lot of extensions. If you want it to be ANSI C++ compliant, use switch -ansi. To specify a particular standard gcc uses, use switch -std=. For more information do a 'info g++' on the command line and search for -ansi.


Adrian
Feb 14 '07 #8
nmadct
83 Expert
Actually that array isn't static, it's allocated on the stack. Stack memory is allocated at runtime so there's no problem in determining the size. As already mentioned, GCC allows this as a GNU extension. If you read the GCC documentation it clearly describes what features are GNU extensions. There are command-line switches to disable the extensions, and the -Wall switch makes GCC give better warnings. (Generally speaking, you should ALWAYS compile with -Wall and fix all warnings, as these warnings usually indicate a bug in your code, or a future bug waiting to happen.)
Feb 14 '07 #9
nmadct
83 Expert
So, for what I understand, gcc uses C99 standard even when compiling plain C++, even if standard C++ does not support some of its features.
GCC does not use C99 to compile C++, this would be incoherent because C and C++ are two different languages. It uses its own version of C++, which is basically the ISO C++ standard with GNU extensions. Some of the GNU extensions happen to involve adding features that also exist in the C99 standard.
Feb 14 '07 #10

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

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
5
by: Crimzon | last post by:
I am using MSVC++ 6.0 compiler. I am declaring an array char ch. Program works fine for these arbitrary sizes. But if I make the size of the array bigger like ch, the program gives me an error...
12
by: Neil Dunn | last post by:
Hi, I don't seem to be able to get asprintf to work when compiling under OS X. Using the following code: #include <string.h> #include <stdio.h>
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
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
13
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.