Re: multi dimensional arrays as one dimension array
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Mon, 01 Sep 2008 21:32:41 -0400
Ben Bacarisse wrote:
pete <pfiland@xxxxxxxxxxxxxx> writes:
Richard Heathfield wrote:Ben Bacarisse said:There can't be any difference.Richard Heathfield writes:<snip>
<weasel>If all you want is a solution that is guaranteed not to break any rules,Are you of the opinion that one or both of memcpy(p, arr, sizeof arr)
it's pretty easy. If MATLAB provides a space into which you need only
copy the data, you can do so in a simple loop:
/* We assume that arr is defined as double arr[ROWS][COLS]. We
further assume that p is of type double *, and points to
space at least ROWS * COLS * sizeof(double) bytes in size.
*/
t = p;
thisrow = 0;
while(thisrow < ROWS)
{
memcpy(t, arr[thisrow], sizeof arr[thisrow]);
t += COLS; /* move t on by COLS doubles */
++thisrow;
}
memcpy(p, &arr, sizeof arr) are undefined?
I'm of the opinion that the above code represents a squeaky-clean
way of converting a two-dimensional array into a one-dimensional
array.
</weasel>
A slightly less weaselly answer to your question would be that I'm
not entirely sure that a pedant (i.e. plenty of people in this
newsgroup, including myself) could not construct an argument that
the memcpy route could exhibit undefined behaviour.
What puzzles me is that arr (in a context where it converts to
&arr[0]) is supposed to be pointer constrained legally to range over
only the first (array) element of arr, yet if have
double x[ROWS];
x (in places where it converts to &x[0]) is permitted to range beyond
that first (non-array) element of x. Now, in the first case you have
an array pointer that gets further converted and in the second you
don't so this may be where the difference comes from, but I am having
trouble seeing the wording in the standard.
What is it about the conversions in memcpy(p, arr, sizeof arr) that
causes trouble when those in memcpy(p, x, sizeof x) do not?
In both cases the third argument is the number of bytes
of the object refered to by the second argument.
And in both cases the second parameter is initialised
to the address of the lowest addressable byte
of the object refered to by the second argument.
Firstly, that wording (about lowest addressable byte) applies only to
conversion to "pointer to character" types. All sane people know it
applies to void * too (and hence memcpy) but it is very hard to prove
it from the wording in the standard.
I made a mistake when I said "parameter".
The internal workings of the string functions are described as:
N869
7.21 String handling <string.h>
7.21.1 String function conventions
[#1] The header <string.h> declares one type and several
functions, and defines one macro useful for manipulating
arrays of character type and other objects treated as arrays
of character type.
To treat an object as an array of character type,
the object must be accessed as though by a pointer to character type.
--
pete
.
- References:
- Re: multi dimensional arrays as one dimension array
- From: Ben Bacarisse
- Re: multi dimensional arrays as one dimension array
- From: Richard Heathfield
- Re: multi dimensional arrays as one dimension array
- Prev by Date: Re: Typedef Bug/Error
- Next by Date: Re: p 145 K&R
- Previous by thread: Re: multi dimensional arrays as one dimension array
- Next by thread: Re: multi dimensional arrays as one dimension array
- Index(es):
Relevant Pages
|