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

Difference in the array declaration

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
Jul 19 '05 #1
7 5443
"Arthur Sinko" <as****@msn.com> wrote in message
news:7s******************@twister.southeast.rr.com ...
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


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
Jul 19 '05 #2
Thank you very much.

Arthur

"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@uni00nw.unity.ncsu.edu...
"Arthur Sinko" <as****@msn.com> wrote in message
news:7s******************@twister.southeast.rr.com ...
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


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

Jul 19 '05 #3

"Arthur Sinko" <as****@msn.com> wrote in message news:7s******************@twister.southeast.rr.com ...
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.


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.
Jul 19 '05 #4
"Arthur Sinko" <as****@msn.com> wrote in message news:<7s******************@twister.southeast.rr.co m>...
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


to place a 1000000 element array on the heap do it like:

int n = 1000000;
double* a;
a = new double [n];
Jul 19 '05 #5
J. Campbell wrote:

to place a 1000000 element array on the heap do it like:

int n = 1000000;
double* a;
a = new double [n];


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.

Jul 19 '05 #6
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<Qe*****************@newsread4.news.pas.earth link.net>...
J. Campbell wrote:

to place a 1000000 element array on the heap do it like:

int n = 1000000;
double* a;
a = new double [n];


Why like that rather than like this:

std::vector<double> a(n);

?

-Kevin


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.
Jul 19 '05 #7
J. Campbell wrote:
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<Qe*****************@newsread4.news.pas.earth link.net>...

Why like that rather than like this:

std::vector<double> a(n);

?

-Kevin

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.


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.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul | last post by:
Say I have: char foo; char** bar; In my understanding, these two have the same meaning in memory. Could someone elucidate upon these two types and their meanings? Thanks, Paul
17
by: ambar.shome | last post by:
Can anyone tell me : 1. void main() { char temp = "Hello"; temp='K';
10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
11
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.