473,484 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alignment

How to declare a variable guaranteed to have the strictest possible
alignment?

--
The defense attorney was hammering away at the plaintiff:
"You claim," he jeered, "that my client came at you with a broken bottle
in his hand. But is it not true, that you had something in YOUR hand?"
"Yes," the man admitted, "his wife. Very charming, of course,
but not much good in a fight."
Aug 19 '07
55 2986
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
>>Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
I cannot imagine why one needs a strictest-aligned variable of
static duration, but the following would work
typedef union {
[...]
>>>} strict_align_t;
strict_align_t myvar;

Unsigned and complex stuff can be inferred to fit this alignment.

It's very likely to work, but it's certainly not guaranteed to work.
There are arbitrarily many types, and any of them could theoretically
have stricter alignment requirements than any of the types you've
listed.
[...]

Do you think the following code you work at least 80% of the time:
---------------
#include <stdio.h>
#include <stddef.h>
typedef struct aligned_s aligned_t;
typedef union aligned_offset_u aligned_offset_t;
typedef union aligned_ptrs_u aligned_ptrs_t;
typedef union aligned_uptrs_u aligned_uptrs_t;
typedef union aligned_ints_u aligned_ints_t;
typedef union aligned_uints_u aligned_uints_t;
typedef enum aligned_enum_u aligned_enum_t;
#if ! defined(ALIGN_TYPEUNION_APPEND)
typedef union aligned_append_u aligned_append_t;

union aligned_append_u {
char whatever[1024];
};

#define ALIGN_TYPEUNION_APPEND() aligned_append_t
#endif
enum aligned_enum_u {
aligned_enum_u_v1,
aligned_enum_u_v2,
aligned_enum_u_v3
};
union aligned_ptrs_u {
char* t_char;
short* t_short;
int* t_int;
long* t_long;
float* t_float;
double* t_double;
long double* t_long_double;
long long* t_long_long;
ptrdiff_t* t_ptrdiff;
void* t_void;
float* (*t_fptr_0) (long double);
long long* (*t_fptr_1) (void*);
void* (*t_fptr_2) (void*, ptrdiff_t*);
aligned_ints_t* t_ints;
aligned_ptrs_t* t_aptrs;
aligned_offset_t* t_offset;
aligned_enum_t *t_enum;
};

union aligned_uptrs_u {
unsigned char* t_uchar;
unsigned short* t_ushort;
unsigned int* t_uint;
unsigned long* t_ulong;
size_t* t_size;
float* t_ufloat;
double* t_udouble;
unsigned long long*t_ulong_long;
unsigned int* t_uint_ptr;
unsigned long* (*t_ufptr_1) (unsigned int);
unsigned char* (*t_ufptr_2) (double, long double);
aligned_uints_t* t_uints;
aligned_uptrs_t* t_uaptrs;
};
union aligned_ints_u {
char t_char;
short t_short;
int t_int;
long t_long;
float t_float;
double t_double;
long double t_long_double;
long long t_long_long;
ptrdiff_t t_ptrdiff;
aligned_enum_t t_enum;
};

union aligned_uints_u {
unsigned char t_uchar;
unsigned short t_ushort;
unsigned int t_uint;
unsigned long t_ulong;
unsigned long long t_ulong_long;
size_t t_size;
};
union aligned_offset_u {
aligned_ints_t t_ints;
aligned_uints_t t_uints;
aligned_ptrs_t t_ptrs;
aligned_uptrs_t t_uptrs;
ALIGN_TYPEUNION_APPEND() t_append;
};
struct aligned_s {
char base;
aligned_offset_t offset;
};
#define ALIGN_MAX_ESTIMATE() ( \
sizeof(aligned_t) sizeof(aligned_offset_t) \
? sizeof(aligned_t) - sizeof(aligned_offset_t) \
: sizeof(aligned_offset_t) \
)


int main(void) {
{
printf("estimated strict aligment: %u\n",
ALIGN_MAX_ESTIMATE());
}

puts("\n\n\n\
_______________________\npress <enterto exit...\n");
return getchar();
}

---------------

?

Aug 25 '07 #51
[...]
Do you think the following code you work at least 80% of the time:
[...]

That was suppose to read as:

'Do you think the following code might work at _least_ 80% of the time'
Aug 25 '07 #52
Chris Thomasson wrote:
>
union aligned_ptrs_u {
char* t_char;
void* t_void;
t_char and t_void
have the same representation and alignment requirements.

--
pete
Aug 25 '07 #53
"Chris Thomasson" <cr*****@comcast.netwrote in message
news:cq******************************@comcast.com. ..
[...]
Do you think the following code you work at least 80% of the time:
[...]

A user can mutate the posted alignment code with the
'ALIGN_TYPEUNION_APPEND' macro function as follows:

-----------
typedef union aligned_your_special_u aligned_your_special_t;

union aligned_your_special_u {
int whatever_types_you_need;
};

#define ALIGN_TYPEUNION_APPEND() aligned_your_special_t
-----------
So, this might be 81% reliable...

;^)

Aug 25 '07 #54
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Chris Thomasson" <cr*****@comcast.netwrites:
>[...]
>>Do you think the following code you work at least 80% of the time:
[...]

That was suppose to read as:

'Do you think the following code might work at _least_ 80% of the time'

Maybe.

If I find a platform where if fails, and run it 99 times on that
platform and once on a platform where it works, then it will fail 99%
of the time.
Well, we should measure per-platform, not per-run? So that would be 1
failure and 1 success...

;^)

[...]

Aug 25 '07 #55
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Chris Thomasson" <cr*****@comcast.netwrites:
>[...]
>>Do you think the following code you work at least 80% of the time:
[...]

That was suppose to read as:

'Do you think the following code might work at _least_ 80% of the time'

Maybe.

If I find a platform where if fails, and run it 99 times on that
platform and once on a platform where it works, then it will fail 99%
of the time.
[...]

Can you think of an existing platform where the code would most likely
break?

Aug 25 '07 #56

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

Similar topics

4
17671
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
10
3195
by: j0mbolar | last post by:
for any pointer to T, does a pointer to T have different or can have different alignment requirement than a pointer to pointer to T? if so, where is the exact wording in the standard that would...
67
10649
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
2022
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
13
2963
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
12
818
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
10
2176
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
2
3767
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
2
20107
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
6945
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
7100
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,...
0
7136
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...
0
7163
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
5400
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,...
0
3042
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...
0
1350
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 ...
1
584
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
228
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...

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.