Thread: from 2D array to 1D array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    from 2D array to 1D array

    Hi there,

    I need to use a function that requires a 1D array in entry; to do so I created a 1D array from my original 2D array in order to pass it to the function (consider that I cannot modify the function because it's part of a library that must be kept as it is), but with the obvious waste of memory.

    In fact, when I am treating very large matrices (extremely large), I get to a segmentation fault that is more than likely due to the size of the 2D array plus the new writing of the 1D vector.

    I would like to pass directly the original 2D array to the function that requires a 1D array.

    In this pseudo code probably my need is clearer:

    Code:
    int n=... (very large number. It changes with different input files that I read from a previous part of my code)
    int m=3;
    
    //Original 2D array from my code:
    double COORDS[n][5];   //Originally this array is dynamically allocated with malloc): double **VARS + dynamic allocation with malloc
    
    //Definition of the calling function: void function(double *VARIABLES)
    This is what I am doing now before passing the required 1D array to the function; and this is where I get a segmentation fault when using extremely large arrays:

    Code:
           k = 0;
    	for( i = 0; i<=nnodes-1; i++){	
    		COORDSarray[k] = COORDS[i][0];
    		COORDSarray[k+1] = COORDS[i][1];
    		COORDSarray[k+2] = 0.0;
           	
    	k=k+3;
    	}

    As said above, I would like to pass the 2D array as if its content were to be written in a 1D array

    I hope someone can help. Please, ask me if something is not clear from my description

    All the best
    CFD
    Last edited by cfdprogrammer; 03-23-2009 at 06:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  2. 1d to 2d array
    By helloamuro in forum C Programming
    Replies: 6
    Last Post: 04-23-2008, 06:14 PM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. 1D and 2D Arrays
    By Rajin in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 06:23 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM