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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:08:48+00:00 2026-06-17T01:08:48+00:00

I have a question regarding the new/delete operator compared to alloc/free. When dynamicly allocating

  • 0

I have a question regarding the new/delete operator compared to alloc/free.

When dynamicly allocating memory for a char*, I normaly use calloc so all reserved bits are set to zero, so I don’t have to bother with nulltermination.

Does char *string = new char[20] only reserve 20 items with the size of a char or does it also sets all bits in that memoryarea to zero?

If not can memset be used on this memory to accomplish that?
And if memset or any other method of zeroing out the allocated memory is possibly, can the new operator be overloaded for the native char type to do this work on its own?

  • 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-17T01:08:49+00:00Added an answer on June 17, 2026 at 1:08 am

    When you use:

    string = new char[20];
    

    the allocated array is not initialized. In order to default-initialize it, you have to use:

    string = new char[20]();  // Allocates and initializes all members to 0
    

    This is stated in C++03 §5.3.4/15:

    A new-expression that creates an object of type T initializes that object as follows:
    …

    If the new-initializer is of the form (), the item is
    value-initialized (8.5);

    As for your second question — yes, you can use memset to fill the memory with any values you want.

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

Sidebar

Related Questions

I'm new to C++ and have a question regarding memory management. In the header,
I have a question regarding the new iPad 3 and iBooks. I'm trying to
I am new to chef and I have a question regarding the following. I
I'm new to version control and I have a question regarding branching. I have
I'm new with vim's taglist plugin and have a question regarding display style of
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I'm new to SharePoint and have a general question regarding list comparison. Currently, there
I have question regarding the use of function parameters. In the past I have
I have general question regarding the use of pointers vs. references in this particular
I am new to ASP.NET MVC and I have a question regarding viewing entity

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.