about the array and vector

R

remlostime

now, i write some code
code1:
int a[10000000];
int main(){}

code2:
vector<int> a(10000000);
int main(){}

after using g++ compile, and run it, code1 is broken, but code2 runs
well, why?
i think the size of a is the same in code1 and code2, but only vector
can run big size, how can i
use big size in array?
 
J

James Kanze

now, i write some code
code1:
int a[10000000];
int main(){}
code2:
vector<int> a(10000000);
int main(){}
after using g++ compile, and run it, code1 is broken, but
code2 runs well, why?

Who knows? Both work perfectly well on my machine, with either
g++ or Sun CC (under Solaris) or VC++ (under Windows). However,
the two programs do use resources differently, so it is
conceivable that one works and the other crashes on some
implementations. (Basically, the first uses static memory, the
second acquires its memory on the heap.)

If I modify the program so that the arrays are local variables,
then the first does fail under Windows (but not under Solaris,
regardless of the compiler). This change causes the memory for
the first version to be taken from yet another source: the
stack; some OS's limit stack size. (Under Unix, you can often
limit it artificially, by means of ulimit. One reason for doing
this is to cause a program with infinite recursion to crash
before it uses up all of the system resources, and causes the
system to start thrashing.)
 
J

Juha Nieminen

remlostime said:
now, i write some code
code1:
int a[10000000];
int main(){}

code2:
vector<int> a(10000000);
int main(){}

after using g++ compile, and run it, code1 is broken, but code2 runs
well, why?

Your OS or compiler have some weird settings. The OS usually reserves
certain amount of memory for the heap, the stack and the global
variables of every process. If the first code is crashing it means that
the OS is not giving it enough space for that global array, for some reason.
This is sometimes a problem if you put such a huge array inside a
function (in which case it's allocated on the stack). However, that
problem is usually solved by defining it outside the function (usually
inside a nameless namespace, for cleaniness).
 
R

remlostime

now, i write some code
code1:
int a[10000000];
int main(){}

code2:
vector<int> a(10000000);
int main(){}

Now, i have compiled both of them, and i write another
code1:
int main()
{
int a[10000000];
return 0;
}

code2:
#include <vector>
int main()
{
vector<int> a(10000000);
return 0;
}
and u can run it, code1 can't run, but code2 runs well, why vector can
work?
 
R

remlostime

remlostime said:
now, i write some code
code1:
int a[10000000];
int main(){}
code2:
vector<int> a(10000000);
int main(){}
after using g++ compile, and run it, code1 is broken, but code2 runs
well, why?
i think the size of a is the same in code1 and code2, but only vector
can run big size, how can i
use big size in array?

As others here have noted, your compiler likely isn't
allocating enough global memory to handle Code1.

For information purposes, my favoritie compiler, DJGPP
(DJ Delorie's port of g++ to the 32-bit Windows command-prompt)
compiles and runs those both fine (at least when I put in the
missing headers, the missing "std::", etc).

Oddly, your Code1 takes an exhorbitant amount of time to compile,
about 60 seconds, whereas your Code2 only takes 2 seconds. Unknown
why the extreme difference in compile time. May have something to
do with the necessity of allocating over 40 million bytes of global
memory.

But both version DO compile and run. I get:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code1:

#include <iostream>
int a[10000000];
int main (void)
{
std::cout << "sizeof a = " << sizeof a << std::endl;
return 0;

}

cwd = C:\RHE\src\test
$make a-array-test.exe
Using pattern rule %.exe:%.cpp to compile a-array-test.cpp to a-array-test.exe:
gpp -I c:/rhe/src/test -I C:/RHE/include -Wall -Os -s a-array-test.cpp -LC:/RHE/
lib -lrh -lfl -ly -lm -lname -o C:/bin-test/a-array-test.exe
a-array-test.exe is up to date.

cwd = C:\RHE\src\test
$a-array-test
sizeof a = 40000000

cwd = C:\RHE\src\test
$

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code2:

#include <iostream>
#include <vector>
std::vector<int> a(10000000);
int main (void)
{
std::cout << "sizeof a = " << sizeof a << std::endl;
return 0;

}

cwd = C:\RHE\src\test
$make a-vector-test.exe
Using pattern rule %.exe:%.cpp to compile a-vector-test.cpp to a-vector-test.exe
:
gpp -I c:/rhe/src/test -I C:/RHE/include -Wall -Os -s a-vector-test.cpp -LC:/RHE
/lib -lrh -lfl -ly -lm -lname -o C:/bin-test/a-vector-test.exe
a-vector-test.exe is up to date.

cwd = C:\RHE\src\test
$a-vector-test
sizeof a = 12

cwd = C:\RHE\src\test
$

Note that the array takes up 40MB of global memory, whereas
the vector only takes 12 bytes, with the remainder being
dynamically allocated. So the ways in which Code1 and Code2
use memory are DRASTICALLY different, hence I wouldn't be
surprised at all if they behave differently on some platforms.

I'd strongly recommend the vector version for several reasons:
1. Cleaner-looking code.
2. Less error prone.
3. Built-in range checking available.
4. Memory allocation handled for you.
5. Doesn't strain global memory space as much.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

yes, but the speed of vector is slower than array, because if i use
the size bigger than the vector, it will require a new space and copy
all items into it, so how i can use vector in an efficient way?
 
K

Kai-Uwe Bux

remlostime said:
yes, but the speed of vector is slower than array, because if i use
the size bigger than the vector, it will require a new space and copy
all items into it,

That is a weird statement: you complain that a vector is slower than an
array when performing an operation that the array cannot perform at all.
so how i can use vector in an efficient way?

E.g., by not resizing it. Have a look into reserve() or use a constructor to
allocate the correct size right away.


Best

Kai-Uwe Bux
 

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,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top