Bo Thompson
Bo Thompson

Reputation: 371

C++: Global Instance of Class vs Namespace: RAM Usage?

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?

Upvotes: 0

Views: 298

Answers (1)

Leo Adberg
Leo Adberg

Reputation: 342

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.

Upvotes: 1

Related Questions