如何在不使用malloc或new运算符的情况下分配内存空间? [英] How to allocate memory space without using malloc or new operator?
本文介绍了如何在不使用malloc或new运算符的情况下分配内存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我的朋友昨天接受采访时,他被问到一个问题:
实现一个不使用* alloc或new操作符分配内存空间的函数,函数应该返回一个指向该地址的指针。
他和我都不能找到答案。
When my friend had his interview yesterday, he was asked a question: Implement a function that allocates memory space without using the *alloc or new operator, and the function should return a pointer to the address. Neither he nor I can find the answer.
推荐答案
我认为这个问题更多的是一个谜题显示了编程经验。我的解决方案将分配一个全局字节数组,将被用来代替堆:
I think the question is more of a puzzle than a question that shows experience with programming. My solution would be allocating a global byte-array, that would be used instead of the heap:
char heap[MAX_ALLOWED_MEM];
/*
The following function uses 'heap' as raw memory!
void* like_malloc(size_t bytes);
...
*/
这篇关于如何在不使用malloc或new运算符的情况下分配内存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文