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

The Archive Base Latest Questions

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

Currently I am working on a project where goto statements are heavely used. The

  • 0

Currently I am working on a project where goto statements are heavely used. The main purpose of goto statements is to have one cleanup section in a routine rather than multiple return statements. Like below:

BOOL foo() {    BOOL bRetVal = FALSE;    int *p = NULL;     p = new int;    if (p == NULL)    {      cout<<' OOM \n';      goto Exit;    }     // Lot of code...  Exit:    if(p)    {      delete p;      p = NULL;    }    return bRetVal; } 

This makes it much easier as we can track our clean up code at one section in code, that is, after the Exit label.

However, I have read many places it’s bad practice to have goto statements.

Currently I am reading the Code Complete book, and it says that we need to use variables close to their declarations. If we use goto then we need to declare/initialize all variables before first use of goto otherwise the compiler will give errors that initialization of xx variable is skipped by the goto statement.

Which way is right?


From Scott’s comment:

It looks like using goto to jump from one section to another is bad as it makes the code hard to read and understand.

But if we use goto just to go forward and to one label then it should be fine(?).

  • 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-11T00:35:11+00:00Added an answer on May 11, 2026 at 12:35 am

    I am not sure what do you mean by clean up code but in C++ there is a concept called ‘resource acquisition is initialization‘ and it should be the responsibility of your destructors to clean up stuff.

    (Note that in C# and Java, this is usually solved by try/finally)

    For more info check out this page: http://www.research.att.com/~bs/bs_faq2.html#finally

    EDIT: Let me clear this up a little bit.

    Consider the following code:

    void MyMethod() {     MyClass *myInstance = new MyClass('myParameter');     /* Your code here */     delete myInstance; } 

    The problem: What happens if you have multiple exits from the function? You have to keep track of each exit and delete your objects at all possible exits! Otherwise, you will have memory leaks and zombie resources, right?

    The solution: Use object references instead, as they get cleaned up automatically when the control leaves the scope.

    void MyMethod() {     MyClass myInstance('myParameter');     /* Your code here */     /* You don't need delete - myInstance will be destructed and deleted      * automatically on function exit */ } 

    Oh yes, and use std::unique_ptr or something similar because the example above as it is is obviously imperfect.

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • 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
  • added an answer You should use EVT_UPDATE_UI(yourCommandID, yourEventHandler) for checking/unchecking and enabling/disabling menu… May 11, 2026 at 11:26 am
  • added an answer using System.IO; using System.Xml; using System.Xml.XPath; . . . string… May 11, 2026 at 11:26 am
  • added an answer div#container { width:500px; /* Same width as both columns */… May 11, 2026 at 11:26 am

Related Questions

Currently I am working on a project where goto statements are heavely used. The
I am currently working on a project where a programmer who fancied themselves a
I am currently working on a project where users need to be able to
I am working on a project currently where there are SQL strings in the
I am currently working on a project and my goal is to locate text
I am currently working on a project with specific requirements. A brief overview of
I am currently working on a project that is moving from .NET 2.0 to
I am currently working on a project to convert a number of Excel VBA
I am currently working on a project that will store specific financial information about
I am currently working on a project which requires migration of content from different

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.