C/C++ query

3 views
Skip to first unread message

Abhishek

unread,
Aug 6, 2010, 11:12:52 PM8/6/10
to DS & Algo@itbhu
Initializing an array via a variable is restricted in C++.

int a=5;
int arr[a];//error

I checked it in GNU compilers to my astonishment that this code really
works.

Also suppose an array is declared to hold 5 data.i.e.

int arr[5];

Now, if you try to assign value to array element even greater than 5,
the compiler doesnt have a problem.

arr[7]=10;//this works

Kindly solve the doubt as to what is going wrong.

Abhishek

unread,
Aug 6, 2010, 11:13:13 PM8/6/10
to DS & Algo@itbhu
For a const variable, in general the compiler does'nt allocate memory
for that variable in C++. So you cannot reference the variable to
initialize any array for its size as:

const int a=5;
int arr[a];//incorrect

The above code is also verified to be incorrect in Bruce Eckel. But to
my surprise its running well and fine in both GNU compiler and DEV-CPP
compiler.

Please Help.

Regards,
Abhishek

Ankit Agrawal

unread,
Aug 6, 2010, 11:40:26 PM8/6/10
to ds--al...@googlegroups.com
Can you please give me page reference no. where you have read this statement:

So you cannot reference the variable to
initialize any array for its size as:

const int a=5;
int arr[a];//incorrect

According to me bruce eckel says that this thing is valid (Refer page no 335 to 337 of chap 8)



--
You received this message because you are subscribed to the Google Groups "DS & Algo@itbhu" group.
To post to this group, send an email to ds--al...@googlegroups.com.
To unsubscribe from this group, send email to ds--algoitbh...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ds--algoitbhu?hl=en-GB.




--
Ankit Agrawal
Final year undergraduate
Department of Computer Science & Engineering
IT-BHU
Varanasi-221005
+919305293441

Abhishek

unread,
Aug 6, 2010, 11:47:35 PM8/6/10
to DS & Algo@itbhu
Sorry guys,
I made a wrong statement, the following statement is said to be
incorrect in C++

int a=5;
int arr[a]; //said to be incorrect but is working fine for me

Compiler cannot reference a variable who has been allocated memory
during compile time, but this is working fine in GNU compiler.

On Aug 7, 8:40 am, Ankit Agrawal <ankit.agrawal.cs...@itbhu.ac.in>
wrote:
> > ds--algoitbh...@googlegroups.com<ds--algoitbhu%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/ds--algoitbhu?hl=en-GB.
>
> --
> Ankit Agrawal
> Final year undergraduate
> Department of Computer Science & Engineering
> IT-BHU
> Varanasi-221005+919305293441begin_of_the_skype_highlighting              +919305293441      end_of_the_skype_highlighting

prashant bhutani

unread,
Aug 7, 2010, 2:12:35 AM8/7/10
to ds--al...@googlegroups.com
What Bruce Eckel says about

int a=5;
int arr[a];
is that this should be AVOIDED because int a=5 is not known to the compiler during compilation time while int arr[a] needs a to be known to the compiler during the compilation time.
SO, this ought not to work does not mean it will never work. It simply means that your compiler is outshining the one he got in his time :)

about

const int a=5;
int arr[a];
Bruce Eckel says that,
any constant variable declared in the program is (mostly) not assigned the address and is treated as compile time constant (same way as our pre-processors do). So the requirement needed for declaration of array is fulfilled.

Now, problem of accessing a[7] is not a fault. In words of Bjarne Stroustrup(page 92), taking a pointer to the element beyond the end of the array is guaranteed to work. This is important for many algorithms. However, such a pointer does not in fact point to an element to the array, it , may not be used for reading or writing. ( According to this, other elements as in above case can be altered.)
Also, the result of taking a element before array's first element is undefined and should be avoided.
Taking the pointer beyond array boundary is restricted as it may point to some address needed for other useful work by your program or computer and thus overwriting is can cause problems.

HTH.

Regards,
Prashant Bhutani
Junior Undergraduate
CSE, IT-BHU

To unsubscribe from this group, send email to ds--algoitbh...@googlegroups.com.

gaurav mishra

unread,
Aug 7, 2010, 5:15:57 AM8/7/10
to ds--al...@googlegroups.com
in declaring arr[5] the space is allocated on stack
 
and in arr[size]; where size is a variable
the space is allocated on the heap (new compilers are fodu and they call new fucntion behind the scenes)
Reply all
Reply to author
Forward
0 new messages