alignment in malloc

This is a discussion on alignment in malloc within the C Programming forums, part of the General Programming Boards category; how to code the following functions? please help! Thank you very much! void * aligned_malloc(size_t bytes, size_t alignment); void aligned_free(void* ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    alignment in malloc

    how to code the following functions? please help!
    Thank you very much!
    void * aligned_malloc(size_t bytes, size_t alignment); void aligned_free(void* p);
    aligned_malloc and aligned_free functions may only use the c runtime functions malloc and free in their implementation and cannot use any static memory.
    aligned_malloc takes the size of the buffer you would like to allocate and also alignemnt which is a power of two that will force the starting address of the buffer you return to the user to start on an alignemnt boundary.
    for example, a user may request 1000 bytes starting on a 128 byte boundary by calling aligned_malloc(1000, 128). aligned_free frees the buffer returned from aligned_malloc.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What part of this are you struggling with?

    It sounds very much like an assignment, so we need to follow the guidelines for homework, which is: You do the work, we help you with advice and clarficiations. So you need to tell us what you actually need us to help you with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    it is not a homework.
    i found this question online for preparing my interview.....
    the problem I am struggling with is how to alignment boundary when using malloc
    Thank you very much!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so if you only want to solve it for your own sake and don't care how you do it, then I suggest you google.

    If you want to solve it to learn from it, then we should treat it as a homework excercise, becasue me writing and posting the details of how you do it will only teach you the same as you would learn from google (aside from the possible benefit that if you google, you learn how to use google to find things on the 'net).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    I don't know what you want to know.
    I post here, because i cannot find a satisfied solution from google and i don't have any idea about how to solve this problem.
    anyway, thank you for your replying and recommendation

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm perfectly happy HELPING you, ok. I just don't want to "splat" the aligned_malloc post here on the page without you understanding what it means.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > I don't know what you want to know.
    > I post here, because i cannot find a satisfied solution from google and i don't have any idea about how to solve this problem.
    So have you ever tried to figure out the answer to a problem, or have you always taken the "google" way out?
    If you got this far with too much google, then to be honest, you haven't learnt much at all.

    In short, are you capable of original thought in solving problems, or are you just good at rehashing other peoples left-overs?

    Prospective employers are far more interested in people that can solve unique problems (where no amount of googling will help you). Being able to recite the language reference manual, or memorise by rote a whole bunch of online exercises might get you a job, but it sure won't help you keep it.

    I mean, whilst I was composing this missive, I thought of a couple of ways to solve the problem. And I wasn't even paying attention to solving it.

    If you really want to learn how to do this, you should be looking at "How would I solve it". Not running to google every time and typing in "C source code aligned malloc" for example.
    Being stuck and frustrated by a problem is all part of the deal - get used to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have written this sort of code many times, and I just wrote one for demonstration purposes. It is about 15 or so lines for aligned malloc and less than 5 lines of free.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to guarantee alignment with malloc and or new?
    By mynickmynick in forum C++ Programming
    Replies: 16
    Last Post: 08-27-2008, 02:49 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM
  5. malloc invalid address alignment
    By code2big in forum C Programming
    Replies: 15
    Last Post: 05-25-2004, 12:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21