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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:17+00:00 2026-06-17T08:49:17+00:00

I have a function to allocate a 2D array in order to not to

  • 0

I have a function to allocate a 2D array in order to not to expend more memory than I need:

_>

template <class Xvar> Xvar** New2 (unsigned int rows,unsigned int cols)
{
    Xvar**  mem;
    unsigned int size, i;

    size = rows * cols;
    mem = new Xvar* [rows];
    mem [0] = new Xvar [size];
    for (i=1;i<rows;i++)
        mem [i] = &mem [0][i*cols];
    return mem;
}

Now, I need to check if that memory is allocated. (handle memory allocation errors),
without decreasing the performance of the function.

Should I use a try-catch block for each memory allocation, or only a unique try-catch block for the function.

template <class Xvar> Xvar** New2 (unsigned int rows,unsigned int cols)
{
    Xvar**  mem;
    unsigned int size, i;

    size = rows * cols;
    try {
    mem = new Xvar* [rows];
    }
    catch (...) { assert (...) } 
    try {
    mem [0] = new Xvar [size];
    } catch (...) { assert (...) }
    for (i=1;i<rows;i++)
        mem [i] = &mem [0][i*cols];
    return mem;
}

or something like:

template <class Xvar> Xvar** New2 (unsigned int rows,unsigned int cols)
{
    try { 
    Xvar**  mem;
    unsigned int size, i;

    size = rows * cols;
    mem = new Xvar* [rows];
    mem [0] = new Xvar [size];
    for (i=1;i<rows;i++)
        mem [i] = &mem [0][i*cols];
    return mem;
     }catch  (...) { assert (...) }
}

I think, the second way is not recommended because, if the first new fails, mem is NULL,
so if we do mem [0] , we are accessing to a memory that is not allocated, so that application fails at that point, and error cannot be catched.

  • 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-06-17T08:49:18+00:00Added an answer on June 17, 2026 at 8:49 am

    In the second way, if the first new fails, then evaluation immediately jumps to the catch block and never even tries to access mem[0].

    In any case, if you want to allow allocation to fail and detect this easily, you should probably use the nothrow variant, which simply returns NULL if the allocation fails. So something like

    mem = new (nothrow) Xvar*[rows];
    if (!mem) {
        // allocation failed, do whatever you want
    }
    mem[0] = new (nothrow) Xvar[size];
    if (!mem[0]) {
        // allocation failed, do whatever you want
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, this is homework, so I can not dynamically allocate memory for an array
I have to allocate a struct from within another function, obviously using pointers. I've
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
I ve got a problem with allocating cli::array in function. I have this kind
As an example say I have a class foo that does not have a
I have a zap() function written to deallocate a 1-d array as follows. void
I want to allocate variable sized 2D array in a function without using new
In other words, what options do I have to allocate memory in JavaScript? I
I have a template class that I have made called hash . My template
I'm writing a function where I need a significant amount of heap memory. 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.