char alignment in C

M

madhurak

Hello All,
I am facing a problem in C. I want that the char address should be
always an even address. Can we achieve this in C.

Thanks in advance for ur help.
Waiting for the reply.
Madhura
 
C

Chris Dollin

Hello All,
I am facing a problem in C. I want that the char address should be
always an even address.

What does "even address" mean to you in C?

Why do you want characters to have even addresses?
Can we achieve this in C.

Well, yes, it seems likely:

char array[2];

At least one of the elements should have an even address. I'm not
sure that's much help to you, though.
Thanks in advance for ur help.

It's not urhelp - it's modern help.
 
I

Ico

I am facing a problem in C. I want that the char address should be
always an even address. Can we achieve this in C.

malloc() 2 bytes of memory, look at the returned address and use only
the byte that has the even address. (and check for NULL)

Some compilers can be given hints about the alignment of data. The
method of doing this is highly compiler-specific, though. Check your
compiler's documentation.
Thanks in advance for ur help.

Please try your best to use proper english, avoid this kind of
'abbreviations', they only make you look childish.

Ico
 
K

Keith Thompson

Ico said:
malloc() 2 bytes of memory, look at the returned address and use only
the byte that has the even address. (and check for NULL)

malloc() always returns an address suitably aligned for any data type.
On most systems, that means the address will be "even".

It's not clear that it's necessarily meaningful in general for an
address to be "even", though it does have an obvious meaning on most
actual systems.

A question for the OP: why exactly do you need to do this?
 
J

Jordan Abel

malloc() 2 bytes of memory, look at the returned address and use only
the byte that has the even address. (and check for NULL)

For that matter, malloc is "suitably aligned for any type", so if
there's any chance of it mattering at all, the pointer returned by
malloc will be "even".
 
O

Old Wolf

Jordan said:
For that matter, malloc is "suitably aligned for any type", so if
there's any chance of it mattering at all, the pointer returned by
malloc will be "even".

What if the system has an odd number for the alignment?
eg. sizeof(int) is 3 and ints must be aligned to a multiple of 3.

(I'm sure there is none, but this is c.l.c after all)
 
K

Keith Thompson

Old Wolf said:
What if the system has an odd number for the alignment?
eg. sizeof(int) is 3 and ints must be aligned to a multiple of 3.

(I'm sure there is none, but this is c.l.c after all)

On such a system, there's very little chance that "even" alignment is
going to matter, so Jordan's qualified statement is very close to
being correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top