0

I'm working in a very RAM- and progmem- constrained space. I have a small collection of related variables and functions that I'd like to group together in some meaningful way. This collection will be visible to the rest of the program.

My first impulse (and implementation, actually) is to create a class to group all this info together, then create one global instance of that class. I understand that this is the way one is supposed to do this. There is another way to do this using a namespace instead, however, which requires no instantiation.

My question boils down to this: does the namespace approach use less RAM? By that I guess I mean, does the instantiation of a class cause additional RAM usage or some other kind of overhead? Or perhaps does it use RAM in a different way at all, like perhaps instantiation causes all the variables to exist in the heap instead of some dedicated address?

trincot
  • 317,000
  • 35
  • 244
  • 286
Bo Thompson
  • 359
  • 1
  • 12
  • Yeah if you make a singleton object, it will be located in heap usually. But I think there will be no meaningful memory waste though you make it as an object unless you make virtual functions . – overfit Jan 28 '20 at 04:49
  • If you are anxious about heap usage, of course you can use stack mem instead, by making local (@main function) or global object. – overfit Jan 28 '20 at 05:03
  • Namespace = compile mode thing only. – Michael Chourdakis Jan 28 '20 at 05:29
  • @overfit is that correct? making a global object uses stack memory instead of heap memory? If so, that might be part of the answer to my question; maybe a local object is heap, maybe a global object is stack, and maybe namespace is stack with no instance overhead? – Bo Thompson Jan 28 '20 at 06:15
  • @BoThompson No, stack is the space used when you make static variables/objects. Heap is the space to allocate mem space when you make objects dynamically using new or new[] operators. The scope of a variable does not matter. – overfit Jan 28 '20 at 06:43
  • @BoThompson So the declaration of `your_class obj;` outside of your main function makes it into your stack. `your_class* p_obj;` is the declaration making a pointer variable p_obj into your stack. **Execution** of `p_obj = new your_class(...) ;` makes a your_class object into your heap and returns the object instance's address back to your global pointer variable p_obj. – overfit Jan 28 '20 at 06:56
  • That clears things up slightly. Thank you for clarifying. – Bo Thompson Jan 29 '20 at 02:21

1 Answers1

1

Adding a namespace doesn't really affect any part of the program runtime. It is simply a way for the compiler to find the relevant names, so it shouldn't consume any additional RAM. Each class instantiation would use RAM to store its member variables, but considering that those variables would still exists in the namespace example, it wouldn't be any different.

If you want, then write both methods and profile it, but I doubt either would have any significant impact (e.g. beyond a few bytes) on your program.

Leo Adberg
  • 342
  • 2
  • 18