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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:23:26+00:00 2026-06-15T00:23:26+00:00

How can I zero a 2D array in C++? Do I need two for

  • 0

How can I zero a 2D array in C++? Do I need two for loops just for that?

Coming from other higher languages, I wonder why C++ doesn’t initialize arrays to meaningful/sensible defaults? Do I always need to declare an array then “zero” it out right afterwards?

  • 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-06-15T00:23:27+00:00Added an answer on June 15, 2026 at 12:23 am

    C++ language tries to follow the principle of “you don’t pay for what you don’t use”. It doesn’t initialize fundamental types to any default values because you might not want it to happen. In any case, the language provides you the opportunity to explicitly request such initialization.

    At the moment of declaration you can use initializers and simply do this

    int array[10][20] = {};
    

    or, for a dynamically allocated array

    int (*array)[20] = new int[10][20]();
    

    and this will give a zero-initialized array. No loops necessary.

    But if you want to zero-out an existing array, then you will indeed have to use something more elaborate. In case of integer types a good old memset will work. For pointer or floating-point types the situation is in general case more complicated. memset might work or it might not work, depending on the implementation-defined properties. In any case, the standard library can help you to reduce the number of explicit loops by providing such loop wrappers as std::fill.

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

Sidebar

Related Questions

I just learned the hard way that IntPtr.Zero cannot be compared to default(IntPtr). Can
How can I set my countdown correctly? I'm counting from 33,000.00 to zero. It
I am comparing two binary arrays. I have an array where values can either
I need to perform some operations on a 2D array of values read from
I have an array that could contain zero values. $array = array(3=> 1000, 5=>
How can I modify my method to check only indexes that are equal zero
I am trying to set a negative zero to an array in two ways.
I need to create a CImage from a byte array (actually, its an array
I need to iterate through the elements in a numpy array so I can
I have Table1 and Table2 related on Table1.ID. There can be zero or more

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.