Thread: Question Fortran like 2D array

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Lightbulb Question Fortran like 2D array

    Hi all,
    I try to write code that stores 2D array in a row, but I want to access to it by [][] operator. The code is

    Code:
    double** arr;
    int x=19,y=17;
    
    arr=(double**)malloc((x+1)*sizeof(double*))+1;
    arr[-1]=(double*)calloc(y*x,sizeof(double));
    for(int i=0;i!=x;i++)
      arr[i]=&arr[-1][i*y];
    May be there is an mistake causing errors in my numerical computation. Valgrind says there are no errors. Pleas hlep.
    P.S.: I hope you can understand my english

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Use Boost.Multi_Array. It does the memory management and index computation for you, and you can choose whether you want row-major (Fortran) or column-major (C) format.

    http://www.boost.org/libs/multi_array/doc/index.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    I used Boost, but it was too slow

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I used Boost, but it was too slow
    Boost is used in many high performance applications and games. It is not slow.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On a proper inlining compiler, MultiArray should have just about zero overhead over a manual implementation of index computation - perhaps even be faster. It should definitely be faster than your nested allocation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    I was surpriced too. I used Boost.MultiArray library only and the performance was poor (may be because of bad ussage). Using standard C++ array shows better performance. Finally, fortran arrays are faster (as I heard), so I want to write function that stores a matrix in a vector.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by TauWich View Post
    Finally, fortran arrays are faster (as I heard)
    Technically impossible. Raw arrays are as fast as the hardware allows.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Ok, i shall read more about MultiArray library to know where the error was. Many thanks for replys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  2. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM