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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:55:22+00:00 2026-05-13T05:55:22+00:00

Recently I came across a gcc extension that I have found rather useful: __attribute__(cleanup)

  • 0

Recently I came across a gcc extension that I have found rather useful: __attribute__(cleanup)

Basically, this allows you to assign a cleanup call to a local variable at the time it exits scope. For instance, given the following section of code, all memory must be maintained and handled explicitly in any and all cases within the call to foo.

void foo() {
   char * buff = ...; /* some memory allocation */
   char * buff2 = 0, * buff3 = 0;
   if (! buff) {
      return;
   } else {
      buff2 = ...; /* memory allocation */
      if (! buff2) {
         goto clean_exit;
      } else {
         /* ... and so on ... */
      }
   }

clean_exit:
   free (buff);
   free (buff2);
   free (buff3);
}

However, by using the extension that can reduce to

#define clean_pchar_scope __attribute__((cleanup(pchar_free)))

void pchar_free (char ** c) { free (*c); }

void foo () {
   char * buff clean_pchar_scope = ...; /* some memory allocation */
   char * buff2 clean_pchar_scope = 0, * buff3 clean_pchar_scope = 0;
   if (! buff)
      return;
   buff2 = ...; /* memory allocation */
   if (! buff2)
      return;
   /* and so on */
}

Now all memory is reclaimed on the basis of scope without the use of nested if/else or goto constructs coupled with a consolidated memory release section of the function. I realize that the use of goto could be avoided there for a more nested if/else construct (so, please, no holy wars on the goto…) and that the example is contrived, but the fact remains that this is can be quite a useful feature.

Unfortunately, as far as I know, this is gcc-specific. I’m interested in any portable ways to do the same thing (if they even exist).
Has anyone had experiences in doing this with something other than gcc?

EDIT:
Seems that portability is not in play. Considering that, is there a way to do this outside of the gcc space? It seems like to nice a feature to be gcc-specific…

  • 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-13T05:55:23+00:00Added an answer on May 13, 2026 at 5:55 am

    There’s no portable way in C.

    Fortunately it’s a standard feature of C++ with destructors.

    Edit:

    MSVC appears to have __try and __finally keywords which can be used for this purpose as well. This is different than the C++ exception handling and I think it’s available in C.

    I think you’ll find that cleanup and try/finally aren’t widely used specifically because of the implicit support in C++, which is “close enough” to C that people interested in the behavior can switch their code to C++ with ease.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The hypervisor LDom from SUN can be used on a… May 13, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer Does it have to be a web API? Typically you'd… May 13, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer This works: <s:Group id="windmill" width="50" height="200" x="400" y="400"> <s:Group id="windmillBlades"… May 13, 2026 at 4:56 pm

Related Questions

Recently I came across a situation where, while using a double-byte language, I had
From my experience I have learn that using an surrogate INT data type column
I recently came across a great data structures book, Data Structures Using C (c)
I recently came across a ASP 1.1 web application that put a whole heap
I recently came across a use of the @import rule on Coda.com. They're actually

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.