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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:36:35+00:00 2026-05-11T01:36:35+00:00

In c (or maybe c++) , what’s the difference between char myarr[16]={0x00}; and char

  • 0

In c (or maybe c++) , what’s the difference between

 char myarr[16]={0x00}; 

and

 char myarr[16]; memset(myarr, '\0', sizeof(myarr)); 

??

edit: I ask this because in vc++ 2005 the result is the same..
edit more : and

char myarr[16]={0x00,}; 

?
maybe can get more comprehensive answer and not ambiguous as some answers below refer to this kind of code,ie. put comma just before closing curly braces. Also the result is the same in vc++ 2005.

  • 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. 2026-05-11T01:36:36+00:00Added an answer on May 11, 2026 at 1:36 am

    The important difference is that the first default initializes the array in an element-specific manner: Pointers will receive a null pointer value, which doesn’t need to be 0x00 (as in all-bits-zero), booleans will be false. If the element type is a class type that’s not a so-called POD (plain old data-type), then you can only do the first one, because the second one only works for the simplest cases (where you don’t have virtual functions, user defined constructors and so on). In contrast, the second way using the memset sets all elements of the array to all-bits-zero. That is not always that what you want. If your array has pointers for example, they won’t be set to null-pointers necessarily.

    The first will default initialize the elements of the array, except for the first one, which is set to 0 explicitly. If the array is local and on the stack (that is, not a static), the compiler internally often does a memset to clear the array out. If the array is non-local or static, the first version can be considerably more efficient. The compiler can put the initializers already, at compile time, into the generated assembler code, making it require no runtime code at all. Alternatively, the array can be laid out on a section that is automatically zero’d out (also for pointers, if they have a all-bits-zero representation) when the program starts in a fast manner (i.e page-wise).

    The second does a memset explicitly over the whole array. Optimizing compilers will usually replace a memset for smaller regions with inline machine code that just loops using labels and branches.

    Here is assembler-code generated for the first case. My gcc stuff isn’t much optimized, so we got a real call to memset (16 bytes at the stack-top are always allocated, even if we got no locals. $n is a register number):

    void f(void) {     int a[16] = { 42 }; }  sub     $29, $29, 88 ; create stack-frame, 88 bytes stw     $31, $29, 84 ; save return address add     $4, $29, 16  ; 1st argument is destination, the array. add     $5, $0, 0    ; 2nd argument is value to fill add     $6, $0, 64   ; 3rd argument is size to fill: 4byte * 16 jal     memset       ; call memset add     $2, $0, 42   ; set first element, a[0], to 42 stw     $2, $29, 16  ; ldw     $31, $29, 84 ; restore return address add     $29, $29, 88 ; destroy stack-frame jr      $31          ; return to caller 

    The gory details from the C++ Standard. The first case above will default-initialize remaining elements.

    8.5:

    To zero-initialize storage for an object of type T means:

    • if T is a scalar type, the storage is set to the value of 0 (zero) converted to T;
    • if T is a non-union class type, the storage for each nonstatic data member and each base-class subobject is zero-initialized;
    • if T is a union type, the storage for its first data member is zero-initialized;
    • if T is an array type, the storage for each element is zero-initialized;
    • if T is a reference type, no initialization is performed.

    To default-initialize an object of type T means:

    • if T is a non-POD class type, the default constructor for T is called
    • if T is an array type, each element is default-initialized;
    • otherwise, the storage for the object is zero-initialized.

    8.5.1:

    If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (8.5).

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Ok I found a fix... I changed how I start… May 11, 2026 at 1:36 pm
  • added an answer Re the problem using AddressOf - if you know the… May 11, 2026 at 1:36 pm
  • added an answer Couldn't you just call input.getBorder() and cache it somewhere before… May 11, 2026 at 1:36 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.