C++库研究笔记——用C语言函数指针包装内存分配、释放
2013-08-21 22:49
357 查看
源自库:SuiteSparse_config
源码来自:SuiteSparse_config
/* Copyright (c) 2012, Timothy A. Davis. No licensing restrictions
* apply to this file or to the SuiteSparse_config directory.
* Author: Timothy A. Davis.
*/
对于操作大的数据,手工控制内存分配很重要的,写个wrapper 有时很必要。
源码来自:SuiteSparse_config
/* Copyright (c) 2012, Timothy A. Davis. No licensing restrictions
* apply to this file or to the SuiteSparse_config directory.
* Author: Timothy A. Davis.
*/
对于操作大的数据,手工控制内存分配很重要的,写个wrapper 有时很必要。
/* SuiteSparse-wide parameters will be placed in this struct. */ typedef struct SuiteSparse_config_struct { void *(*malloc_memory) (size_t) ; /* pointer to malloc */ void *(*realloc_memory) (void *, size_t) ; /* pointer to realloc */ void (*free_memory) (void *) ; /* pointer to free */ void *(*calloc_memory) (size_t, size_t) ; /* pointer to calloc */ } SuiteSparse_config ; void *SuiteSparse_malloc /* pointer to allocated block of memory */ ( size_t nitems, /* number of items to malloc (>=1 is enforced) */ size_t size_of_item, /* sizeof each item */ int *ok, /* TRUE if successful, FALSE otherwise */ SuiteSparse_config *config /* SuiteSparse-wide configuration */ ) ; void *SuiteSparse_free /* always returns NULL */ ( void *p, /* block to free */ SuiteSparse_config *config /* SuiteSparse-wide configuration */ ) ;
#include "SuiteSparse_config.h" /* -------------------------------------------------------------------------- */ /* SuiteSparse_malloc: malloc wrapper */ /* -------------------------------------------------------------------------- */ void *SuiteSparse_malloc /* pointer to allocated block of memory */ ( size_t nitems, /* number of items to malloc (>=1 is enforced) */ size_t size_of_item, /* sizeof each item */ int *ok, /* TRUE if successful, FALSE otherwise */ SuiteSparse_config *config /* SuiteSparse-wide configuration */ ) { void *p ; if (nitems < 1) nitems = 1 ; if (nitems * size_of_item != ((double) nitems) * size_of_item) { /* Int overflow */ *ok = 0 ; return (NULL) ; } if (!config || config->malloc_memory == NULL) { /* use malloc by default */ p = (void *) malloc (nitems * size_of_item) ; } else { /* use the pointer to malloc in the config */ p = (void *) (config->malloc_memory) (nitems * size_of_item) ; } *ok = (p != NULL) ; return (p) ; } /* -------------------------------------------------------------------------- */ /* SuiteSparse_free: free wrapper */ /* -------------------------------------------------------------------------- */ void *SuiteSparse_free /* always returns NULL */ ( void *p, /* block to free */ SuiteSparse_config *config /* SuiteSparse-wide configuration */ ) { if (p) { if (!config || config->free_memory == NULL) { /* use free by default */ free (p) ; } else { /* use the pointer to free in the config */ (config->free_memory) (p) ; } } return (NULL) ; }
相关文章推荐
- 数据结构 学习笔记之:关于顺序栈中给结构体类型指针分配内存时,使用malloc和不使用malloc的疑惑之解惑!
- C++多维指针的内存的分配与释放
- 深入理解C指针学习笔记---多级指针的内存分配问题(五)
- [delphi函数] Dispose 释放为指针分配的内存空间
- Delphi 给结构体指针分配内存,用new(p),释放用dispose(p)
- 无废话之指针研究二 内存分配与管理
- 关于C++中结构体及其指针成员的内存分配和释放
- C++编程入门系列之三十二(数组、指针和字符串:动态内存分配和释放)
- C++笔记(7):动态内存分配与释放函数
- 结构体指针数组 内存分配 释放
- 三十二、数组、指针和字符串:动态内存分配和释放
- 鸡啄米:C++编程入门系列之三十二(数组、指针和字符串:动态内存分配和释放)
- [笔记]dotNET内存的分配、指针
- 指向指针的指针的内存分配与释放
- 函数中的指针分配的内存怎么释放
- C和指针之动态内存分配堆、栈、全局区(静态区)、常量区对比总结学习笔记
- 数组、指针和字符串:动态内存分配和释放
- C++动态分配三级指针的内存与释放
- delphi 指针的内存分配和释放
- 结构体指针数组 内存分配 释放