Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allocate memory space without using malloc or new operator?

Tags:

c++

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.

like image 270
qwerty Avatar asked Apr 24 '11 16:04

qwerty


1 Answers

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);
   ...
*/
like image 162
Khaled Alshaya Avatar answered Oct 20 '22 20:10

Khaled Alshaya