Program file bigger when using vector. |
So?
My question is : Is this correct ? The difference in program size seems like a
lot. |
‘This’ what? And: is 60 Kb a lot?
Loading large executables into memory could become expensive, but for example g++.exe is 1104 Kbytes and in my old notebook starts in a blink. A GUI program will load a lot of libraries and other executables, but, for what I can see, everybody wants GUI programs nowadays. What are your concerns?
two minimal examples … so we can compile it |
I think “so we can compile it” meant it should be compilable - your code doesn’t compile even after having added
#include <array> and
#include <vector>.
Anyway, this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
// #include <array>
#include <vector>
// 3 x 4 ( rows of ints )
const std::vector<std::vector<int>> minivectorgc {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
// extra braces and inverted indices yay !
// const std::array<std::array<int, 3>, 4> miniarraygc /* 3 x 4 ints */
// ^ ^ This is not consistent with
// your initialization
// \/ \/
// const std::array<std::array<int, 4>, 3> miniarraygc { {
// { 1, 2, 3, 4 },
// { 5, 6, 7, 8 },
// { 9, 10, 11, 12 }
// } };
int main()
{
return 1;
}
|
compiled this way:
g++.exe -std=c++2a -Wall -Wextra -pedantic-errors main.cpp -o main.exe
in my W7 machine creates an .exe 525,133 bytes big, while this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <array>
// #include <vector>
// 3 x 4 ( rows of ints )
// const std::vector<std::vector<int>> minivectorgc {
// { 1, 2, 3, 4 },
// { 5, 6, 7, 8 },
// { 9, 10, 11, 12 }
// };
// extra braces and inverted indices yay !
// const std::array<std::array<int, 3>, 4> miniarraygc /* 3 x 4 ints */
// ^ ^ This is not consistent with
// your initialization
// \/ \/
const std::array<std::array<int, 4>, 3> miniarraygc { {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
} };
int main()
{
return 1;
}
|
creates an .exe 85,869 bytes big.
The difference is 439,264 bytes, that is 429 Kbytes (428.96875).
If I check my ‘vector’ file in …\include\c++\8.1.0, I can see it is 2,747 byte big and includes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <bits/stl_algobase.h> // 50,464 bytes
#include <bits/allocator.h> // 7,569 bytes
#include <bits/stl_construct.h> // 7,397 bytes
#include <bits/stl_uninitialized.h> // 27,649 bytes
#include <bits/stl_vector.h> // 60,488 bytes
#include <bits/stl_bvector.h> // 33,703 bytes
#include <bits/range_access.h> // 10,030 bytes
// total 197,300 bytes
#ifndef _GLIBCXX_EXPORT_TEMPLATE
# include <bits/vector.tcc>
#endif
#ifdef _GLIBCXX_DEBUG
# include <debug/vector>
#endif
#ifdef _GLIBCXX_PROFILE
# include <profile/vector>
#endif
|
while ‘array’ is 11,657 bytes, but includes:
1 2 3 4 5 6 7 8 9
|
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#include <utility> // 12,358 bytes
#include <stdexcept> // 7,975 bytes
#include <bits/stl_algobase.h> // 50,464 bytes
#include <bits/range_access.h> // 10,030 bytes
// total 80,827 bytes
|
197,300 + 2,747 = 200,047
80,827 + 11,657 = 92,484
200,047 - 92,484 = 107,563 bytes --> 105 Kb (105.04199219).
Perhaps just this very, very naive analysis accounts for nearly the 25% of the size difference between the two .exes.