Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array

Reply
Thread Tools

Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array

 
 
Jonathan Underwood
Guest
Posts: n/a
 
      08-12-2003
Hi

I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?

Many thanks
Jonathan.
 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      08-13-2003
Jonathan Underwood wrote:

> Hi
>
> I have a 2 dimensional array allocated as a contiguous chunk of
> memory, as described in Question 6.16 in the FAQ (i.e. in the manner
> of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
> I have a function declared as func(..., array[],...) and I wish to
> pass my 2D array to that function. As I understand it, this should be
> allowable since the 2D array is contiguous in memory, and if i
> therefore call it as func(..., my2darray[0], ...) the data types
> should match (double * in both cases). Is this the correct way to do
> this?
>


Should work, but this makes it look like you are trying to conform to the
f2c version of Fortran 77, rather than programming to the style of any
programming language.
--
Tim Prince
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      08-13-2003
"Jonathan Underwood" <(E-Mail Removed)> wrote:
> I have a 2 dimensional array allocated as a contiguous chunk of
> memory, as described in Question 6.16 in the FAQ (i.e. in the manner
> of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
> I have a function declared as func(..., array[],...) and I wish to
> pass my 2D array to that function. As I understand it, this should be
> allowable since the 2D array is contiguous in memory, and if i
> therefore call it as func(..., my2darray[0], ...) the data types
> should match (double * in both cases). Is this the correct way to do
> this?


Yes, this is fine with the definition as in FAQ 6.16:
int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));

However, if it were a normal array with a definition like:
double my2darray[4][7];

The expression my2darray[0] has type 'double[7]' or 'array of 7 double'.
It is undefined behaviour to attempt to access beyond the 7th element
of my2darray[0], that is beyond my2darray[0][6], so you're not supposed
to access the whole of my2darray through my2darray[0].

This could generate problems on a really good bounds-checking
implementation of C. Most implementations won't catch it though.

--
Simon.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
problem passing const_iterator to a function expecting iterator magnus.moraberg@gmail.com C++ 4 02-23-2009 04:28 PM
Passing a vector to a function expecting an array alamaison C++ 20 01-15-2009 10:05 PM
passing arg char[X][Y] to function expecting (char**) MackS C Programming 11 01-31-2005 07:31 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM



Advertisments