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 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 padding/alignment issues and given the following struct, what is best way to get
lint produces some warning like: foo.c XXX Warning 534: Ignoring return value of function
i am seeking help on ignoring null values for updating the mysql database:- $cst
I'm working on some code that generates a lot of ignoring return value of
I'm having serious trouble debugging the following Matlab Warning: Warning: Ignoring excess mask dialog
I am doing some simple work with uploading a file. I am ignoring error
Ignoring the special libraries that allow you to work with very big numbers, what's
The following is ignoring the date range. Please could someone explain what is going
Mac seems to be ignoring new changes to my hosts file. Older changes work
Ignoring the IE case, are there any other browsers that can't understand the application/xhtml+xml

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.