473,322 Members | 1,403 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,322 software developers and data experts.

char alignment in C

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

Jan 11 '06 #1
6 2604
ma******@cybage.com 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.
Jan 11 '06 #2
Ico
ma******@cybage.com 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
Jan 11 '06 #3
Ico <us****@zevv.nl> writes:
ma******@cybage.com 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) ks***@mib.org <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.
Jan 11 '06 #4
On 2006-01-11, Ico <us****@zevv.nl> wrote:
ma******@cybage.com 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".
Jan 11 '06 #5
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)

Jan 12 '06 #6
"Old Wolf" <ol*****@inspire.net.nz> 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) ks***@mib.org <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.
Jan 12 '06 #7

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

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
2
by: derek.google | last post by:
I have an application that's crashing because of an alignment problem, and this is the smallest program that demonstrates what's happening: int main() { struct Message { unsigned short size;...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
13
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given...
2
by: ais523 | last post by:
I've been wondering about the compatibility of pointers to signed, unsigned and plain char, and it's not clear to me how interchangeable they are. Is the following portable? int main(int argc,...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
43
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize =...
4
by: rorni | last post by:
Hi, I'm porting code from Windows to HP-UX 11.23, using gcc (I'm begining to suspect this might not be the best choice, but I thought it would be most compatible since we've already compiled on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.