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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:24:51+00:00 2026-06-01T01:24:51+00:00

For example, I need to malloc two pieces of memory, so: void *a =

  • 0

For example,
I need to malloc two pieces of memory, so:

void *a = malloc (1);

if (!a)
  return -1;

void *b = malloc (1);

if (!b)
{
  free (a);
  return -1;
}

Notice if the second malloc fails, I have to free “a” first. The problem is, this can be very messy if there are many such malloc’s and error checking’s, unless I use the notorious “goto” clause and carefully arrange the order of free’s along with the labels:

void *a = malloc (1);

if (!a)
  goto X;

void *b = malloc (1);

if (!b)
  goto Y;

return 0; //normal exit

Y:
  free (a);
X:
  return -1;

Do you have any better solution to this situation? Thanks in advance.

  • 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-01T01:24:53+00:00Added an answer on June 1, 2026 at 1:24 am

    We do like this:

    void *a = NULL;
    void *b = NULL;
    void *c = NULL;
    a = malloc(1);
    if (!a) goto errorExit;
    b = malloc(1);
    if (!b) goto errorExit;
    c = malloc(1);
    if (!b) goto errorExit;
    
    return 0;
    errorExit:
    //free a null pointer is safe.
    free(a);
    free(b);
    free(c);
    return -1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I need to disable two buttons in runtime. After I disabled first
I have read that free() generally does not return memory to the OS. Can
For example I need to grab from http://gmail.com/ the number of free storage: Over
I need to have : http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/ so : (1) remove the
A specific example I need to generate a random number between 0 and 2,
I need an example algorithm that will draw pixels one at a time on
I need an example of the shortest path of a directed cyclic graph from
i need an example of the shortest path of directed graph cycle bye one
I need a basic example of how to use the IComparable interface so that
I need a detailed example about session in asp.net using c# For example

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.