1

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

6
  • 3
    Leaks, and is gross. You should wrap resources up, like std::vector. Until your program is correct, who cares about speed? Commented Aug 29, 2010 at 7:41
  • Again, please use the tab key to indent. Programming is much easier if you use an editor that supports tabs. Commented Aug 29, 2010 at 8:02
  • @Potatoswatter: Indenting the problem, or tab-based indenting? Cause there's religious wars about that... Commented Aug 29, 2010 at 8:04
  • @Merlyn: I'm talking about the tab key, not the tab character. Really, there are people who insist that they (or everyone) manually press the spacebar multiple times? Well, that's okay, but do it consistently!! Commented Aug 29, 2010 at 8:07
  • @Potatoswatter: Nah, they just insist that the editor spit out spaces when you hit the tab key. Commented Aug 29, 2010 at 8:15

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I assume you plan to add delete[], or the program will terminate before leakage matters.

Anyway, it won't be very efficient.

First, the array will be composed of non-contiguous blocks of memory. That makes it harder for the machine's memory subsystem to handle.

Second, some extra space is being wasted to hold the array of pointers.

Just do it the old fashioned way:

int *a = new int[ r * c ];

or with vector

std::vector<int> a( r * c );

and compute indexes as ever:

cout << a[ i * c + j ] << ' ';

However, since you are looping over the entire array, you could ignore the two-dimensionality except for formatting:

for ( int i = 0; i < r * c; ++ i ) {
    cout << a[ i ] << ' ';
    if ( i % c == c-1 ) cout << '\n';
}

Comments

0

If you don't delete the memory that you have allocated by using new, then you will leak memory.

7 Comments

so in main just write delete a at the end of program and no problems?
@c-programmer: Until you forget, or work with more complicated code that may throw exceptions. You really need to take a step back, get a good book, and learn good fundamentals.
@GMAN thanks i know but problem is that i have not money to buy books which i need so unfortunately i have big problems with buy books
@c-programmer: Your library may have one available for you. Also, these books are free.
@c-programmer: Yes. Very rarely are online tutorials good. (For example, that site never touches on programming practices, just the language.And not too complete at all.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.