C - Design your own free( ) function

前端 未结 8 1500
感动是毒
感动是毒 2020-12-29 08:27

Today, I appeared for an interview and the interviewer asked me this,

  1. Tell me the steps how will you design your own f
相关标签:
8条回答
  • 2020-12-29 08:57

    You can't blindly design free() without knowing how malloc() works under the hood because your implementation of free() would need to know how to manipulate the bookkeeping data and that's impossible without knowing how malloc() is implemented.

    So an unswerable question could be how you would design malloc() and free() instead which is not a trivial question but you could answer it partially for example by proposing some very simple implementation of a memory pool that would not be equivalent to malloc() of course but would indicate your presence of knowledge.

    0 讨论(0)
  • 2020-12-29 08:58

    Tell me the steps how will you design your own free( ) function for deallocate the allocated memory.

    #include <stdlib.h>
    #undef free
    #define free(X) my_free(X)
    
    inline void my_free(void *ptr) { }
    

    How can it be more efficient than C's default free() function ?

    It is extremely fast, requiring zero machine cycles. It also makes use-after-free bugs go away. It's a very useful free function for use in programs which are instantiated as short-lived batch processes; it can usefully be deployed in some production situations.

    What can you conclude ?

    I really want this job, but in another company.

    0 讨论(0)
  • 2020-12-29 09:04

    malloc and free only have a meaning if your app is to work on top of an OS. If you would like to write your own memory management functions you would have to know how to request the memory from that specific OS or you could reserve the heap memory right away using existing malloc and then use your own functions to distribute/redistribute the allocated memory through out your app

    0 讨论(0)
  • 2020-12-29 09:04

    There is an architecture that malloc and free are supposed to adhere to -- essentially a class architecture permitting different strategies to coexist. Then the version of free that is executed corresponds to the version of malloc used.

    However, I'm not sure how often this architecture is observed.

    0 讨论(0)
  • 2020-12-29 09:05

    One common approach when you only have access to user space (generally known as memory pool) is to get a large chunk of memory from the OS on application start-up. Your malloc needs to check which areas of the right size of that pool are still free (through some data structure) and hand out pointers to that memory. Your free needs to mark the memory as free again in the data structure and possibly needs to check for fragmentation of the pool.

    The benefits are that you can do allocation in nearly constant time, the drawback is that your application consumes more memory than actually is needed.

    0 讨论(0)
  • 2020-12-29 09:05

    The knowledge of working of malloc() is necessary to implement free(). You can find a implementation of malloc() and free() using the sbrk() system call in K&R The C Programming Language Chapter 8, Section 8.7 "Example--A Storage Allocator" pp.185-189.

    0 讨论(0)
提交回复
热议问题