473,890 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multi dimensional arrays as one dimension array

The subject might be misleading.
Regardless, is this code valid:

#include <stdio.h>

void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}

Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).

Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.
Aug 29 '08
152 9984
James Tursa <ac************ *******@hotmail .comwrites:
On Sun, 31 Aug 2008 11:43:23 -0700, Keith Thompson <ks***@mib.or g>
wrote:
[...]
>>No. There cannot be padding between array elements; in particular,
given:

double arr[10][10];

the size of arr is guaranteed to be exactly 100*sizeof(doub le).

Padding isn't the issue.

Well, I didn't really believe that padding was an issue but that's
what seem to be implied by the response.
> The issue is that the standard doesn't
require implementations to support indexing past the end of an array.
So if I write

arr[0][15]

I'm trying to refer to an element of arr[0] that doesn't exist.
There's a valid object, accessible as arr[1][5], at the intended
location in memory -- and *most* C compilers will let you access that
object either as arr[0][15] or as arr[1][5]. But arr[1][5] is
guaranteed to work, and arr[0][15] isn't, because it attempts to index
beyond the end of the double[10] array arr[0].

In other words, implementations are allowed, but not required, to
perform bounds checking.

Well, I am still trying to understand how that argument applies to the
original OP posted code. Your argument is based on using arr directly.
But you seem to be saying that OP can't do this:

double *dp = (double *) arr;

and then traverse the entire array using dp. Is that what you are
saying?
Close. I'm saying that you most likely *can* get away with that
(treating an array of array of double as if it were an array of
double), but the standard doesn't require an implementation to make it
work. The most likely ways it can fail are if an implementation
performs run-time or compile-time bounds checking, or if an optimizer
assumes (as it's permitted to do) that you're not doing something like
this, causing the generated code not to do what you expected it to do.

Harald's explanation elsewhere in this thread makes the point more
clearly than I did, I think:

The fact that double[2][3] doesn't have elements such as
x[0][5]. There must be a valid double, 5*sizeof(double ) bytes into
x. However, x[0][5] doesn't mean just that. x[0][5] (or
((double*)x)[5]) means you're looking 5*sizeof(double ) bytes into
x[0]. x[0] doesn't have that many elements.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 31 '08 #21
On Sun, 31 Aug 2008 20:20:37 +0000, James Tursa wrote:
So you are saying that x[0][5] means exactly the same thing to the
compiler as ((double*)x)[5] ?
Yes, because (double*)x is a pointer to x[0][0].

(Actually, since the behaviour is undefined, it is allowed and
realistically possible for the compiler to treat the two differently, for
many possible reasons, but I'm not aware of any relevant specific details.)
I thought x[0] would be an array of 3
doubles, whereas (double*)x would be a plain double*.
Well, (double*)x is a plain double*, just not one into the whole array.
The only way I can think of to get overlapping arrays in the way you're
looking for is by using a union:

union {
double singledim[6];
double multidim[2][3];
} x;

but this is only possible if you know the length of the array at compile
time. Do you?
Aug 31 '08 #22
On Sun, 31 Aug 2008 13:53:18 -0700, vippstar wrote:
On Aug 31, 11:47 pm, Harald van Dijk <true...@gmail. comwrote: <snip>
>The only way I can think of to get overlapping arrays in the way you're
looking for is by using a union:

union {
double singledim[6];
double multidim[2][3];

} x;

but this is only possible if you know the length of the array at
compile time. Do you?

What about C99's VLAs?
VLAs aren't allowed as structure or union members. I can't find the
normative wording in the standard (references are appreciated), but the
example in 6.7.5.2p10 is explicit enough that I trust it to correctly
reflect the intent.
Aug 31 '08 #23
On Sun, 31 Aug 2008 23:26:36 +0200, Harald van Dijk wrote:
VLAs aren't allowed as structure or union members. I can't find the
normative wording in the standard (references are appreciated),
Never mind. It's 6.7.2.1p8:
"A member of a structure or union may have any object type other than a
variably modified type."
Aug 31 '08 #24
On Sep 1, 12:31 am, Harald van Dk <true...@gmail. comwrote:
On Sun, 31 Aug 2008 23:26:36 +0200, Harald van Dk wrote:
[correcting me]
VLAs aren't allowed as structure or union members. I can't find the
normative wording in the standard (references are appreciated),

Never mind. It's 6.7.2.1p8:
"A member of a structure or union may have any object type other than a
variably modified type."

Ah yes. Thanks.
Aug 31 '08 #25
On Sep 1, 2:03 am, James Tursa <aclassyguywith akno...@hotmail .com>
wrote:

<snip>
But the only way I can make sense of everything else that everyone is
trying to tell me is that there are special rules for unsigned char
that prevent bounds checking for this type of code that don't apply to
other types. For example:
Yes there is. Because you can treat any ptr to object as an array of
unsigned char, to observe the objects representation.
Aug 31 '08 #26
James Tursa wrote:
But the only way I can make sense of everything else that everyone is
trying to tell me is that there are special rules for unsigned char
that prevent bounds checking for this type of code that don't apply to
other types.
That's how the string functions work.

N869
7.21 String handling <string.h>
7.21.1 String function conventions
[#1] The header <string.hdeclar es 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.
--
pete
Sep 1 '08 #27
On Sun, 31 Aug 2008 16:42:47 -0700 (PDT), vi******@gmail. com wrote:
>On Sep 1, 2:03 am, James Tursa <aclassyguywith akno...@hotmail .com>
wrote:

<snip>
>But the only way I can make sense of everything else that everyone is
trying to tell me is that there are special rules for unsigned char
that prevent bounds checking for this type of code that don't apply to
other types. For example:

Yes there is. Because you can treat any ptr to object as an array of
unsigned char, to observe the objects representation.
So is this conforming?

double d[2][3];
double *dp = (double *) d;
int i;
for( i=0; i<6; i++ )
dp = (double *)(((unsigned char *)dp) + sizeof(double)) ;

James Tursa
Sep 1 '08 #28
On Sep 1, 3:37 am, James Tursa <aclassyguywith akno...@hotmail .com>
wrote:
On Sun, 31 Aug 2008 16:42:47 -0700 (PDT), vipps...@gmail. com wrote:
<snip>
Because you can treat any ptr to object as an array of
unsigned char, to observe the objects representation.

So is this conforming?

double d[2][3];
double *dp = (double *) d;
int i;
for( i=0; i<6; i++ )
dp = (double *)(((unsigned char *)dp) + sizeof(double)) ;
I don't know, but it's a good question; I'd like to know as well.

Sep 1 '08 #29
On Sep 1, 11:03*am, James Tursa <aclassyguywith akno...@hotmail .com>
wrote:
I get the part about x[0][5] maybe not working.

But the only way I can make sense of everything else that everyone is
trying to tell me is that there are special rules for unsigned char
that prevent bounds checking for this type of code that don't apply to
other types.
There is no special case for unsigned char
w.r.t bounds checking. You can't create
a pointer into an object, and then use that
pointer to access beyond the object's bounds,
regardless of the pointer type.

For example:
>
double d[2][3];
double *dp = (double *) d;
unsigned char *ucp = (unsigned char *) d;
double *xdp = malloc(sizeof(d ));
unsigned char *xucp = malloc(sizeof(d ));
mydoublecopy(xd p,dp,6); *// assume prototype present
myucharcopy(xuc p,ucp,6*sizeof( double));

where

void mydoublecopy(do uble *t, double *s, size_t n) {
while( n-- ) { *t++ = *s++; } }

void myucharcopy(uns igned char *t, unsigned char *s, size_t n) {
while( n-- ) { *t++ = *s++; } }
Note that the expression "d" is defined to
mean &d[0], in a value context (which is what
we have here). That is, the address of an array
of three doubles.

'mydoublecopy' and 'myucharcopy' both cause
undefined behaviour because they try to access
beyond the bounds of this array.
Now, if you had written:
double *dp = (double *) &d;
unsigned char *ucp = (unsigned char *) &d;

then there would be no undefined behaviour,
because d is an object containing six doubles
(as opposed to d[0] which is an object
containing three doubles).
Sep 1 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1691
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written all the 3D rendering scripts, but what's really hanging me up is how to I pass information from an one-dimensional arraylist to a two-dimensional array. Here's what I'm trying to do: 1) My program generates an arraylist called "mpoints" of 2D...
6
9526
by: Gregory L. Hansen | last post by:
I'm sure I saw this mentioned somewhere, but I can't find it. How can I dynamically allocate a multi-dimensional array in C++? My next question, if this gets figured out, is if I should use the delete command to remove it, or would that be delete ? -- "What are the possibilities of small but movable machines? They may or may not be useful, but they surely would be fun to make."
4
2256
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
8
2258
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
11
4482
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
1
6314
by: Fayez Al-Naddaf | last post by:
I got this message when I tried to browse my web service "Multi-dimensional arrays are not supported. Use a jagged array instead" Can someone told me why?
0
1370
by: volx | last post by:
Hello all: What is the proper way to implement in MC++ a web service which accepts a multi dimensional array as a parameter to one of its methods? This does compile: double pass2DArray(Double array __gc);
5
1638
by: David T. Ashley | last post by:
Hi, Aside from the obvious recursive syntax for initialization, $x = array( ... array( ...)), what is the best way to deal with multi-dimensional arrays in PHP? It seems there is no syntax like $x???
3
2137
by: ben.r.wood | last post by:
I am not entirely sure, but after scanning the web believe I need to use multi-dimensional arrays for my problem - although I have not seen any examples of what I am trying to achieve. I have a form with one field, for which a user will submit a lump of delimited data. The data will always be in the same format, ie. 5 fields per record, each field has a delimeter and a new row starts a new record. for example:
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.