Difference in the array declaration 
July 19th, 2005, 06:42 PM
| | | |
Hi,
Is it possible to answer the following question: what is the difference
between two declarations:
vector<double> a(n);
and
double a[n];
When I put, say, n=1000000, the program with the first line is working well,
whereas with the second it terminates with error. What is the maximum
possible array that can be defined this way? For n=100000 both declarations
works fine.
Compiler g++.
Thank you.
Arthur | 
July 19th, 2005, 06:42 PM
| | | | re: Difference in the array declaration
"Arthur Sinko" <asinko@msn.com> wrote in message
news:7s9ab.18341$x21.3339@twister.southeast.rr.com ...[color=blue]
> Hi,
>
> Is it possible to answer the following question: what is the difference
> between two declarations:
>
> vector<double> a(n);
>
> and
>
> double a[n];
>
> When I put, say, n=1000000, the program with the first line is working[/color]
well,[color=blue]
> whereas with the second it terminates with error. What is the maximum
> possible array that can be defined this way? For n=100000 both[/color]
declarations[color=blue]
> works fine.
> Compiler g++.
>
> Thank you.
>
> Arthur
>
>[/color]
double a[1000000]; is allocated on the stack, which probably causes the
stack to overflow. maximum size, dont know, depends on the size of the
stack, several Ks, Megs maybe???
vector<double> a(1000000); is also allocated on the stack... but the array
it used internally is allocated on the heap (free store). in other works,
vector's internal variables (like current size, capacity, etc) are on the
stack, together with a pointer to the actual data (which is stored on the
heap). heap can hold far more data than the stack can. maximum size... Total
RAM - Allready Allocated RAM :-)
hope that helps.
Martin | 
July 19th, 2005, 06:56 PM
| | | | re: Difference in the array declaration
Thank you very much.
Arthur
"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bkb911$iie$1@uni00nw.unity.ncsu.edu...[color=blue]
> "Arthur Sinko" <asinko@msn.com> wrote in message
> news:7s9ab.18341$x21.3339@twister.southeast.rr.com ...[color=green]
> > Hi,
> >
> > Is it possible to answer the following question: what is the difference
> > between two declarations:
> >
> > vector<double> a(n);
> >
> > and
> >
> > double a[n];
> >
> > When I put, say, n=1000000, the program with the first line is working[/color]
> well,[color=green]
> > whereas with the second it terminates with error. What is the maximum
> > possible array that can be defined this way? For n=100000 both[/color]
> declarations[color=green]
> > works fine.
> > Compiler g++.
> >
> > Thank you.
> >
> > Arthur
> >
> >[/color]
>
> double a[1000000]; is allocated on the stack, which probably causes the
> stack to overflow. maximum size, dont know, depends on the size of the
> stack, several Ks, Megs maybe???
>
> vector<double> a(1000000); is also allocated on the stack... but the array
> it used internally is allocated on the heap (free store). in other works,
> vector's internal variables (like current size, capacity, etc) are on the
> stack, together with a pointer to the actual data (which is stored on the
> heap). heap can hold far more data than the stack can. maximum size...[/color]
Total[color=blue]
> RAM - Allready Allocated RAM :-)
>
> hope that helps.
>
> Martin
>
>[/color] | 
July 19th, 2005, 06:56 PM
| | | | re: Difference in the array declaration
"Arthur Sinko" <asinko@msn.com> wrote in message news:7s9ab.18341$x21.3339@twister.southeast.rr.com ...[color=blue]
> Hi,
>
> Is it possible to answer the following question: what is the difference
> between two declarations:
>
> vector<double> a(n);
>
> and
>
> double a[n];
>
> When I put, say, n=1000000, the program with the first line is working well,
> whereas with the second it terminates with error. What is the maximum
> possible array that can be defined this way? For n=100000 both declarations
> works fine.[/color]
vector internally calls an allocator which by default just invokes new for the objets.
While the vector itself is an auto variable, it really only typically has a few pointers
inside it. The memory is dynamically allocated elsewhere.
The array tries to create the array wherever auto storage is kept ( usually on a runtime
stack). Auto variables typically have smaller maximum sizes than dynamic ones. | 
July 19th, 2005, 06:56 PM
| | | | re: Difference in the array declaration
"Arthur Sinko" <asinko@msn.com> wrote in message news:<7s9ab.18341$x21.3339@twister.southeast.rr.co m>...[color=blue]
> Hi,
>
> Is it possible to answer the following question: what is the difference
> between two declarations:
>
> vector<double> a(n);
>
> and
>
> double a[n];
>
> When I put, say, n=1000000, the program with the first line is working well,
> whereas with the second it terminates with error. What is the maximum
> possible array that can be defined this way? For n=100000 both declarations
> works fine.
> Compiler g++.
>
> Thank you.
>
> Arthur[/color]
to place a 1000000 element array on the heap do it like:
int n = 1000000;
double* a;
a = new double [n]; | 
July 19th, 2005, 06:57 PM
| | | | re: Difference in the array declaration
J. Campbell wrote:[color=blue]
>
> to place a 1000000 element array on the heap do it like:
>
> int n = 1000000;
> double* a;
> a = new double [n];[/color]
Why like that rather than like this:
std::vector<double> a(n);
?
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | 
July 19th, 2005, 07:00 PM
| | | | re: Difference in the array declaration
Kevin Goodsell <usenet1.spamfree.fusion@neverbox.com> wrote in message news:<Qeoab.8525$BS5.3069@newsread4.news.pas.earth link.net>...[color=blue]
> J. Campbell wrote:[color=green]
> >
> > to place a 1000000 element array on the heap do it like:
> >
> > int n = 1000000;
> > double* a;
> > a = new double [n];[/color]
>
> Why like that rather than like this:
>
> std::vector<double> a(n);
>
> ?
>
> -Kevin[/color]
well...the OP knows how to use vectors and arrays...he simply wanted
to know why his array ran out of space, while the vector didn't, so I
was showing how to put the array on the heap. | 
July 19th, 2005, 07:01 PM
| | | | re: Difference in the array declaration
J. Campbell wrote:[color=blue]
> Kevin Goodsell <usenet1.spamfree.fusion@neverbox.com> wrote in message news:<Qeoab.8525$BS5.3069@newsread4.news.pas.earth link.net>...
>[color=green]
>>
>>Why like that rather than like this:
>>
>>std::vector<double> a(n);
>>
>>?
>>
>>-Kevin[/color]
>
>
> well...the OP knows how to use vectors and arrays...he simply wanted
> to know why his array ran out of space, while the vector didn't, so I
> was showing how to put the array on the heap.[/color]
It's worth noting that using a std::vector has a number of advantages
over managing your own dynamically allocated array, even if you don't
plan on doing resizing:
1) You don't need to worry about delete[]ing it, thus preventing memory
leaks.
2) You don't need to worry about delete[]ing it, thus preventing the
possibility of undefined behavior if you make the all-too-common mistake
of using delete instead of delete[].
3) The size is stored automatically and can be retrieved using the
..size() function - you don't need to store it in a separate variable.
4) When invoking a standard algorithm, you can use the nice, clean
(vec.begin(), vec.end()) syntax for the sequence.
5) Exception safety - if an exception is thrown, the vector's destructor
will free its memory and destruct the objects it held. With an array
you'd have to either use a smart pointer (and you can't use auto_ptr for
this), or catch the exception, delete[] the array, and re-throw.
6) You have the option of checked indexing using the .at() member.
(And probably more)
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|