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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:35:40+00:00 2026-05-15T20:35:40+00:00

Following this nice example I found, I was trying to create a function that

  • 0

Following this nice example I found, I was trying to create a function that dynamically generates a 2D grid (two dimensional array) of int values.

It works fairly well the first couple of times you change the values but if crashes after that. I guess the part where memory is freed doesn’t work as it should.

void testApp::generate2DGrid() {
    int i, j = 0;

    // Delete previous 2D array
    // (happens when previous value for cols and rows is 0)
    if((numRowsPrev != 0) && (numColumnsPrev != 0)) {
        for (i = 0; i < numRowsPrev; i++) {
            delete [ ] Arr2D[i];
        }
    }

    // Create a 2D array
    Arr2D = new int * [numColumns];
    for (i = 0; i < numColumns; i++) {
        Arr2D[i] = new int[numRows];
    }

    // Assign a random values
    for (i=0; i<numRows; i++) {
        for (j = 0; j < numColumns; j++) {
            Arr2D[i][j] = ofRandom(0, 10);
        }
    }

    // Update previous value with new one
    numRowsPrev = numRows;
    numColumnsPrev = numColumns;
}
  • 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-15T20:35:41+00:00Added an answer on May 15, 2026 at 8:35 pm

    2-dim array in C++ with no memory issues:

    #include <vector>
    
    typedef std::vector<int> Array;
    typedef std::vector<Array> TwoDArray;
    

    Usage:

    TwoDArray Arr2D; 
    
    // Add rows
    for (int i = 0; i < numRows; ++i) {
        Arr2D.push_back(Array());
    }
    
    // Fill in test data
    for (int i = 0; i < numRows; i++) {    
        for (int j = 0; j < numCols; j++) {
            Arr2D[i].push_back(ofRandom(0, 10));           
        }
    }
    
    // Make sure the data is there
    for (int i = 0; i < numRows; i++) {    
        for (int j = 0; j < numCols; j++) {
            std::cout << Arr2D[i][j] << ' ';
        }
    std::cout << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

By following example from em-websocket gem, I've just create simple echo-server that running with
Following this question, it seems that it is possible to open a file from
Following this question , I use this function to convert arrays to objects, function
After following this MVC 4 tutorial series I was trying some of the stuff
For a present, I am trying to create a code that reads as a
I was looking for a way to create a nice progressbar. I found some
I'm trying to interop with C in Python, and I have the following function
Following this question and answer , I still have a bit trouble in the
Following this code my button works perfectly: http://twitter.github.com/bootstrap/javascript.html#modals Fades and everything. I want to
Following this discussion , I've encountered a bad access issue; A loop has several

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.