473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ 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 #1
55 3017

<fi******@inval id.comwrote in message
news:sl******** ************@no spam.invalid...
How to declare a variable guaranteed to have the strictest possible
alignment?
In practise, align to a double.
eg

union alignedint
{
double dummy;
int x;
}

x will now have double alignement.

In strictly correct terms, use compiler pragmas to achieve what you want.
double isn't guaranteed to be the most strictly aligned type and there is no
portable mechnaism for finding it.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 19 '07 #2
fi******@invali d.com wrote:
How to declare a variable guaranteed to have the strictest possible
alignment?
Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.

However, why do you need this? There's very little in standard C that you
can do with knowledge of alignment of non-dynamic objects. Even if you have
a structure containing an array of four chars, which is properly aligned
for an int, and sizeof(int)==4 on your platform, you're still not allowed
to access this structure as were it an int.
Aug 19 '07 #3
Harald van Dijk wrote:
fi******@invali d.com wrote:
>How to declare a variable guaranteed to have the strictest possible
alignment?

Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.
Sorry, this is of course incorrect. I was thinking of pointers to
structures. The below is still true, though you'd need to have another way
of making the structure properly aligned for it to be relevant.
However, why do you need this? There's very little in standard C that you
can do with knowledge of alignment of non-dynamic objects. Even if you
have a structure containing an array of four chars, which is properly
aligned for an int, and sizeof(int)==4 on your platform, you're still not
allowed to access this structure as were it an int.
Aug 19 '07 #4
fi******@invali d.com wrote:
>
How to declare a variable guaranteed to have the strictest possible
alignment?
Allocated objects have strictest alignment.

Does it have to be a declared variable?

--
pete
Aug 19 '07 #5
fi******@invali d.com wrote:
How to declare a variable guaranteed to have the strictest possible
alignment?
How to ask a question without a subject and verb?

I assume you mean something like "how can I declare a variable
guaranteed to have the strictest possible alignment?".

To get alignment suitable to any type, dynamically allocate it.
Otherwise, you can declare a union containing all the types that must be
aligned properly.

--
Thad
Aug 19 '07 #6
"Malcolm McLean" <re*******@btin ternet.comwrite s:
<fi******@inval id.comwrote in message
news:sl******** ************@no spam.invalid...
>How to declare a variable guaranteed to have the strictest possible
alignment?
In practise, align to a double.
eg

union alignedint
{
double dummy;
int x;
}

x will now have double alignement.
What if long double has stricter alignment requirements than double?
In strictly correct terms, use compiler pragmas to achieve what you
want. double isn't guaranteed to be the most strictly aligned type and
there is no portable mechnaism for finding it.
In strictly correct terms, (or, as I prefer to say, "correct terms")
there is no portable way to do this; the language doesn't define any
pragmas for this purpose.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 19 '07 #7
Keith Thompson wrote:
Harald van Dijk <tr*****@gmail. comwrites:
>fi******@invali d.com wrote:
>>How to declare a variable guaranteed to have the strictest possible
alignment?

Given that any object type can be contained in a structure or a union,
and all structure and union types have the same alignment requirements,
any structure or union type will do.
[...]

Not true. All *pointers* to structure or union types have the same
alignment requirements, but that refers only to the alignment of
pointer objects, not to the alignment of what they point to. It's
entirely possible for 'struct { int n; }' to have stricter alignment
than 'struct { char c; }', for example.
To clarify beyond the message I sent earlier: I used to believe that all
structures must have the same alignment requirements. I have never read a
satisfactory explanation of how the representation requirements for
pointers to structures do not imply this, I accept that the intent was that
different structures may have different alignment requirements, and all
that has nothing to do with my first message in this thread, which was
nothing more a slip of the mind unrelated to that old argument.

Or, to shorten it somewhat: agreed.
Aug 19 '07 #8

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"Malcolm McLean" <re*******@btin ternet.comwrite s:
><fi******@inva lid.comwrote in message
news:sl******* *************@n ospam.invalid.. .
>>How to declare a variable guaranteed to have the strictest possible
alignment?
In practise, align to a double.
eg

union alignedint
{
double dummy;
int x;
}

x will now have double alignement.

What if long double has stricter alignment requirements than double?
Computer like things in units of powers of two.
doubles are almost always 64 bits, in fact have to be if IEEE. long doubles
are 80. So the alignment requirements for double are likely to be stricter.
However you are right, in practise isn't the same as "by the standard". It
might even be storing up trouble for the future to advise such a thing. I
haven't made my mind up about this one.
How about a new type. align_t ?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 19 '07 #9
fi******@invali d.com wrote:
How to declare a variable guaranteed to have the strictest possible
alignment?
I guess the first question is, what's alignment? C99 3.2 says
"requiremen t that objects of a particular type be located on storage
boundaries with addresses that are particular multiples of a byte address."

That's a motivational explanation (like a math explanation of a set) but
not a definition: addresses are not numbers, and a multiple of a byte
address is... what?

Is it fair to say that all we can say (axiomatically) that
- For a type T there is an alignment good for storing a T object, and
- if T *p is an address good for storing a T object, then so is p+1?
[And then whatever alignment hierarchy the standard defines.]

I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed
} strict_align_t;
strict_align_t myvar;

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

-- Ark
Aug 20 '07 #10

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

Similar topics

4
17676
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 b;
10
3208
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 suggest so?
67
10679
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 other. All pointers to union types shall have the same representation and alignment requirements as each other. Does it mean that *all* structure (or...
7
2025
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 (http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc), but I get no alignment at all when calling the class. George Shephard's site, while imminently useful, does not have an anwer for...
13
2978
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 sizeof(T). (Proof: In `T array;' both `array' and `array' must be correctly aligned.) Since `sizeof(unsigned char)' is divisible only by one and since...
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
2185
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; char nodes; //will be list_node1,list_node2... };
2
3780
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 Align; union header { struct {
2
20131
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 switch back to "Left" aligned for remaining paragraphs, the text within the Word document remained Centered. I'm using: VS2008 w/ .NET Framework 3.5,...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7956
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...
1
7469
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...
0
7808
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...
0
6040
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...
1
5368
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...
0
3498
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...
1
1935
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
0
757
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...

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.