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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:07:03+00:00 2026-05-16T18:07:03+00:00

i have following code for allocation two dimensional array #include <iostream> using namespace std;

  • 0

i have following code for allocation two dimensional array

#include <iostream>
using namespace std;
int **malloc2d(int r,int c){
    int **t=new int*[r];
     for (int i=0;i<r;i++)
         t[i]=new int[c];

      for (int i=0;i<r;i++){
           for (int j=0;j<c;j++){
                t[i][j]=i+j;
           }
      }
     return t;
     }

int main(){
    int m=10;
    int n=10;
    int **a=malloc2d(m,n);
     for (int i=0;i<m;i++){
          for (int j=0;j<n;j++){

              cout<<a[i][j]<< " ";
              cout<< " \n";
          }
          cout<< " \n";
     }


    return 0;


}

it works but my question is: how good is this code according to performance efficienty or according to code speed? thanks

  • 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-16T18:07:04+00:00Added an answer on May 16, 2026 at 6:07 pm

    With an int ** you have lots of pointers to tiny (4 byte) memory spaces which is inefficient due to malloc overhead (every malloc implementation has an overhead, the minimum normally is sizeof(void*) AFAIK which in your case would mean there’s at least a 100% overhead for all “cells”).

    As an alternative, you could use a one-dimensional array and calculate the indexes yourself like this: index = (row * num_columns) + column. You would lose the nice a[row][column] notation, though. Still, it should be faster to access as well because in your (clean) solution there have to be two pointer dereferences (memory operations) while in the way I suggest you only have one. It would look something like this:

    #include <iostream>
    
    using namespace std;
    
    inline int a_index(int row, int column, int column_size) {
            return((row * column_size) + column);
    }
    
    int *malloc2d(int r,int c) {
            int *t=new int[r * c];
            for (int i=0;i<r;i++){
                    for (int j=0;j<c;j++){
                            t[a_index(i,j,c)]=i+j;
                    }
            }
            return t;
    }
    
    
    int main(){
            int m=10;
            int n=10;
            int *a=malloc2d(m, n);
            for (int i=0;i<m;i++){
                    for (int j=0;j<n;j++){
                            cout<<a[a_index(i,j,n)]<< " ";
                            cout<< " \n";
                    }
                    cout<< " \n";
            }
    
            return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to What does zero-sized array allocation do/mean? I have following code
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code using hibernate to throw a custom exception on error and
I have a C++ code mentioned below: #include<iostream> #include<stdexcept> const long MAX = 10240000;
I have the following code, which I am using to test how I'm using
I have a question regarding memory allocation order. In the following code I allocate
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

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.