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

  • SEARCH
  • Home
  • 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 564399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:45:44+00:00 2026-05-13T12:45:44+00:00

In C/C++, why are globals and static variables initialized to default values? Why not

  • 0

In C/C++, why are globals and static variables initialized to default values?

Why not leave it with just garbage values? Are there any special
reasons for this?

  • 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-13T12:45:45+00:00Added an answer on May 13, 2026 at 12:45 pm
    1. Security: leaving memory alone would leak information from other processes or the kernel.

    2. Efficiency: the values are useless until initialized to something, and it’s more efficient to zero them in a block with unrolled loops. The OS can even zero freelist pages when the system is otherwise idle, rather than when some client or user is waiting for the program to start.

    3. Reproducibility: leaving the values alone would make program behavior non-repeatable, making bugs really hard to find.

    4. Elegance: it’s cleaner if programs can start from 0 without having to clutter the code with default initializers.

    One might then wonder why the auto storage class does start as garbage. The answer is two-fold:

    1. It doesn’t, in a sense. The very first stack frame page at each level (i.e., every new page added to the stack) does receive zero values. The “garbage”, or “uninitialized” values that subsequent function instances at the same stack level see are really the previous values left by other method instances of your own program and its library.

    2. There might be a quadratic (or whatever) runtime performance penalty associated with initializing auto (function locals) to anything. A function might not use any or all of a large array, say, on any given call, and it could be invoked thousands or millions of times. The initialization of statics and globals, OTOH, only needs to happen once.

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

Sidebar

Related Questions

Possible Duplicate: Why global and static variables are initialized to their default values? What
Maybe I'm just not seeing it, but is there any other way to access
Are static variables stored on the stack itself similar to globals? If so, how
All the initialized global/static variables will go to initialized data section . All the
When C says start-up values of global[/static] variables are zero, does it mean also
Is there any way in PHP to do static code analysis and detect reliance
We say that global variables and static variables are always initialized to 0. Then
I have one static variable declared inside a function, which is not initialized to
I've just found this function definition in some embedded code: float round_float_to_4(static float inputval);
I have global static variables in a C library, which generate exceptions in a

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.