473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Flexible array member and alignement

Hello,
I want to do something similar to this:

#include <stdlib.h>

struct coord {
double val;
double err;
};

struct point{
double value;
char foo;
struct coord coords[];
};

struct expe{
struct point *pts;
int np;
int nc;
};

struct expe *create_expe(int nc, int np){
struct expe *expe_p;

/* check for NULL omitted for simplicity */

expe_p = malloc(sizeof *expe_p);
expe_p->pts = malloc(np * (sizeof *expe_p->pts
+ nc * sizeof *expe_p->pts->coords));

expe_p->np = np;
expe_p->nc = nc;

return expe_p;
}

struct point *point(struct expe *expe_p, int i) {
return (struct point *) (
(char *) expe_p->pts
+ i * (sizeof *expe_p->pts
+ expe_p->nc * sizeof *expe_p->pts->coords)
);
}

int main(void) {
struct expe *expe_p=create_expe(4,100);

point(expe_p,2)->value=3;
point(expe_p,2)->coords[1].val=41;
point(expe_p,2)->coords[1].err=34;

return 0;
}

It compiles wo. warning with gcc 4.1.0 and runs ok but I'm not
sure if the the char foo in points can mess up the alignement
requirements of elements of the pts array. Any language lawyer
out there?
Tobias.

May 20 '06 #1
2 2250
to*******@hotmail.com schrieb:
Hello,
I want to do something similar to this:

#include <stdlib.h>

struct coord {
double val;
double err;
};

struct point{
double value;
char foo;
struct coord coords[];
};
Note: C99 demands sizeof (struct point) == offsetof(struct foo, c)
for
struct foo {double value; char foo; struct coord c[1];};
but gcc 3.x chooses the size of struct point such that coords meets the
maximum alignment requirements.
In the case of double, this may be the same.
The gcc crowd holds this to be a defect in the C99 standard;
unfortunately, they removed the description of this particular issue
from the C99 status page but did not state how gcc 4.x behaves.
struct expe{
struct point *pts;
int np;
int nc;
};

struct expe *create_expe(int nc, int np){
struct expe *expe_p;

/* check for NULL omitted for simplicity */

expe_p = malloc(sizeof *expe_p);
expe_p->pts = malloc(np * (sizeof *expe_p->pts
+ nc * sizeof *expe_p->pts->coords));
Structures with flexible array member must not be elements of an array;
this is a constraint violation (c.f. C99, 6.7.2.1#2), i.e. we do not
need to consider np != 1.
Once again, due to your using double, this probably "works" as
"expected" but in the general case gives potentially rise to alignment
issues.
expe_p->np = np;
expe_p->nc = nc;

return expe_p;
}

struct point *point(struct expe *expe_p, int i) {
return (struct point *) (
(char *) expe_p->pts
+ i * (sizeof *expe_p->pts
+ expe_p->nc * sizeof *expe_p->pts->coords)
);
}

int main(void) {
struct expe *expe_p=create_expe(4,100);

point(expe_p,2)->value=3;
point(expe_p,2)->coords[1].val=41;
point(expe_p,2)->coords[1].err=34;
These accesses might fail.
return 0;
}

It compiles wo. warning with gcc 4.1.0 and runs ok but I'm not
sure if the the char foo in points can mess up the alignement
requirements of elements of the pts array.


A safe and correct solution is to declare
struct expe{
struct point **pts_p;
int np;
int nc;
};
where pts_p points to allocated storage equivalent to an array np
of struct point *. You have to allocate storage for the elements of
pts_p to point to separately.
This means more allocations but easier access; i.e.
struct expe *create_expe(int nc, int np){
struct expe *expe_p;
int i;

expe_p = malloc(sizeof *expe_p);
if (expe_p != NULL) {
expe_p->pts_p = malloc(np * sizeof *expe_p->pts_p);
if (expe_p->pts_p != NULL) {
for (i = 0; i < np; i++) {
expe_p->pts_p[i] = malloc(sizeof *(expe_p->pts_p[0])
+ nc * sizeof expe_p->pts_p[0]->coords[0]));
if (expe_p->pts_p[i] == NULL) {
break;
}
}
if (i < np) {
/* error handling omitted */
}
} else {
/* error handling omitted */
}
expe_p->np = np;
expe_p->nc = nc;
}

return expe_p;
}

struct point *point(struct expe *expe_p, int i) {
if (i >= 0 && i < expe_p->np)
return expe_p->pts_p[i];
else
return NULL;
}
or you can replace point(expe_p, i) by expe_p->pts_p[i].
The code above is untested.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 25 '06 #2
Michael Mair wrote:

Structures with flexible array member must not be elements of an array;
this is a constraint violation (c.f. C99, 6.7.2.1#2), i.e. we do not
need to consider np != 1.
Once again, due to your using double, this probably "works" as
"expected" but in the general case gives potentially rise to alignment
issues.
Thank for that clarification, it means it's hopless to do it in a
conforming
fashion. I managed to cook something up that works for any type but
requires the gcc __alignof__ extension. I'll just forget the idea.
A safe and correct solution is to declare
struct expe{
struct point **pts_p;
int np;
int nc;
};
where pts_p points to allocated storage equivalent to an array np
of struct point *. You have to allocate storage for the elements of
pts_p to point to separately.


Yeah, but no. Too many allocation, not 'local enough' instead I now do

struct point {
double val;
char foo;
};

struct expe{
struct expe *expe;
struct coord *coord;
int nc, np;
};

and malloc np expes and np*nc coord. It makes accessing the stuff
a bit ugly, but at least it's legal :)
Thanks again,
Tobias.

May 29 '06 #3

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

Similar topics

10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
2
by: DevarajA | last post by:
Can someone help me understand what flexible array members exactly are, how they behave and how could them be implemented by a i386? Also I didn't understand the two exceptions that the standards...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
20
by: mechanicfem | last post by:
I thought (as they're in c99) that flexible arrays were there for a number of reasons - but I also thought they'd be great for stepping into structures (say) that were aligned in memory after (say)...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
2
by: easter bunny | last post by:
Hi, I try to learn mmx and sse, to begin with i try to understand some examples i found on the net. Basically all __m128 vectors are 16 bit aligned, but one some examples saw them 64 aligned. Is...
10
by: mojmir | last post by:
hello, i've just encountered following piece of code: struct Vector { float x, y, z; inline float & operator (size_t i) {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...

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.