Difference in the array declaration

A

Arthur Sinko

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
 
M

Marcin Vorbrodt

Arthur Sinko said:
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
 
A

Arthur Sinko

Thank you very much.

Arthur

Marcin Vorbrodt said:
Arthur Sinko said:
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
 
R

Ron Natalie

Arthur Sinko said:
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.
 
J

J. Campbell

Arthur Sinko said:
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];
 
K

Kevin Goodsell

J. Campbell said:
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
 
J

J. Campbell

Kevin Goodsell said:
J. Campbell said:
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.
 
K

Kevin Goodsell

J. Campbell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top