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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:20:47+00:00 2026-05-25T23:20:47+00:00

In C++ FAQ, the [16.16] gives the following example, void manipulateArray(unsigned nrows, unsigned ncols[])

  • 0

In C++ FAQ, the [16.16] gives the following example,

void manipulateArray(unsigned nrows, unsigned ncols[])
{
   typedef Fred* FredPtr;
   FredPtr* matrix = new FredPtr[nrows];

   // Set each element to NULL in case there is an exception later.
   // (See comments at the top of the try block for rationale.)
   for (unsigned i = 0; i < nrows; ++i)
       matrix[i] = NULL;
   try {
     for (unsigned i = 0; i < nrows; ++i)
     matrix[i] = new Fred[ ncols[i] ];
     for (unsigned i = 0; i < nrows; ++i) {
        for (unsigned j = 0; j < ncols[i]; ++j) {
         someFunction( matrix[i][j] );
         }
      }

     if (today == "Tuesday" && moon.isFull()) {
       for (unsigned i = nrows; i > 0; --i)
        delete[] matrix[i-1];
       delete[] matrix;
       return;
     }

 ...code that fiddles with the matrix...

    }
  catch (...) {
     for (unsigned i = nrows; i > 0; --i)
      delete[] matrix[i-1];
      delete[] matrix;
       throw;    // Re-throw the current exception
   }

  for (unsigned i = nrows; i > 0; --i)
     delete[] matrix[i-1];
     delete[] matrix;
  }

Why we have to use delete this way, I mean,

First delete[] matrix[i-1];
then delete[] matrix;

Moreover, what’s the point of after the whole “try…catch” cycle, we still have to put

for (unsigned i = nrows; i > 0; --i)
   delete[] matrix[i-1];
   delete[] matrix;

at the end of this function.

  • 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-25T23:20:47+00:00Added an answer on May 25, 2026 at 11:20 pm
    1. The try/catch block is necessary to ensure proper clean-up even if an exception is thrown anywhere in the code before the normal clean-up happens. This includes an exception in one of the new expressions. The delete[] is safe because all the relevant pointers were initially set to zero, so that the deletion is valid even if no allocation ever occurred.

      (Note that if any exception does occur, it will still be propagated outside the function. The local try/catch block only ensures that the function itself doesn’t leak any memory.)

    2. There are two sets of arrays: one is the outer array matrix, which is an array of pointers. This array gets allocated first and deleted last. Second, each element matrix[i] is itself a pointer to an array of Fred elements. Each array gets allocated in the first for loop, and thus has to be deleted in another loop at the end.

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

Sidebar

Related Questions

The XWiki FAQ gives an example for XWiki 1.0 syntax: {graphviz:type=dot}digraph G {Hello->world}{graphviz} My
I have a simple FAQ page in which each item has the following HTML
Language FAQ says import scala.collection.mutable.{_, Map => _, Set => _} should import all
I have an FAQ in HTML ( example ) in which the questions refer
The PulseAudio FAQ has this sentence : If $DISPLAY is set, the library tries
Given the following HBase schema scenario ( from the official FAQ )... How would
I am creating an online FAQ type system using php and mySQL. The following
The FAQ that comes with IronPython 2.0.1 says the following: You can define interfaces
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is

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.