473,748 Members | 10,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

layout of arrays

AC
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I can
consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is this
correct?

This code works fine for me :

#include <stdio.h>

int
main(void)
{
int a[2][3][1]={1,2,3,4,5,6};
int *p;
int i;
printf("sizeof a =%u\n",(unsigne d int) sizeof a);
p = (int *) &a;
for(i=0; i < sizeof a/sizeof(int);i++ )
printf("p[%d]=%d\n",i,p[i]);
return 0;
}
Nov 14 '05 #1
3 1399

"AC" <te**@test.test > wrote
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I
can consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is
this correct?

Yes, but be warned that everything gets horrible as soon as you try to use
such arrays in non-trivial ways.
Generally if you need a 3d array, consider whether the dimensions are really
fixed at compile time. The consider whether it makes sense to write your
subroutines so that they can only operate on an array of fixed dimensions.
If the answers to these questions are no, you will save yourself a lot of
grief by building the array with calls to malloc().
Nov 14 '05 #2
AC wrote:

I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right?
So I can consider, (int *) &a as a pointer to an array of 2*3*1=6
ints. Is this correct?

This code works fine for me :

#include <stdio.h>

int
main(void)
{
int a[2][3][1]={1,2,3,4,5,6};
int *p;
int i;
printf("sizeof a =%u\n",(unsigne d int) sizeof a);
p = (int *) &a;
for(i=0; i < sizeof a/sizeof(int);i++ )
printf("p[%d]=%d\n",i,p[i]);
return 0;
}


Looks fine to me. You might want to try this modification, playing
with the various *DIM defines, to see the correspondence of the
linear array and the multidimensiona l array. C only has a single
array dimension, and multidimensiona l arrays simply are a mapping
into a single dimensional array. Notice How I have intermixed
array and pointer notation.

Keep the dimensions below 10 so the displays show all coordinates.

#include <stdio.h>

#define XDIM 4
#define YDIM 3
#define ZDIM 2

#define LINEMAX 72

void dump(int a[][YDIM][ZDIM], size_t sz)
{
int linesize;
size_t ix;
int *p;

p = (int *)a;
linesize = 0;
for (linesize = 0, ix = 0; ix < sz; ix++) {
linesize += printf("p[%2d]=%0.3d", (int)ix, p[ix]);
if (linesize <= LINEMAX) {
putchar(' '); linesize++;
}
else {
putchar('\n'); linesize = 0;
}
}
if (linesize) putchar('\n');
} /* dump */

/* -------------------- */

void inita(int a[][YDIM][ZDIM])
{
int x, y, z;
size_t ix;
int *p;

p = (int *)&a;
for (x = 0; x < XDIM; x++)
for (y = 0; y < YDIM; y++)
for (z = 0; z < ZDIM; z++) {
ix = (((z * ZDIM) + y) * YDIM) + x;
a[x][y][z] = ((z * 10 + y) * 10) + x;
}
} /* inita */

/* -------------------- */

int main(void)
{
int a[XDIM][YDIM][ZDIM] = {1,2,3,4,5,6};
int *p;
size_t ix;

printf("sizeof a = %lu\n", (unsigned long) sizeof a);
p = (int *) &a;
for (ix = 0; ix < sizeof a/sizeof(int); ix++)
printf("p[%d]=%d\n", (int)ix, p[ix]);
dump(a, sizeof a/sizeof(int));
inita(a);
dump(a, sizeof a/sizeof(int));
return 0;
}
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #3
On Wed, 18 May 2005 11:08:40 -0400, "AC" <te**@test.test > wrote:
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I can
consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is this
correct?

In practice but not quite 100% formally. It is clearly required by the
standard that the storage (representation ) of an array be the elements
in order without additional padding, and for multidim arrays applying
this rule repeatedly requires row-major order. ("additional " because
there can be padding _within_ a struct (or union) element, including
at its end for alignment.)

It is not clearly required that you can _access_ the "flattened"
elements by a non-character pointer so ( (int*)&a ) [4] isn't itself
guaranteed. But it is required that a character pointer step through
the entire representation, so * (int*) ( (char*)&a + 4*sizeof(int) )
must. In practice, there's no reasonable way to implement the latter
without implementing the former, not to mention that it just "makes
sense" anyway, and everyone does. I think I recall a couple of
committee members commenting on comp.std.c that they intended this,
but couldn't find words they were certain adequately specified C's
historically loose memory model without risking possible problems in
other areas and let the sleeping dog lie. Especially since enough
programs and programmers do this that any implementor who broke it,
even if they could argue that they weren't violating the standard,
would be shunned by most if not all users.

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #4

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

Similar topics

0
2381
by: Peter Hitchmough | last post by:
What is the *truth* when it comes to configuring Oracle files on an Enterprise Virtual Array like an HP EVA3000 or EVA5000 or an EMC Symmetrix frame. As I Understand It - A Virtual Array is a storage appliance with a large number of disks (say over 50), dual-path controller access and, significantly, a very large disk cache. The cache is supported by front-end and back-end (disk-side) processors and a range of algorithms that aim to...
82
10699
by: Peter Diedrich | last post by:
The site design is pretty simple: ============================================ | Head | ============================================ | | | | | | | | | left | center | right | | | | | | | | |
47
9153
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
3
1493
by: Josema | last post by:
Hi, I have a web application, that takes from excel a range of rows and columns... The method that gets the data in the ranges returns an Array of two dimensions with the data... Then i render the data in the browser for instance using html tables.... My problem its that:
1
2418
by: Fuzzy via .NET 247 | last post by:
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field X will either be short integer or double, and other info in the struct can be used to determine if X should be interpreted as short integer or double. I have found the docs for and for . However, one doc state that these fields only control...
0
2590
by: Emil Briggs | last post by:
I am working with an application whose performance is limited by the write activity (inserts, updates and deletes) on three specific tables. The database is RAM resident so read performance is not limited by the disk speeds. We are using Postgres 8 and I am wondering about the best way to set up tablespaces. We have 3 disk arrays. Would it make more sense to place each of tables 1,2 and 3 and their associated indexes on their own...
10
9207
by: Luke | last post by:
Hi. I am trying to make correct layout, here is an example of (dynamically generated content via jsp): http://localhost/www/layout.htm Most outer div is positioned absolute (if not then it will not grow when content inside div.body is greater than width of window of user agent), anyway anyone knowlegable can see it in sources...
14
4858
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
1
1313
by: Adrian Hawryluk | last post by:
I was wondering, does this meet the criteria for "layout compatible" in the C++ specification? struct A { int a; }; struct B { A b; };
0
9548
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
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
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 project—planning, coding, testing, and deployment—without 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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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
4607
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.