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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:29:33+00:00 2026-06-01T01:29:33+00:00

In the code below is the initial first loop needed when creating a 2D

  • 0

In the code below is the initial first loop needed when creating a 2D array, or if there is a failed memory allocation will the pointers to the actual rows be initialized to NULL?

unsigned char **row_pointers;
try
{
  row_pointers = new unsigned char *[height];
  for (int i = 0; i < height; ++i)
    row_pointers[i] = NULL;
  for (int i = 0; i < height; ++i)
    row_pointers[i] = new unsigned char[width];
}
catch (std::bad_alloc)
{
  throw std::runtime_error("Failure to allocate memory for raw data");
}

UPDATE

To clarify, the code I am looking at is:

149   /*
150    * Allocate sufficient space for the data
151    */
152   unsigned char **row_pointers;
153   try
154   {
155     row_pointers = new unsigned char *[height]();
156     for (int i = 0; i < height; ++i)
157       row_pointers[i] = new unsigned char[width];
158   }   
159   catch (std::bad_alloc)
160   {
161     /*
162      * If insufficient memory than try and clean up
163      * and throw runtime error
164      */ 
165     for (int i = 0; i < height; ++i)
166     { 
167       if (row_pointers[i] != NULL)
168       { 
169         delete row_pointers[i];
170       }
171     }
172     throw std::runtime_error("Failure to allocate raw memory for data");
173   }
...   // White Space
177 
178   /*
179    * Now read the data all at once (no need to handle interlacing
180    */
181   png_read_image(m_pPNG, row_pointers);
182 
183   for (int i = 0; i < height; ++i)
184   {
185     for (int j = 0; j < width; ++j)
186       std::cout << row_pointers[i][j];
187     std::cout << std::endl;
188   }
  • 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-01T01:29:34+00:00Added an answer on June 1, 2026 at 1:29 am

    The array allocated with new unsigned char*[height] is not initialized by this operation. If any allocation fails it will throw an exception. In your code you next initialize you array. I think this should look like this:

    std::fill_n(row_pointers, height, 0);
    

    Of course, if any subsequent allocation fails this array will be leaked as will all the other arrays allocated so far. You could clean this mess up in the catch-block.

    Personally, I cannot cope with multiple clean-ups: it is just too complicated to get right. Personally I would use two std::vector<T> instead, bundled into a class:

    • one std::vector<unsigned char*> which gets initialized to point to the start of the subvectors
    • one std::vector<unsigned char> to hold all of the subvectors

    Once these are allocated, the pointers in the first vector are set up to point into the second vector at appropriate locations. Conveniently, if anything goes wrong the destructors of the two vectors will take care of the clean-up.

    Here is how this would look like:

    #include <vector>
    #include <cstddef>
    
    struct array2d
    {
        array2d(std::size_t height, std::size_t width)
            : inner_(height * width)
            , outer_(height)
        {
            for (std::size_t i(0); i != height; ++i) {
                this->outer_[i] = &this->inner_[i * width];
            }
        }
    
        unsigned char** get() { return &this->outer_[0]; }
    
        std::vector<unsigned char> inner_;
        std::vector<unsigned char*> outer_;
    };
    

    When you got an object picture of this type you could use picture.get() to get pointer suitable to be passed to C functions.

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

Sidebar

Related Questions

The code below working for only first div. I can't display other divs when
The code below is for a simple newsletter signup widget. I'm sure there's a
The code below doesn't seem to work or find anything on an array. I'm
Code below is working well as long as I have class ClassSameAssembly in same
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below generates a warning CS3006 Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref
The code below removes www., etc. from the beginning of websites that are entered
The code below allows the user to enter in a phrase in plain English,
The code below projects the blue vector, AC, onto the red vector, AB, the
I am kind of confused about how the below code works. In my head

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.