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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:47:12+00:00 2026-05-18T08:47:12+00:00

Ignoring multithreading issues, is the following guaranteed to work: int can_alloc(size_t i) { void

  • 0

Ignoring multithreading issues, is the following guaranteed to work:

int can_alloc(size_t i)
{
    void *p = malloc(i);
    if(p == NULL) return 0;
    free(p);
    return 1;
}

// later
if(can_alloc(10))
{
    char *c = malloc(10); // no need to verify, we already did?
    memcpy(c, "something", 10);
}

This is mostly out of curiosity. I have no plans to use this for anything, but I believe it should be guaranteed to work, and it would be informative to know for sure.

  • 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-18T08:47:13+00:00Added an answer on May 18, 2026 at 8:47 am

    No. Even without multi-threading, the malloc call is obtaining (memory) resources from the OS. Usually (Windows, Linux, Mac, etc) the OS can do things that affect the available resources while your program is executing – at any time. That means between your check and your actual allocation, the memory might become ‘unavailable’.

    If you have unusually complete control of the OS, then it might be possible to make this robust – but it would be extremely tricky.

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

Sidebar

Related Questions

Ignoring other possiblities such as Request.ServerVariables[HTTP_X_FORWARDED_FOR] , does Request.ServerVariables[REMOTE_ADDR] gives me the principal IP
Rails seems to be ignoring authenticity tokens for AJAX requests. For instance, I purposely
Safari 4 seems to be ignoring element margins unless I add a border. The
I've read several posts here about ignoring files in Mercurial but I'm stumped on
The .gitignore file is very useful in ignoring some of the files that we
I am fetching a web page on Android using HTTPS (ignoring the certificate as
I want to extract the introduction part of a wikipedia article(ignoring all other stuff,
How can I compare 2 strings in C# ignoring the case, spaces and any
I am using Mvvm-Light and have been ignoring how binding in XAML really works

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.