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

How to make local variable's address is 8 byte alignment?

HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?
Jan 10 '08 #1
15 4195
Pengjun Jia wrote:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Why is this important to you?
Is there any option to realize this?
This is specific to your platform, I'd think, so asking in a
platform-specific group would be more appropriate.

In general, I think you'd need to try playing tricks with embedding "p"
in a union (or possibly a structure) to force alignment.

For example, you may find that something like :-

union {
double for_alignment;
char p[1024];
} fudge.

Would get aligned at an 8-byte boundary.

But I repeat this is platform-specific, so YMMV...
Jan 10 '08 #2
Pengjun Jia wrote:
>
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?
/* BEGIN eight_bytes.c */

#include <stdio.h>

#define BYTES 8
#define NUM 256

int main(void)
{
int i[NUM + BYTES / sizeof(int)] = {10};
char *p = BYTES + (char *)i ;

printf("%p, %p\n", (void *)i, (void *)p) ;
return 0 ;
}

/* END eight_bytes.c */

--
pete
Jan 10 '08 #3
pete wrote, On 10/01/08 11:42:
Pengjun Jia wrote:
>HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?

/* BEGIN eight_bytes.c */

#include <stdio.h>

#define BYTES 8
#define NUM 256

int main(void)
{
int i[NUM + BYTES / sizeof(int)] = {10};
You have change the type of i which is likely to be a problem. You have
also failed to note that this may well *not* do what is wanted as the C
language does not guarantee how things will be arranged in memory. For
instance some compiler start playing clever tricks with where arrays are
placed on the stack and/or put "guard values" either side of arrays to
detect buffer overflows and/or try and detect when they have occurred.
char *p = BYTES + (char *)i ;

printf("%p, %p\n", (void *)i, (void *)p) ;
It would have been helpful if you had told the OP why you added the
casts. The reason being that %p specifically requires a void* pointer
and other pointer types can have different sizes, representations or
passing mechanisms.
return 0 ;
}

/* END eight_bytes.c */
--
Flash Gordon
Jan 10 '08 #4
Pengjun Jia wrote:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}
Well, you could always allocate an extra 8 bytes for the array p
and then use a pointer to the first position in that array which is on
an 8 byte alignment instead of p itself.

I don't see the point though for this example. With integers and such
it will likely make a difference if the variable is not properly
aligned, but the compiler will generally take care of that for you.

Regards,

David Mathog
Jan 10 '08 #5
David Mathog wrote:
Pengjun Jia wrote:
>HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

Well, you could always allocate an extra 8 bytes for the array p
and then use a pointer to the first position in that array which is on
an 8 byte alignment instead of p itself.

I don't see the point though for this example. With integers and such
it will likely make a difference if the variable is not properly
aligned, but the compiler will generally take care of that for you.
If the OP wanted to use a general area of bytes as a place he could take
a pointer into and manipulate as (for example) long or double, he may
need this sort of guarantee. Perhaps he'll tell us what he's trying to
do...
Jan 10 '08 #6
Pengjun Jia wrote:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?
Of course, the other approach is to malloc the space rather than using
automatic storage, as malloc() must return a block of memory
appropriately aligned for any kind of data, which implies the strictest
alignment constraint.
Jan 10 '08 #7
Flash Gordon wrote:
>
pete wrote, On 10/01/08 11:42:
Pengjun Jia wrote:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?
/* BEGIN eight_bytes.c */

#include <stdio.h>

#define BYTES 8
#define NUM 256

int main(void)
{
int i[NUM + BYTES / sizeof(int)] = {10};

You have change the type of i which is likely to be a problem.
Yes.
You have also failed to note that this may well *not* do what is
wanted as the C language does not guarantee how things will be
arranged in memory.
For instance some compiler start playing clever tricks with
where arrays are
placed on the stack and/or put "guard values" either side of arrays to
detect buffer overflows and/or try and detect when they have occurred.
char *p = BYTES + (char *)i ;
The C language guarantees this much for this case:
1 p is 8 bytes after i
2 p[0] through p[256 * sizeof(int) - 1] may be accessed

This line would have been better though:

int i[(1024 + BYTES) / sizeof(int)] = {10};

then it would have been p[0] through p[1023].

--
pete
Jan 11 '08 #8
On Thu, 10 Jan 2008 03:09:51 -0800 (PST), Pengjun Jia
<ji********@gmail.comwrote in comp.lang.c:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p's address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?
There is no valid or invalid option to align the "pointer p's address"
because 'p' is not, and never will be,a pointer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 11 '08 #9
On Thu, 10 Jan 2008 16:46:30 +0000, Mark Bluemel
<ma**********@pobox.comwrote in comp.lang.c:
David Mathog wrote:
Pengjun Jia wrote:
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}
Well, you could always allocate an extra 8 bytes for the array p
and then use a pointer to the first position in that array which is on
an 8 byte alignment instead of p itself.

I don't see the point though for this example. With integers and such
it will likely make a difference if the variable is not properly
aligned, but the compiler will generally take care of that for you.

If the OP wanted to use a general area of bytes as a place he could take
a pointer into and manipulate as (for example) long or double, he may
need this sort of guarantee. Perhaps he'll tell us what he's trying to
do...
No, he can't want to do that, it would produce undefined behavior.
Look up "declared type" in the standard. Since 'p' is defined as an
array of char, accessing it as objects type double or long is
undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 11 '08 #10
Yes, you are right. In later code I have a sentence like this : if
(*((long *) p) == 10000) ; { ... }
So I have to make sure p is 4 alignment (32-bit) and 8 alignment (64-
bit).
On 1ÔÂ11ÈÕ, ÉÏÎç12ʱ46·Ö, Mark Bluemel <mark_blue...@pobox.comwrote:
David Mathog wrote:
If the OP wanted to use a general area of bytes as a place he could take
a pointer into and manipulate as (for example) long or double, he may
need this sort of guarantee. Perhaps he'll tell us what he's trying to
do...
Jan 14 '08 #11
Pengjun Jia wrote: (Top-posting corrected
Mark Bluemel <mark_blue...@pobox.comwrote:
>If the OP wanted to use a general area of bytes as a place he could take
a pointer into and manipulate as (for example) long or double, he may
need this sort of guarantee. Perhaps he'll tell us what he's trying to
do...

Yes, you are right. In later code I have a sentence like this : if
(*((long *) p) == 10000) ; { ... }
So I have to make sure p is 4 alignment (32-bit) and 8 alignment (64-
bit).
I thought that must be it.

As I've already said, you need to either
a) use malloc() to get your memory
(that is guaranteed to be correctly aligned and usable for any
data type)
or
b) use a union to force alignment.
(I'm not convinced that this is actually guaranteed to work by
the standard, but in practice, I'm sure it will on most "normal"
hosted C implementations.)
Jan 14 '08 #12
Mark Bluemel wrote:
Pengjun Jia wrote: (Top-posting corrected
....
>Yes, you are right. In later code I have a sentence like this : if
(*((long *) p) == 10000) ; { ... }
So I have to make sure p is 4 alignment (32-bit) and 8 alignment (64-
bit).
....
b) use a union to force alignment.
(I'm not convinced that this is actually guaranteed to work by
the standard, but in practice, I'm sure it will on most "normal"
hosted C implementations.)
How could an implementation of C meet the requirements for how unions
work if one of the union members was misaligned for its type?
Jan 14 '08 #13
James Kuyper wrote:
Mark Bluemel wrote:
>Pengjun Jia wrote: (Top-posting corrected
...
>>Yes, you are right. In later code I have a sentence like this : if
(*((long *) p) == 10000) ; { ... }
So I have to make sure p is 4 alignment (32-bit) and 8 alignment (64-
bit).
...
> b) use a union to force alignment.
(I'm not convinced that this is actually guaranteed to work by
the standard, but in practice, I'm sure it will on most "normal"
hosted C implementations.)

How could an implementation of C meet the requirements for how unions
work if one of the union members was misaligned for its type?
My concern is whether Jack Klein's comment "Since 'p' is defined as
an array of char, accessing it as objects type double or long is
undefined" was justified...

In Pengjun's position, I'd be using malloc(), I think.
Jan 14 '08 #14
Mark Bluemel wrote:
James Kuyper wrote:
>Mark Bluemel wrote:
....
>> b) use a union to force alignment.
(I'm not convinced that this is actually guaranteed to work by
the standard, but in practice, I'm sure it will on most "normal"
hosted C implementations.)
How could an implementation of C meet the requirements for how unions
work if one of the union members was misaligned for its type?

My concern is whether Jack Klein's comment "Since 'p' is defined as
an array of char, accessing it as objects type double or long is
undefined" was justified...
I presumed that you were referring to replacing p with a object of a
union type that contained both double and long members. That avoids the
problem that Jack Klein was talking about, at least if the union is used
correctly: never read from a member of one type if the last write was to
a member of the other type.

Jan 14 '08 #15
Mark Bluemel wrote:
>
Pengjun Jia wrote: (Top-posting corrected
Mark Bluemel <mark_blue...@pobox.comwrote:
If the OP wanted to use a general area
of bytes as a place he could take
a pointer into and manipulate as (for example) long or double,
he may
need this sort of guarantee.
Perhaps he'll tell us what he's trying to
do...
Yes, you are right. In later code I have a sentence like this : if
(*((long *) p) == 10000) ; { ... }
So I have to make sure p is 4 alignment
(32-bit) and 8 alignment (64-bit).

I thought that must be it.

As I've already said, you need to either
a) use malloc() to get your memory
(that is guaranteed to be correctly aligned and usable for any
data type)
or
b) use a union to force alignment.
(I'm not convinced that this is actually guaranteed to work by
the standard, but in practice, I'm sure it will on most "normal"
hosted C implementations.)
The simplest way to acheive alignment and sufficient memory,
is to declare the whole array as being an array of type long,
and then point p, which is a (char *), at one of the members.

--
pete
Jan 14 '08 #16

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

Similar topics

4
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...
8
by: Groups User | last post by:
C allows type casting in which a variable is converted from one type to another. Does C (whatever standard) allow the type of a variable to change, within a statement, avoiding the conversion?...
11
by: Taran | last post by:
Hi all, I was wondering how does address alignment to x byte boundary is done. For example, if I say "adjust the requested size to be on a 4-byte boundary" or for that matter 8 byte boundary....
4
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
2
by: Avi Laviad | last post by:
hi, sorry for the dumb question but how do i code a bit variable in c? i mean - shoudlnt the synax need to be: bit j; j = 1; correct me if im wrong (and i suppose i am). Avi.
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
6
Facultas
by: Facultas | last post by:
Hi, I need to write a piece of code that will let me find the address boundaries of various local variables in order to determine their alignment. I was wondering if anyone would a) Be able...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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
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...

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.