Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by iammilind for C++ allocating memory using array

One way is to have a memory pool declared in data segment and have your own memory allocation function.

char myMemoryPool[10000000]; // global variable (can be enclosed in namespace)

And you have to write your own memory manager:

void* MyMemoryAlloc (size_t SIZE){ //... use SIZE}

Usage:

A* p = (A*)MyMemoryAlloc(sizeof(A) * 3);

Or more precisely,

template<typename T>T* MyMemoryAlloc (unsigned int UNITS){//... use (sizeof(A) * UNITS)}

Usage:

A *p = MyMemoryAlloc<A>(3);

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>