char alignment in C

Discussion in 'C Programming' started by madhurak@cybage.com, Jan 11, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Chris Dollin Guest

    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.
    Chris Dollin, Jan 11, 2006
    #2
    1. Advertising

  3. Ico Guest

    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
    Ico, Jan 11, 2006
    #3
  4. 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) <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.
    Keith Thompson, Jan 11, 2006
    #4
  5. Jordan Abel Guest

    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".
    Jordan Abel, Jan 11, 2006
    #5
  6. Old Wolf Guest

    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)
    Old Wolf, Jan 12, 2006
    #6
  7. "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) <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.
    Keith Thompson, Jan 12, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    537
  2. wwj
    Replies:
    24
    Views:
    2,473
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    458
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    569
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,004
    Ian Collins
    May 9, 2006
Loading...

Share This Page