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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:53:59+00:00 2026-05-26T17:53:59+00:00

From what I understand, I have to choose between calloc , which will allocate

  • 0

From what I understand, I have to choose between calloc, which will allocate zeroed memory, and malloc, which can allocate memory on demand.

Is there a function that combines both those properties? Maybe direct call to mmap?

If it’s possible, why calloc doesn’t do it?

  • 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-26T17:54:00+00:00Added an answer on May 26, 2026 at 5:54 pm

    There are a few mechanisms to get pre-zeroed memory from the operating system:

    mmap(2)‘s MAP_ANONYMOUS flag forces the contents to be initialized to zero.

    The POSIX shared memory segments can also zero

    • shm_open(3) provides you with a file descriptor
    • ftruncate(2) the “file” to the size you want
    • mmap(2) the “file” into your address space

    The memory comes pre-zeroed:

       This volume of IEEE Std 1003.1-2001 specifies that memory
       objects have initial contents of zero when created. This is
       consistent with current behavior for both files and newly
       allocated memory. For those implementations that use physical
       memory, it would be possible that such implementations could
       simply use available memory and give it to the process
       uninitialized. This, however, is not consistent with standard
       behavior for the uninitialized data area, the stack, and of
       course, files. Finally, it is highly desirable to set the
       allocated memory to zero for security reasons. Thus,
       initializing memory objects to zero is required.
    

    It appears that this memory is zeroed at use: mm/shmem.c function shmem_zero_setup():

    /**
     * shmem_zero_setup - setup a shared anonymous mapping
     * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
     */     
    int shmem_zero_setup(struct vm_area_struct *vma)
    {   
        struct file *file;
        loff_t size = vma->vm_end - vma->vm_start;
    
        file = shmem_file_setup("dev/zero", size, vma->vm_flags);
        if (IS_ERR(file))
            return PTR_ERR(file);
    
        if (vma->vm_file)
            fput(vma->vm_file);
        vma->vm_file = file;
        vma->vm_ops = &shmem_vm_ops;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand you can choose from a range of bindings, such as TCP, HTTP,
From what I understand, in TDD you have to write a failing test first,
I have read about session fixation and from what I understand it forces a
I have got a problem with GridView because I do not understand how can
I am using DBus in a project. I understand from DBus specification that for
From what I understand, the parent attribute of a db.Model (typically defined/passed in the
From what I understand, due to the same origin policy enforcement in current browsers,
From what I understand of the SDK, this exception is raised when the bindings
From what I understand of REST principles, URLs should represent a single resource, like
From what I understand, the best way to deal with dates in the Zend

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.