荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: bakey (猪朋狗友), 信区: Program
标  题: [合集] 哪位有malloc函数的实现?
发信站: 荔园晨风BBS站 (Sat Apr 29 20:36:09 2006), 站内

☆─────────────────────────────────────☆
   szuxjq (vincent) 于  (Wed Apr 26 17:56:35 2006)  提到:



最好是简单点的,太深奥的代码看不懂。。。


☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 18:46:28 2006)  提到:

原型:extern void *malloc(unsigned int num_bytes);
如:char* s;
   s=malloc(100*sizeof(char));
【 在 szuxjq (vincent) 的大作中提到: 】
: 最好是简单点的,太深奥的代码看不懂。。。





☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 19:01:10 2006)  提到:

因为它返回的是void*,是连续字节块的指针,所以最好是这样:
char* s;
s=(char*)(100*sizeof(char));//强制转换为指向100个字符空间的字符串指针,对于其它类
型或者结构体也是一样的
【 在 starsun (法克斯尼) 的大作中提到: 】
: 原型:extern void *malloc(unsigned int num_bytes);
: 如:char* s;
:    s=malloc(100*sizeof(char));





☆─────────────────────────────────────☆
   Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 于  (Wed Apr 26 19:04:08 2006)  提?

他应该是问内部实现代码吧。。

【 在 starsun (法克斯尼) 的大作中提到: 】
: 原型:extern void *malloc(unsigned int num_bytes);
: 如:char* s;
:    s=malloc(100*sizeof(char));
: 【 在 szuxjq (vincent) 的大作中提到: 】
: : 最好是简单点的,太深奥的代码看不懂。。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 19:07:54 2006)  提到:

不是吧...内部代码-_-0?怎么可能?
【 在 Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 的大作中提到: 】
: 他应该是问内部实现代码吧。。





☆─────────────────────────────────────☆
   Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 于  (Wed Apr 26 19:09:53 2006)  提?

stl都有源代码。。
库函数应该也有吧。。
但我没找过。。
【 在 starsun (法克斯尼) 的大作中提到: 】
: 不是吧...内部代码-_-0?怎么可能?
: 【 在 Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 的大作中提到: 】
: : 他应该是问内部实现代码吧。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 19:11:43 2006)  提到:

个人感觉这个函数不应该是C的语法,跟编译器有关...
【 在 Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 的大作中提到: 】
: stl都有源代码。。
: 库函数应该也有吧。。
: 但我没找过。。





☆─────────────────────────────────────☆
   mmkiller (追求新鲜的好奇宝宝) 于  (Wed Apr 26 19:11:56 2006)  提到:


 汇编

【 在 szuxjq (vincent) 的大作中提到: 】
: 最好是简单点的,太深奥的代码看不懂。。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 19:12:31 2006)  提到:

赞成~~
【 在 mmkiller (追求新鲜的好奇宝宝) 的大作中提到: 】
:  汇编





☆─────────────────────────────────────☆
   nic (Ghost) 于  (Wed Apr 26 19:45:08 2006)  提到:

http://fxr.watson.org/fxr/source//kern/kern_malloc.c
【 在 szuxjq (vincent) 的大作中提到: 】
: 最好是简单点的,太深奥的代码看不懂。。。





☆─────────────────────────────────────☆
   bso (meteor) 于  (Wed Apr 26 20:03:53 2006)  提到:

在DEV C++里看到下面的

#ifndef _MALLOC_ALLOCATOR_H
#define _MALLOC_ALLOCATOR_H 1

#include <new>

namespace __gnu_cxx
{
  /**
   *  @brief  An allocator that uses malloc
   *
   *  This is precisely the allocator defined in the C++ Standard.
   *    - all allocation calls malloc
   *    - all deallocation calls free
   *
   *  (See @link Allocators allocators info @endlink for more.)
   */
  template<typename _Tp>
    class malloc_allocator
    {
    public:
      typedef size_t     size_type;
      typedef ptrdiff_t  difference_type;
      typedef _Tp*       pointer;
      typedef const _Tp* const_pointer;
      typedef _Tp&       reference;
      typedef const _Tp& const_reference;
      typedef _Tp        value_type;

      template<typename _Tp1>
        struct rebind
        { typedef malloc_allocator<_Tp1> other; };

      malloc_allocator() throw() { }

      malloc_allocator(const malloc_allocator&) throw() { }

      template<typename _Tp1>
        malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }

      ~malloc_allocator() throw() { }

      pointer
      address(reference __x) const { return &__x; }

      const_pointer
      address(const_reference __x) const { return &__x; }

      // NB: __n is permitted to be 0.  The C++ standard says nothing
      // about what the return value is when __n == 0.
      pointer
      allocate(size_type __n, const void* = 0)
      {
        pointer __ret = static_cast<_Tp*>(malloc(__n * sizeof(_Tp)));
        if (!__ret)
          throw std::bad_alloc();
        return __ret;
      }

      // __p is not permitted to be a null pointer.
      void
      deallocate(pointer __p, size_type)
      { free(static_cast<void*>(__p)); }

      size_type
      max_size() const throw()
      { return size_t(-1) / sizeof(_Tp); }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 402. wrong new expression in [some_] allocator::construct
      void
      construct(pointer __p, const _Tp& __val)
      { ::new(__p) value_type(__val); }

      void
      destroy(pointer __p) { __p->~_Tp(); }
    };

  template<typename _Tp>
    inline bool
    operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
    { return true; }

  template<typename _Tp>
    inline bool
    operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
    { return false; }
} // namespace __gnu_cxx

#endif

【 在 szuxjq (vincent) 的大作中提到: 】
: 最好是简单点的,太深奥的代码看不懂。。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 20:16:46 2006)  提到:

一看到就不想再看下去...
【 在 nic (Ghost) 的大作中提到: 】
: http://fxr.watson.org/fxr/source//kern/kern_malloc.c





☆─────────────────────────────────────☆
   bso (meteor) 于  (Wed Apr 26 21:03:13 2006)  提到:

偶还没到那种程度去看这些.......

【 在 starsun (法克斯尼) 的大作中提到: 】
: 一看到就不想再看下去...
: 【 在 nic (Ghost) 的大作中提到: 】
: : http://fxr.watson.org/fxr/source//kern/kern_malloc.c




☆─────────────────────────────────────☆
   szuxjq (vincent) 于  (Wed Apr 26 21:28:13 2006)  提到:


 看来我也没到那种程度。。。
【 在 starsun (法克斯尼) 的大作中提到: 】
: 一看到就不想再看下去...





☆─────────────────────────────────────☆
   Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 于  (Wed Apr 26 22:20:46 2006)  提?

还没到1k行啊。。。
【 在 nic (Ghost) 的大作中提到: 】
: http://fxr.watson.org/fxr/source//kern/kern_malloc.c
: 【 在 szuxjq (vincent) 的大作中提到: 】
: : 最好是简单点的,太深奥的代码看不懂。。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 22:41:36 2006)  提到:

不是一千行的问题,是那种机制看上去不易于理解~~
【 在 Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 的大作中提到: 】
: 还没到1k行啊。。。





☆─────────────────────────────────────☆
   bso (meteor) 于  (Wed Apr 26 22:46:48 2006)  提到:

很多预编译的.......

【 在 starsun (法克斯尼) 的大作中提到: 】
: 不是一千行的问题,是那种机制看上去不易于理解~~
: 【 在 Yeats (防火防盗防流氓,骗吃骗喝骗师妹~~~) 的大作中提到: 】
: : 还没到1k行啊。。。




☆─────────────────────────────────────☆
   starsun (法克斯尼) 于  (Wed Apr 26 22:47:56 2006)  提到:

他还定义了realloc,calloc
【 在 bso (meteor) 的大作中提到: 】
: 很多预编译的.......



[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店