0

My application is C++ bare metal based without any OS. If I never bother new, can I set the heap size to 0?

trincot
  • 317,000
  • 35
  • 244
  • 286
c-stranger
  • 691
  • 1
  • 7
  • 13
  • Could you clarify: your application needs to be *OS agnostic*, or it is somehow running without an operating system? If it's the latter, [please see this answer](http://stackoverflow.com/a/1552846/1717828). – user1717828 Jan 02 '16 at 18:01
  • My app is an embedded system with single thread which is the main reason that I don't use OS. – c-stranger Jan 02 '16 at 18:10
  • 1
    You don't need an OS to be able to use heap memory. If you're affraid of heap fragmentation, there are other ways to solve this. You could also avoid `new` or `malloc` if you reserve static arrays with predefined max values or global variables to hold your data. – huysentruitw Jan 02 '16 at 18:45
  • 4
    No, there's nothing in C++ that would let you set heap size. It's not clear why you would need any such tool. If you don't want to use heap, just don't use it. – n. m. could be an AI Jan 02 '16 at 18:47
  • 2
    If there is no OS, you will do the memory management your self. If you decide that you do not need a heap, then let your malloc always return nullptr. – user877329 Jan 02 '16 at 18:49
  • See http://stackoverflow.com/questions/21946870/is-it-possible-to-completely-avoid-heap-fragmentation – huysentruitw Jan 02 '16 at 18:53
  • @n.m. Please do not post answers as comments. – user1717828 Jan 02 '16 at 20:13
  • @n.m.I'm not sure if you are familiar with embedded system compiler such as IAR and crossworks, they do have heap size configuration. I have noticed that they reserve memory for heap when heap is configured to none zero even my code does nothing with new. – c-stranger Jan 02 '16 at 20:27
  • @user1717828 an answer would need to be a lot more extensive. – n. m. could be an AI Jan 02 '16 at 20:28
  • If your compiler lets you do that, do that. What's the question? – n. m. could be an AI Jan 02 '16 at 20:30
  • @n.m, My concern is that, if the heap size is configured to 0 and I can guarantee my code never touches 'new' then does my code has absolutely no problem with heap? For embedded system, memory always is expensive and not willing to be wasted. – c-stranger Jan 02 '16 at 20:42
  • @n.m., there is no "extensiveness" restriction on answers. Your comment concisely answers OP's questions. Please see [when should I (not) comment](http://stackoverflow.com/help/privileges/comment). – user1717828 Jan 02 '16 at 22:04
  • You need to consult your specific implementation's documentation, there's no general answer – n. m. could be an AI Jan 02 '16 at 22:29

1 Answers1

3

My application is C++ bare metal based without any OS.

Then what are your bindings to allocate heap memory?

Usually without any OS you don't have these anyways, and linking should fail if new is used anywhere in your code.

Hence it's not necessary to "set heap size to zero".

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • At least both IAR and Crossworks does reserve heap size if it is set to none zero even 'new' never been used. – c-stranger Jan 02 '16 at 20:05
  • @c-stranger Again, which bindings are used? I'd even call dynamic memory allocation kinda OS? So you can't claim you have none, and your code is addressing _bare metal_. – πάντα ῥεῖ Jan 02 '16 at 20:12
  • My app is embedded system and actually has nothing with 'binding', doesn't have any OS. After system reset, the code does simple initialization and then jump to 'main' and then my code will manage everything such as memory and peripheral. – c-stranger Jan 02 '16 at 20:56
  • @c-stranger So what does _"your code"_ when `new` is called anywhere actually? And before you're tempted to ask so: Yes, I'm familiar with embedded systems. – πάντα ῥεῖ Jan 02 '16 at 20:59
  • I understand that if 'new' is whenever used, a certain heap MUST be reserved, meaning that the heap size can't be set to 0. However the question is, if I don't use 'new', can I set heap to zero? – c-stranger Jan 02 '16 at 21:07
  • @c-stranger If your board configuration supports that setting, yes you can set it to zero and leave any available memory to the stack. – πάντα ῥεῖ Jan 02 '16 at 21:09
  • @c-stranger: At work, we have a zero sized heap area defined in the command file (icf). Because it is not used, the linker drops it from the executable, as it doesn't show up in the map. Thus IAR doesn't reserve heap size unless you tell it to do so. – Thomas Matthews Jan 02 '16 at 23:37