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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:07:28+00:00 2026-05-13T15:07:28+00:00

We recently received a report that our application will occasionally fail to run. I

  • 0

We recently received a report that our application will occasionally fail to run. I tracked down the problem code to this:

struct ARRAY2D
{
   long[] col;
}

int numRows = 800000;
int numCols = 300;
array = (ARRAY2D*) malloc(numRows * numCols * sizeof(long))

This allocation of 800 Mb can fail if the user doesn’t have a large enough free block. What is the best way to change how I allocate the memory?

Keep in mind that I have a large amount of code that accesses this object like this: array[row].col[colNum], so I need something that requires minor or primarily find & replace editing of the array access code.

  • 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-13T15:07:28+00:00Added an answer on May 13, 2026 at 3:07 pm

    You can allocate smaller chunks of memory separately, instead of one huge block.

    long** array = NULL;  
    array = (long**) malloc(numCols * sizeof(long*));  
    for (int i = 0; i < numCols; i++)  
       array[i] = (long*)  malloc(numRows * sizeof(long));
    

    Generally, memory allocation may fail, every allocation. However, let’s say statistically, due to memory fragmentation, allocating a single large block of memory has higher chance to fail more often than allocating N number of smaller blocks.
    Although, also the solution above may cause problems as it is a bit like a double-bladed sword because it may lead to further memory fragmentation.

    In other words, there is no generally perfect answer and solution depends on details of a system and application.

    As from the comments it seems C++ library is a possibility, then solution based on std::vector (i.e. generic vector of vectors in C++) or using Boost.MultiArray

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

Sidebar

Related Questions

I was recently working on an application that sent and received messages over Ethernet
Recently, we received a bug report from one of our users: something on the
I recently received an email from my girlfriend that spamassassin marked as spam, mostly
I'm using subclipse in Flex Builder 3, and recently received this error when trying
I recently asked a question about functional programming, and received (good!) answers that prompted
I recently received a project that contains multiple web applications with no MVC structure.
I recently wrote a section of code wherein upon finishing, I received a warning
Recently, I ran into a problem with my application: the size of the JSON
I recently received a 64-bit crash dump from a customer. Our processes are all
I recently received a feedback from a colleague about my source code of a

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.