Hi all, I know you people are very busy but please go through this. It is about Heapless C/C++.
What is heapless C++? Heapless C++ is the concept in which you are forbidden to use heap of system. Why heapless C++? Because: 1. Stack is addressed by SP (stack pointer) which is a register of CPU and operates at the same clock at which CPU is operating while heap is not addressed by any such register. 2. Any address of a stack can be obtained by an offset but in heap the data structure in use will not allow you that. 3. Heap gets fragmented but stack never gets fragmented. Where is it used or useful? Embedded systems already do it. They do not use heap. How to use heapless C++? First thing which you have to remember is forget new() operator of C++ and malloc() of C. After this you need to set stack size of OS to a large value. In GNU/Linux you can do this by #ulimit -s integral_size_of_stack_KB Please refer to man page. I do not know how to do it in Windows but on a global scale but for one program you can define STACKSIZE in its def file. Surviving without new() new() returns a pointer to allocated object. So, use a reference in case you need a pointer. It is widely known that Bjarne Stroustrup says references are preferred. I am just saying you must use only references whenever pointers are required. More Advantages of heapless C++ Since nothing is on heap and no new is there all memory leaks are automatically gone. You do not even need a garbage collector like Java. For the same reason there will be no late binding (but you can still have polymorphic behavior) and your performance will improve. Real world example What can happen because of heap is that if you try to clean it on a large-scale system GC may take up mins. In real-time large-scale systems this is simply not acceptable. Manual deletion is a pain. Well, yes there are smart pointers but they incur overhead. For C we do not have references so we will have to use * but malloc family hasĀ be removed. Poposal We can first develop a virtual machine for C as C++ is much bigger and complex. However, my knowledge is limited and I have a full-time job to support my family so I cannot devote much time to it. Also, I have never written a compiler or interpreter though if I devote time I can. We will have to have some function like alloca() which will allocate memory on stack but this piece of memory must not be released when SP goes down the stack. A record of all allocated objects by this function has to be kept. Also, there will be gaps or fragmentation in Stack because of this for which we will have to move allocated area down almost in all cases and sometimes up. It may depend upon the specification. The C language can be used unaltered except heap allocation part. I would prefer something like Java that we have a bytecode format and core must be small. Around this core we can write bigger functionalities. Please share your thoughts. -- Best regards, Shiv Shankar Dayal