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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:33:47+00:00 2026-05-22T03:33:47+00:00

I’m writing a socket listener in C. Whenever it accepts, it creates a new

  • 0

I’m writing a socket listener in C. Whenever it accepts, it creates a new thread (detached) recv the data and process.

But everytime, I need to manually call close(socket_descriptor). Had it been C++, I could have in someway choose to close it inside a destructor.

I was trying to find out something that provides me same behaviour.
Basically, I want to create something like shared_ptr in C.

Is there anyway in C where one can get a signal or a notification when an object is going out of scope?

  • 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-22T03:33:47+00:00Added an answer on May 22, 2026 at 3:33 am

    No, that’s not an inherent feature of C … to enable something like what you’re asking for you would have to create some sort of call-back mechanism that would use a look-up table to match an object to its “destrutor”, and call that general call-back function at the end of every function you write, or more likely, at the end of every scope where an object was declared. I’d consider that very messy and complicated, and in the end, that’s probably much more hassle than it’s worth, i.e., you could simply use a goto and write all your clean-up code at the end of the function (I know in-general using goto‘s is considered very bad style, but for jumping to clean-up code at the end of a function, they can actually make the code much cleaner than the alternative which is to keep repeating the same clean-up code every time there’s an error).

    For example, suppose you had a function that allocated some memory and opened a file descriptor at some early point in the function, but then if there were some errors, could not continue … you can’t simply return an error code, you have to-do some clean-up, but it would be a pain to repeat the clean-up code over and over again right in the middle of the code:

    int my_function()
    {
        int* array_on_heap = malloc(ARRAYSIZE * sizeof(int));
        int file_descriptor;
    
        //...some code
    
        if (some_ptr == NULL)
            goto error_cleanup;
    
        //...some more code
    
        if (some_other_pointer != some_ptr)
            goto error_cleanup;
    
        //...even more code
    
        //normal return with no error
        return 0;
    
        error_cleanup:
            //clean-up code in case there was an error
            free(array_on_heap);
            close(fd);
            return -1;
    }
    

    This deliberate and judicious use of the goto here creates a very concise area of the function where destruction and clean-up can take place, and you don’t have to worry about possibly missing clean-up functionality had you decided to sprinkle clean-up code in the actual “mechanics” of the function itself.

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

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.