这两个函数哪一个效率更高些?比如在分配内存的操作要在循环体中执行几千次,那么用哪个函数好一些?
另:这两个函数分配内存的时候,有最小单位吗?是以字节为最小单位?(就是任何大小的内存均可分配),还是以4字节?或16字节?或4K字节?
6 个解决方案
#1
当你对windows的内存分配不熟悉的时候,建议你用new/delete来分配/释放内存。
如果你需要大量的小块内存,那么可以自己做内存块管理,先申请一大块的内存,然后自己进行分块管理,这样会比你直接通过new不断向系统要求分配内存的效率高很多。
如果你需要大量的小块内存,那么可以自己做内存块管理,先申请一大块的内存,然后自己进行分块管理,这样会比你直接通过new不断向系统要求分配内存的效率高很多。
#2
new delete +1
#3
用GlobalAlloc比较好一些, 可以申请比较大的内存.
一般用new更好.
一般用new更好.
#4
一般用New和Delete
#5
malloc:
The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information.
GlobalAlloc:
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
GlobalAlloc效率应该低一点,MSDN也说了,用HeapAlloc
The global functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a global function should be used.
The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information.
GlobalAlloc:
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
GlobalAlloc效率应该低一点,MSDN也说了,用HeapAlloc
The global functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a global function should be used.
#6
分具体情况一般来说mallo使用次数多很多
#1
当你对windows的内存分配不熟悉的时候,建议你用new/delete来分配/释放内存。
如果你需要大量的小块内存,那么可以自己做内存块管理,先申请一大块的内存,然后自己进行分块管理,这样会比你直接通过new不断向系统要求分配内存的效率高很多。
如果你需要大量的小块内存,那么可以自己做内存块管理,先申请一大块的内存,然后自己进行分块管理,这样会比你直接通过new不断向系统要求分配内存的效率高很多。
#2
new delete +1
#3
用GlobalAlloc比较好一些, 可以申请比较大的内存.
一般用new更好.
一般用new更好.
#4
一般用New和Delete
#5
malloc:
The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information.
GlobalAlloc:
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
GlobalAlloc效率应该低一点,MSDN也说了,用HeapAlloc
The global functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a global function should be used.
The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information.
GlobalAlloc:
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
GlobalAlloc效率应该低一点,MSDN也说了,用HeapAlloc
The global functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a global function should be used.
#6
分具体情况一般来说mallo使用次数多很多