C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-23-2009, 05:23 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 111
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.
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 06:05 AM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
What do you think, can a pointer equal a double value??
Multi-dimensional arrays are laid out as a row of logically contiguous memory locations ie 1D.

Last edited by itCbitC; 03-23-2009 at 07:26 AM.
itCbitC is offline   Reply With Quote
Old 03-23-2009, 06:21 AM   #3
Registered User
 
Join Date: Mar 2009
Posts: 111
Hi there,

I was thinking the same thing, but I wouldn't know how to apply that concept when passing my 1D pointer to the calling function:

Code:
function(double *COORDS)
Would this be correct?
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 06:27 AM   #4
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
yep, that is correct.
itCbitC is offline   Reply With Quote
Old 03-23-2009, 06:39 AM   #5
Registered User
 
Join Date: Mar 2009
Posts: 111
Thank you, Ill try it and let u know soon
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 06:53 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 111
Quote:
Originally Posted by itCbitC View Post
yep, that is correct.
Hi again, the problem is that, given the following definitions:

Code:
double **COORDS;

//dynamic allocation would be here ...
and

Code:
float function(double *array)
How would I pass only one of the columns of **COORDS (let's say column 2) to "function"?
I cannot call it as:
Code:
function(*COORDS[2])
because I get an error saying : error: incompatible type for argument 1 of 'function',

nor can I call it as:
Code:
function(*COORDS)
which gives me a segmentation fault in execution

Sorry about the many problems
CFD
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 07:22 AM   #7
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
Quote:
Originally Posted by cfdprogrammer View Post
[/code]

How would I pass only one of the columns of **COORDS (let's say column 2) to "function"?
I cannot call it as:
Code:
function(*COORDS[2])
because I get an error saying : error: incompatible type for argument 1 of 'function',
Dereferencing the pointer makes it a double instead of a pointer to double.
To pass column 2 (array element at index 2??) the function call would be:
Code:
function(COORDS[2]);
itCbitC is offline   Reply With Quote
Old 03-23-2009, 08:38 AM   #8
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
An assumption was that enough memory has been allocated for each of the pointers of the array that COORDS points to; is that correct?
itCbitC is offline   Reply With Quote
Old 03-23-2009, 09:36 AM   #9
Registered User
 
Join Date: Mar 2009
Posts: 111
Quote:
Originally Posted by itCbitC View Post
An assumption was that enough memory has been allocated for each of the pointers of the array that COORDS points to; is that correct?
Hello again; yes, your assumption is correct, that is why I am having all this doubts about the segmentation faults that I get with very large matrices.

However, when passing the 3rd column of the matrix as you suggested

Code:
 function( COORDS[2])
apparently I am not passing what I mean to pass, because when I print it out to screen from inside the function to verify its correctness, I do not get either of the 3 columns of that 2D array, but a strange combination of values.

I am lost

thank you again
CFD
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 11:50 AM   #10
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
Hard to tell unless you post the portion of the code that's causing the problem.
itCbitC is offline   Reply With Quote
Old 03-23-2009, 11:57 AM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
If you are keeping data in format A, and you need to pass it somewhere that expects format B, then there is no way around the fact that you have to convert it, which will be inefficient. Unless format B is completely bad for your own purposes, it might be better to just represent the data as format B yourself, to avoid this overhead.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 03-23-2009, 12:51 PM   #12
Registered User
 
Join Date: Mar 2009
Posts: 111
Thank you very much for replying.
I wanted to avoid to write another array because they are extremely big ones, and I believe that is what is causing my system to crash (segmentation fault if big matrices are used).

I see what I can do in a different way

I appreciate

CFD
cfdprogrammer is offline   Reply With Quote
Old 03-23-2009, 02:17 PM   #13
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
How big is the size of your array? Given that each double is 8 bytes, sizeof COORDS will be 8 x rows x cols bytes.
itCbitC is offline   Reply With Quote
Old 03-24-2009, 02:34 AM   #14
Registered User
 
Join Date: Mar 2009
Posts: 111
Hi,

The arrays Im using can be as big as 10000 elements; I changed all my doubles into floats to start "saving"
cfdprogrammer is offline   Reply With Quote
Old 03-24-2009, 09:56 AM   #15
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,446
Quote:
Originally Posted by cfdprogrammer View Post
Hi,

The arrays Im using can be as big as 10000 elements; I changed all my doubles into floats to start "saving"
With 10000 elements, 8x10000 is approx. 80Kb, hardly enough to fill up the heap. Perhaps storage hasn't been allocated for first-level pointers to double.
itCbitC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22