By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

about the array and vector

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

code2:
vector<inta(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?
Jul 21 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jul 21, 5:15 am, remlostime <remlost...@gmail.comwrote:
now, i write some code
code1:
int a[10000000];
int main(){}
code2:
vector<inta(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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34
Jul 21 '08 #2

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

code2:
vector<inta(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).
Jul 21 '08 #3

P: n/a

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

code2:
vector<inta(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<inta(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
Jul 22 '08 #4

P: n/a
On 7月21日, 上午11时15分, remlostime <remlost...@gmail.comwrote:
now, i write some code
code1:
int a[10000000];
int main(){}

code2:
vector<inta(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<inta(10000000);
return 0;
}
and u can run it, code1 can't run, but code2 runs well, why vector can
work?

Jul 22 '08 #5

P: n/a
On 7月22日, 上午8时14分, "Robbie Hatley"
<see.my.signat...@for.my.email.addresswrote:
"remlostime" wrote:
now, i write some code
code1:
int a[10000000];
int main(){}
code2:
vector<inta(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<inta(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?
Jul 22 '08 #6

P: n/a
remlostime wrote:
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
Jul 22 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.