Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:28:18+00:00 2026-05-28T19:28:18+00:00

If I define and initialize a large vector globally, then will its size be

  • 0

If I define and initialize a large vector globally, then will its size be included in object file?

for e.g

case 1: If I have an uninitialized large global array x, its size correctly shows up in bss segment size but it will not be added to object file size as it is uninitialized data , this is expected.

#include <iostream>
#define SIZE 200000000

char x[SIZE];

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762            572        200000040        200002374        bebcb46        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 7477 Jan 28 02:52 a.out

case 2: similarly if I have large initialized global array, its size will be included in data segment(not in bss), and it will also reflect in size of object file as expected.

#include <iostream>
#define SIZE 200000000

//remaining entries will be value initialized
char x[SIZE] = { 'a', 'b' };

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762        200000600             24        200002386        bebcb52        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 200007533 Jan 28 02:34 a.out

case 3: Now if instead of initialized global array I use a initialized large global vector, then I am expecting a behaviour like case 2 (obj file size includes initialized array size) but instead
I get following behaviour

#include <iostream>
#include <vector>
#define SIZE 200000000

std::vector<char> x(SIZE, 'c');

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
4936            604             48           5588           15d4        a.out

$ls -l a.out

 -rwxrwxr-x. 1 ur ur 13583 Jan 28 02:44 a.out

Can anyone please explain this behaviour, that why initialized vector is not present in object file, and how a globally defined vector is initialized at run time. I think I am missing something fundamental here. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:28:19+00:00Added an answer on May 28, 2026 at 7:28 pm

    std::vector allocates its storage on the heap, at runtime. If you define one as a global variable like that, its constructor will run sometime before main() starts. With the arguments you gave it, the constructor will allocate space for SIZE characters on the heap and then run a loop to store the letter c into each of them.

    All that’s present in your object file is the single copy of the letter c that’s passed to the vector’s constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to define and initialize some variables in web.xml and the access
I define a 'block' of text as all lines between start of file, newline
code: #define f(a,b) a##b #define g(a) #a #define h(a) g(a) main() { printf(%s\n,h(f(1,2))); //[case
I have a struct that's defined with a large number of vanilla char* pointers,
In C++ how initialize all values of a member array for a class? #define
I have my web applicatoin initialize method run based upon the page that is
I have an applet that allows the user to define values for 10 variables,
I'm searching for a fast, in-memory database that allows one to define, initialize &
I have to write a very large test suite for a complex set of
SockeClient.h file #define SIZE_OF_BUFFER 4096 class SocketClient { public: SocketClient(int cfd); virtual ~SocketClient(); int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.