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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:35:35+00:00 2026-05-11T12:35:35+00:00

Yes, two hated constructs combined . Is it as bad as it sounds or

  • 0

Yes, two hated constructs combined. Is it as bad as it sounds or can it be seen as a good way to control usage of goto and also provide a reasonable cleanup strategy?

At work we had a discussion about whether or not to allow goto in our coding standard. In general nobody wanted to allow free usage of goto but some were positive about using it for cleanup jumps. As in this code:

void func() {    char* p1 = malloc(16);    if( !p1 )       goto cleanup;     char* p2 = malloc(16);    if( !p2 )       goto cleanup;   goto norm_cleanup;   err_cleanup:     if( p1 )       free(p1);     if( p2 )       free(p2);   norm_cleanup: } 

The abovious benefit of such use is that you don’t have to end up with this code:

void func() {    char* p1 = malloc(16);    if( !p1 ){       return;    }     char* p2 = malloc(16);    if( !p2 ){       free(p1);       return;    }     char* p3 = malloc(16);    if( !p3 ){       free(p1);       free(p2);       return;    } } 

Especially in constructor-like functions with many allocations this can sometimes grow very bad, not the least when someone has to insert something in the middle.

So, in order to be able to use goto, but still clearly isolate it from being used freely, a set of flow controlling macros was created for handling the task. Looks something like this (simplified):

#define FAIL_SECTION_BEGIN int exit_code[GUID] = 0; #define FAIL_SECTION_DO_EXIT_IF( cond, exitcode ) if(cond){exit_code[GUID] = exitcode; goto exit_label[GUID];} #define FAIL_SECTION_ERROR_EXIT(code) exit_label[GUID]: if(exit_code[GUID]) int code = exit_code[GUID];else goto end_label[GUID] #define FAIL_SECTION_END end_label[GUID]: 

We can use this as follows:

int func() {    char* p1 = NULL;    char* p2 = NULL;    char* p3 = NULL;     FAIL_SECTION_BEGIN    {       p1 = malloc(16);       FAIL_SECTION_DO_EXIT_IF( !p1, -1 );        p2 = malloc(16);       FAIL_SECTION_DO_EXIT_IF( !p2, -1 );        p3 = malloc(16);       FAIL_SECTION_DO_EXIT_IF( !p3, -1 );    }    FAIL_SECTION_ERROR_EXIT( code )    {       if( p3 )           free(p3);        if( p2 )           free(p2);        if( p1 )           free(p1);        return code;     }     FAIL_SECTION_END    return 0; 

It looks nice, and comes with many benefits, BUT, are there any drawbacks we should be thinking about before rolling this out into development? It is after all very flow controlling and goto:ish. Both are discouraged. What are the arguments for discouraging them in this case?

Thanks.

  • 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. 2026-05-11T12:35:36+00:00Added an answer on May 11, 2026 at 12:35 pm

    Error handling is one of the rare situations when goto is not so bad.

    But if I had to maintain that code I would be very upset that goto are hidden by macros.

    So in this case goto is OK for me but not macros.

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

Sidebar

Ask A Question

Stats

  • Questions 243k
  • Answers 243k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i don't think it's possible to create platform independent socket… May 13, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer I guess I do not really understand what you're aiming… May 13, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer You could have some form of handler (e.g. a .NET… May 13, 2026 at 7:51 am

Related Questions

So I have a simple voting feature on my asp.net mvc page. My index
Yes, the title doesn't make much sense, but here's my situation. I have two
I need to kill the yes command after some iterations, two methods: a) kill
I have two tables, each with three boolean (ms-access Yes/No) columns. Table 1: A1,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.