tags:

views:

246

answers:

10

Hi...Please check this code out it compiles and runs absolutely fine.. The question is that when i started learning c++ (turbo c++) i never was able to declare an array of any type as ..

datatype var[variable_set_at_runtime];

and i took it for granted that this cant be possible in latest gcc compilers...but surprisingly this is possible...

So my related question is that whats the need of new operator then??

I know that new operator does a lot of things including dynamically allocating memory at runtime in heap,returning an address to that resource...etc..

What difference i feel is that my code dynamically allocates the memory on stack while new does it on heap?? is this the only difference...

i am really astonished after writing something like this as i could not do this when i started learning c++ and ...to add to it i can do this for custom data types too... :-O

#include<iostream>
using namespace std;
struct A
{
    int a;
    int b;
};
int main()
{
    int a;
    cin>>a;
    int ch[a];
    for(int i=0;i<a;i++)
        ch[i]=i+1;
    for(int i=0;i<a;i++)
        cout<<"\n\nch=="<<ch[i];
    A obj[a];
    cin>>obj[a-1].a;
    cin>>obj[a-1].b;
    cout<<"\n\n obj.a,obj.b=="<<obj[a-1].a<<" , "<<obj[a-1].b;
}

Please shed some light..

Thanks.

A: 

The stack is a very small amount of memory. You should only allocate small, temporary things on the stack. Large or non-temporary objects would do well to be allocated on the heap.

Yes, that will require careful memory management.

Justice
A: 

Things allocated "automatically" will disappear when the allocation goes out of scope. Things allocated "on the heap" with new will not.

unwind
A: 

Any variable declared as you have specified is put on the stack. Once it goes out of scope, it will no longer be valid. However, if you use new to declare your array, it will be allocated on the heap and remain valid until you call delete on it.

Eric
+1  A: 

You'd use new:

datatype *var=new [variable_set_at_runtime];

and then delete it when you're finished with it:

delete[] var;
Sean
A: 

You still need the new operator to allocate things on the heap and this is required if you want them to survive beyond the scope of the function they're created in.

The stack is often also far more limited than the heap in terms of size and I'd love to see a linked list (or any non-array collection) of objects created using the variable length array declaration method :-)

No, I think new may be around for a little while longer.

paxdiablo
+1  A: 

You have to allocate it on the heap, using new :

int* ch = new int[ a ];

but don't forget to deallocate it after usage :

delete [] ch;

A better way would be to use a std::vector that does exactly what you want.

Klaim
A: 

"What difference i feel is that my code dynamically allocates the memory on stack while new does it on heap?? is this the only difference..."

Thats exactly the difference. Just remember. The stack has limited space whereas the heap has none of the restrictions (Well obviously it has some restrictions ... try making a 4 Gig allocation for one ;)). In general id say that if you know your stack allocation will be relatively small then use your method, otherwise create it on the heap.

Goz
+9  A: 

and i took it for granted that this cant be possible in latest gcc compilers...but surprisingly this is possible...

It is legal in C, but not C++. GCC might allow it depending on compiler flags, but if you compile your code as strict C++ (which you should), dynamic-length arrays are not allowed, and you have to use new. (I'm surprised no one has mentioned this little detail yet)

Apart from this, the two other big differences are that:

  • data on the stack is automatically cleaned up when it goes out of scope
  • only something like 1MB is typically allocated for the stack. Large datastructures should go on the heap instead.

But really, the single most important point is the first one -- it's not valid C++. (And as Neil pointed out, it is not valid in C++0x either. There are no plans of adding this to C++)

jalf
And before anyone asks, it appears not be valid C++0x either.
anon
Furthermore, it's legal only in C99.
Tomi Kyöstilä
btw how is C++0x pronounced??
ashishsony
@ashishsony, it seems that it will be C++10 rather than C++0x, because noone know how to pronounce C++0x correctly :)
Kirill V. Lyadvinsky
See plus plus oh ecks?Of course, 0x is just a nickname, because no one is yet sure which year it'll be out. I think the hope for a C++09 are dwindling, so hopefully we'll get a C++10 instead.
jalf
Or C++0xa ... maybe C++0xb :-)
rstevens
+1  A: 

now, There is 2 different approaches, either you use the new operator as other answers have been suggesting, but that have some problems for example you have to make sure to not leak any memory.

I would rather suggest to use STL to handle this kind of stuff, a list for example.

That way you don't have to handle the memory management yourself, and the code looks neater.

look here for some guide: http://www.sgi.com/tech/stl/

Alexander Kjäll
+1: STL is the most appropriate solution for implementing dynamic arrays in C++. Something like std::vector<> is a nice replacement for arrays.
D.Shawley
A: 

Use _alloca. See my answer here for Variable Sized Arrays in C

Jim Buck