Find the answer to your Linux question:
Results 1 to 4 of 4
Is there anyway where I can store a variable at odd address location. Ex: int* s; In the above i want to allocate memory with starting address as odd number....
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    variable storing in odd address

    Is there anyway where I can store a variable at odd address location.
    Ex: int* s;
    In the above i want to allocate memory with starting address as odd number.

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Location
    Chennai, India
    Posts
    26

    But why?

    Quote Originally Posted by madhuti View Post
    Is there anyway where I can store a variable at odd address location.
    Ex: int* s;
    In the above i want to allocate memory with starting address as odd number.


    Yes, You can if your compiler allows you to.

    for example SDCC, a retargettable C compiler, allows you to declare variable storage locations by using __at keyword.

    * Check your system documentation and don't try this if you are not sure about what you want to do. The results can be unpredictable. On GCC you may endup with a segmentation fault.

  3. #3
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    115
    It also depends on whether your CPU architecture allows it. Some architectures don't allow multi-byte data types (like integers, floats, etc.) to reside at odd addresses. Attempting to store a multi-byte value at an odd address can result in a runtime exception in that case.

    Of course, if the compiler knows ahead of time that the value will be at that address, it can implement the correct sequence of calls to pull that data from memory into a register, or store the data from a register back into memory... If the architecture doesn't support non-aligned access, it's still possible, it just takes longer (multiple fetches, some bit shifts, etc...) because you can't use the regular old "store" and "fetch" operations.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by madhuti View Post
    Is there anyway where I can store a variable at odd address location.
    Ex: int* s;
    In the above i want to allocate memory with starting address as odd number.
    Strange question. Why do you need to do this?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...