Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, The following piece of code found in a Linux user space program and it makes me confused: Code: void *wordAlign(void *p) { //aligning a pointer to 4 byte ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    4

    please give explanations about pointer alignment


    Hi all,

    The following piece of code found in a Linux user space program and it makes me confused:

    Code:
    void *wordAlign(void *p) {                      //aligning a pointer to 4 byte boundary
            return (void*)(((int)(p+3))&0xfffffffc);
    }
    In the program which contains above code, arguments passed in wordAlign() function are void pointers which were previously returned by malloc() and the return pointers are later accessed as integer pointer. So the purpose of wordAlign is clear. But I still have some questions:

    (1) I used to malloc a void pointer and later type cast to (int*) without any notice of address alignment (of course malloc size must be suitable for holding one or array of intergers) So I had to review about malloc and found malloc man page said "return value suitably aligned for any kind of variable" :P So can I conclude that above code is redundant (with the fact that arguments passed to it are just pointers returned by malloc)?

    (2) I wonder how the function wordAlign itself works. Suppose that some arguments passed in wordAlign actually weren't aligned. By the result of the wordAlign function, the newly returned pointer will move to the nearest aligned address. Will this overwrite some data which is already allocated in that address?

    Thank you!

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    So can I conclude that above code is redundant (with the fact that arguments passed to it are just pointers returned by malloc)?
    Yes.
    Will this overwrite some data which is already allocated in that address?
    If the pointer returned by wordAlign still points inside the region of memory allocated
    by malloc then no.

    What I find puzzling is that the function says that the pointers are
    alligned to a 4 byte boundary.It seems to me that the function only guarantees
    they will be alligned to a 2 byte boundary.Unless of course it is guaranteed
    that the argument to the function will already be alligned to a
    2 byte boundary.Since the function does not perform any checks on the
    argument given to it , but just goes straight and adds 3 I suspect this is
    indeed the assumption.

    It is rather odd.

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Please ignore the last 2 paragraphs of my previous post.When I wrote
    it I was under the impression than 8+4+2=12

    I guess it was late and all that.

  4. #4
    Just Joined!
    Join Date
    May 2005
    Posts
    4
    Thanks Santa's little helper!
    To me, it is obvious that the function is redundant. But the situation was that program is a believeable one (I know it was developed by some senior programmers), so I need others to consolidate my idea.

    But I still have wonder. It is obviou that the function is redundant because as said, malloc always return aligned pointer. But it is said for malloc by standard glibc. How about others like uclibc? I know that that program once was made to compile with uclibc? Is it one possible reason that people needed to align a pointer returned by malloc? While I am finding uclibc documentation, someone who know about it please tell me.

    Thanks a lot.

Posting Permissions

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