473,386 Members | 2,114 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,386 software developers and data experts.

How to find the alignment requirements of a give type in the C99 standard?

6.2.5.6 says:

"For each of the signed integer types, there is a corresponding (but
different) unsigned integer type (designated with the keyword
unsigned) that uses the same amount of storage (including sign
information) and has the same alignment requirements."

However, I can't find any definition of the alignment requirements of
a given type via full-text search.

Below is my understanding:

To begin with code:

long long m = 0x12345678ABABCDCDLL;
long long* p = &m;
bool b = ((int)p) % 8);

To comply with the C99 standard, the compiler must ensure b is always
false. Is this correct? If this is correct, then where to find such
rules in the standard?

Aug 25 '07 #1
3 1626
>6.2.5.6 says:
>
"For each of the signed integer types, there is a corresponding (but
different) unsigned integer type (designated with the keyword
unsigned) that uses the same amount of storage (including sign
information) and has the same alignment requirements."

However, I can't find any definition of the alignment requirements of
a given type via full-text search.
They are implementation-defined.
>Below is my understanding:

To begin with code:

long long m = 0x12345678ABABCDCDLL;
long long* p = &m;
bool b = ((int)p) % 8);
((int)p) % 8 isn't guaranteed to compute anything useful. It might
extract the lower 3 bits of the memory segment number, which may
be a constant for all addresses in a small memory model. In
particular, it is *NOT* guaranteed to give any indication of whether
the long long m is aligned to a multiple of 8.

There is no guarantee that sizeof(long long) = 8, nor that the alignment
of a long long has anything to do with the number 8.
>To comply with the C99 standard, the compiler must ensure b is always
false. Is this correct?
No. It is possible that a long long is allowed to be byte-aligned.
>If this is correct, then where to find such
rules in the standard?
Aug 25 '07 #2
Thanks. I'm clear now.

Aug 25 '07 #3
go***********@burditt.org (Gordon Burditt) wrote:
6.2.5.6 says:

"For each of the signed integer types, there is a corresponding (but
different) unsigned integer type (designated with the keyword
unsigned) that uses the same amount of storage (including sign
information) and has the same alignment requirements."

However, I can't find any definition of the alignment requirements of
a given type via full-text search.

They are implementation-defined.
long long m = 0x12345678ABABCDCDLL;
long long* p = &m;
bool b = ((int)p) % 8);
There is no guarantee that sizeof(long long) = 8, nor that the alignment
of a long long has anything to do with the number 8.
However, there is a guarantee that the alignment of any type has
something to do with sizeof(that type). It is not possible for the
alignment requirements of <typeto be stricter (i.e., for <typeto be
required to be aligned on a larger amount of bytes) than sizeof(<type>),
because in a <typearray[], array[1] must have the address of array[0]
plus exactly sizeof(<type>) bytes.
It _is_ allowed for <type>'s alignment to be less restrictive than
sizeof(<type>), as Gordon already implied, and it's even allowed
(although I've never seen any such system, and would be highly surprised
if it existed) for <typeto need to be aligned to an address which, if
converted to a intptr_t, to be equal to N*sizeof(<type>)+(a constant <
sizeof(<type>) but >0).

That, I think, is all that's required. It isn't much.

Richard
Aug 27 '07 #4

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
10
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
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...
13
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
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
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;...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
55
by: fishpond | last post by:
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.