4

In my understanding Templates allow us to write one function or class that works for different data types.

Containers such as a stack or linked lists are used to store data but can store one type of data at a time. To store store different types of data we are required to write different versions of the same container class. We can save this repetition of code by writing the class template.

I know it saves the writing effort on programmer's part. but I want to know does using templates saves memory or not.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Prashant Mishra
  • 167
  • 1
  • 9

5 Answers5

3

It depends of the template you write. First of all, if a template create a instance on every data type will waste memory. Simply, the code which is generated by the instantiation of the template will occupy program space for each instance. Having such a template is the same as writing each instance by hand.

On the other hand a template can be optimized a lot if the code of each instance of a given data type will always be the same. Think about a container template like vector which stores pointers to any kind of types. Why should we need extra code to store pointers to int instead of pointers to float? Take a look at the stl implementation for vector and look for specialisation for pointer types. All instances of the vector template for pointers derives from exactly one! base class which implements all needed functionality for pointers. It uses typically void pointers for that.

The only code that the instance for the given data type will create is the cast for the concrete pointer type. But a cast will not generate any processor executable code so it will be done without any program space cost.

And there is a huge amount of templates which will do operations during compile time. This is called meta template programming. This templates often generates no code at all and comes without any cost for program space.

In a short: You have to remember this 3 types of template code to get your answer.

Klaus
  • 24,205
  • 7
  • 58
  • 113
0

Your source files will contain less code. However your executable/binaries which will have template instantiations with concrete types may not.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

Each time you fill in a template variable, or a combination thereof, that has not been used by the compiler, the compiler will instantiate all the code for that template. This can lead to a considerable larger executable which in some situations is not desirable at all for instance if you are working on an embedded processor. Not everybody is working on a pc where the amount of memory is huge.

The process that is in effect is called code bloat. Sometimes the amount of code that is being generated is a lot more than you anticipated for instance when you are using STL. STL containers have quite a bit of code, there is the container , but there is also the iterators and so on.

Because template classes are based on header files, every method is inline. What the compiler will do with all this depends but the end result is that the use of a lot of template classes can blow up the size of your code.

This might not be a problem for you, for instance if you are writing software that is going to run on a pc, then it is not likely that this will impose a problem. But I am writing embedded software in C++ and templates are allowed but heavily discouraged because of there effect on the code size.

There are ways to minimize the code bloat, by using OO techniques, for instance instead of creating a vector for each type you can create a vector for the base type. Push as much functionality as possible to base classes where your template classes derive from and so on. This requires some experience.

Another thing worth mentioning is that when you are working with templates compilers have a tendency to create a big error message if something is wrong with the code. This error message can be hard to interpret by people who are not experienced with templates.

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

I know it saves the writing effort on programmer's part. but I want to know does using templates saves memory or not.

It all depends on how you use them...

It does not automagically reduce executable size or RAM memory, you can think of templates as a some kind of preprocessor that generates classes from some template, this way allowing you to work on higher abstraction levels. Created classes (or functions) this way are later on compiled as if you have written them your self. So it reduces visible source code size.

You can use templates metaprogramming for calculating things at compile time, so such use might reduce executable file size.

Templates can also increase executable size, its called code bloat. You can find on this here : How does template cause the code bloat in C++?, C++ Templates: Convincing self against code bloat. But with modern compiler/linker optimalizations its hard to judge.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
-1

What kind of memory are you talking about? Program space? Variables?

Templates do not save executable space. The executable space will be taken up whether you use a template or not. If you code up the same as the template, but changing the data type each time, you will not be saving code space, it is equivalent.

One advantage to templates is saving development time. In the present time, computers have more memory space so that is no longer an issue. The other issue is development time. If you can use a template to generate code, you will save a major portion of your development time. This accounts for less development costs and faster time to market. The faster you can get a project out, the sooner you can get money in and recover the money spent in development.

Take std::list for example. You can now have a linked list by supplying a data type. No need to write another copy. No need to debug the copy.

So don't worry about code bloat. Worry more about development time and quality of software.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154