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 6356595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:59:34+00:00 2026-05-24T22:59:34+00:00

Does STL default allocator zero raw memory before placing objects into it? See this

  • 0

Does STL default allocator zero raw memory before placing objects into it? See this code. The comments reflect the behavior on my platform.

#include <iostream>
#include <vector>

struct Foo
{
    Foo() {} // n isn't initialized 
    int n;
};

int main()
{
    std::vector<Foo> v(2); // zeroed
    std::cout << v[0].n << '\n';
    std::cout << v[1].n << '\n';

    Foo foo; // contains garbage
    std::cout << foo.n << '\n';
}

Is it possible to disable zeroing raw memory? Note, it isn’t the same as value initializing POD.

  • 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-24T22:59:35+00:00Added an answer on May 24, 2026 at 10:59 pm

    A container will default-initialize the objects it creates if you don’t give them a specific value. In your case the default constructor for the object does not initialize the POD integer, so it will contain whatever was left over in memory.

    Sometimes new heap blocks will be zero initialized by the OS, but you can’t even count on that. A block can get reused and again it will contain leftover garbage from the last time it was used.

    Code that is ultra sensitive to exploits will take care to zero out memory to critical variables such as passwords before destroying them.

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

Sidebar

Related Questions

Does the C++ Standard say I should be able to compare two default-constructed STL
How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
Why does the C++ STL not provide any tree containers, and what's the best
Does anyone know why the STL containers don't have virtual destructors? As far as
Does the std::stack in the C++ STL expose any iterators of the underlying container
How does container object like vector in stl get destroyed even though they are
Using C++ and the STL, does anybody know how to store integer arrays as
For instance why does most members in STL implementation have _M_ or _ or
How does one go about sorting a vector containing custom (i.e. user defined) objects.
How does STL algorithm work independent of Iterator type?

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.