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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:20:57+00:00 2026-05-16T18:20:57+00:00

gcc 4.4.3 c89 I have some functions that initialize some hardware and return either

  • 0

gcc 4.4.3 c89

I have some functions that initialize some hardware and return either true or false. If false then I have to uninitialize in the reverse order.

However, my code is looking very untidy with all the if statements.

For example each function can return either true of false. This is a sample. As you can see the code looks very untidy. I am just looking for any advice on how I can clean it up to make it more manageable and if possible scable?

Many thanks for any advice,

if(init_A() == TRUE) {
 if(init_B() == TRUE) {
  if(init_C() == TRUE) {
   if(init_D() == TRUE) {
    if(init_E() == TRUE) {
     /* ALL STARTED OK */    
    }
    else {
     uninit_A();
     uninit_B();
     uninit_C();   
     uninit_D();    
    }
   }
   else {
    uninit_A();
    uninit_B();
    uninit_C();   
   }
  }
  else {
   uninit_A();
   uninit_B();
  }
 }
 else {
  /* Failed to initialize B */
  uninit_B(); 
 }
}
else {
 /* Failed to start */
}
  • 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-16T18:20:58+00:00Added an answer on May 16, 2026 at 6:20 pm

    This is quite a common problem, where the “init” steps correspond to things like malloc() or lock(), and the “uninit” steps correspond to things like free() and unlock(). It is particularly an issue when resources have to be deallocated in strictly the reverse order in which they were allocated.

    This is one case where the use of goto is justified:

    int somefunc()
    {
        int retval = ERROR;
    
        if (init_A() != TRUE)
            goto out_a;
    
        if (init_B() != TRUE)
            goto out_b;
    
        if (init_C() != TRUE)
            goto out_c;
    
        if (init_D() != TRUE)
            goto out_d;
    
        if (init_E() != TRUE)
            goto out_e;
    
        /* ALL STARTED OK */
        /* ... normal processing here ... */
        retval = OK;
    
        uninit_E();
      out_e:
        uninit_D();
      out_d:
        uninit_C();
      out_c:
        uninit_B();
      out_b:
        uninit_A();
      out_a:
        return retval;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.4 c89 I have seen this in some code I am maintaining. #define
gcc 4.4.2 c89 I am have been re-engineering some one else's source code. In
gcc 4.4.4 c89 I am reading in a file and in that file I
gcc 4.4.2 c89 I re-engineering some code in c89. However, I am totally confused
gcc 4.4.3 c89 I have the following code as a sample of what I
gcc 4.4.2 c89 I was just working on some pointers. However, with the program
GCC compiles (using gcc --omit-frame-pointer -s ): int the_answer() { return 42; } into
Is it possible for gcc to link against a library that was created with
I’m using the gcc in MinGW that comes with Strawberry Perl, on Windows XP.
gcc 4.4.4 c89 I am just wondering is it worth passing a const into

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.