Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char alignment in C

Reply
Thread Tools

char alignment in C

 
 
madhurak@cybage.com
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      01-11-2006
wrote:

> 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.

--
Chris "or maybe post-modern" Dollin
0, 1, 2, coffee, 4, 5, 6, coffee, 8, 9, coffee.
 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      01-11-2006
wrote:

> 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

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2006
Ico <> writes:
> wrote:
>> 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)


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?

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-11-2006
On 2006-01-11, Ico <> wrote:
> wrote:
>
>> 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)


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".
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      01-12-2006
Jordan Abel wrote:
>
> 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)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2006
"Old Wolf" <> writes:
> Jordan Abel wrote:
>> 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)


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.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57