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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:38:26+00:00 2026-05-25T02:38:26+00:00

I am writing a function which has many return points (depending on what conditions

  • 0

I am writing a function which has many return points (depending on what conditions are true) and it does a bunch of news at different points. At every return point, the set of deletes I need to do are different. And even then, I don’t want to do them at different places. What is a good way to handle this?

Some details: Let us consider a function:

int MainClass::testFunction() {
  numThreads++;
  char *a = new char[100];
  // Some computation with a
  if (condition1) {
    numThreads--;
    return -1;
  } 
  char *b = new char[100];
  // Some computation with b
  if (condition2) {
    numThreads--;
    return 42;
  } 
  // Some more stuff. 
  numThreads--;
}

Now, before return -1, I need to do delete a while before return 42, I need to do delete a; delete b;. And you can imagine how this could get complicated if I had multiple return points.

Here are my solutions:

First solution: Put all deletes at the end of the function, put some labels, store the return value at return points, and use goto (yes, that dirty word!) to jump to appropriate deletes and return after executing those deletes.

In the above example, I could say

superset:
  delete b;
subset:
  delete a;
numThreads--;

And put goto superset before return 42 and goto subset before return -1.

I don’t like the solution for obvious reasons! 🙂

Second solution: I can build an inner class instance, and do new on variables of that class instance. Like this…

int MainClass::testFunction() {
  class Local {
    char *a, *b;
    Local () : a(NULL), b(NULL) {}
    ~Local () { if (a != NULL) delete a; if (b != NULL) delete b; }
  };
  Local l = Local();
  l.a = new char[100];
  // Some computation with a
  if (condition1) {
    return -1;
  } 
  l.b = new char[100];
  // Some computation with b
  if (condition2) {
    return 42;
  } 
}

So, what is the problem? Well, how do I access numThreads variable that was in the method? I want to do numThreads++ in the constructor for Local and numThreads-- in the dtor of Local. If it helps, testFunction is also a member function of another class.

Thanks for reading.

Update: Allocating the array on stack is definitely one possibility, but I have run into stack overflow (ah… the name of this website :-)) doing large allocations on stack (stack size for threads is 2MB by default). What I wanted to address in this question was general resource acquisition and destruction.

  • 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-25T02:38:27+00:00Added an answer on May 25, 2026 at 2:38 am

    Um. You’re WAY overcomplicating this. Just set a and b to NULL at the beginning. Surround the whole thing with a try / catch. If anything goes wrong, throw something (anything will do). In the catch block, call delete on all of the variables. delete will do nothing if the variable is NULL.

    And if you need to do stuff with return variables, you could theoretically just throw the return value (yes, you can throw ints). Then when you’re done cleaning up, just return the thrown value.

    char *a = NULL, *b = NULL;
    
    try
    {
        a = ...;
        if(bad_thing)
            throw -1;
    
        b = ...;
        throw 42;
    }
    catch(int e)
    {
        delete[] a;
        delete[] b;
        return e;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a MATLAB application which has many functions spread over different files. I
I am in the middle of writing a program which has a function which
I am writing a function in which I need to read a string contains
I'm writing a construct in PHP where a parser determins which function to call
I am writing a functor F which takes function of type void (*func)(T) and
I have a jQuery selector, which has a chained function. Inside the function I
I am writing a debug function, which prints a variable name, and its value.
I'm writing a C/C++ DLL and want to export certain functions which I've done
I'm writing a set of functions in c++ which can be called by excel.
I am writing a library which uses a few functions from the windows user32.dll

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.