472,992 Members | 3,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 4158
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) ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.