Wednesday 16 November 2011

Which memory model should be used? in C programming

Which memory model should be used?

If you are going to create a .COM file, the Tiny memory model must be used. All code, data, and stack space must fit in 64KB. This memory model is popular among small utility programs. The Small memory model is also used for relatively small programs, except that you are not limited to a total of 64KB for your entire program. In the Small memory model, you can have 64KB for your code space and 64KB for data and stack usage. Besides being used for small programs, the Small memory model is also used in environments such as Windows and for 32-bit compilers because memory addressing is not limited to DOS’s 16-bit constraints. 

If your program has a relatively large amount of code but relatively small amounts of static data, you can choose to write your program with the Medium memory model. If your program is extremely large (requiring many modules, code, and data), you might want to use the Large memory model. This memory model is most often used for writing business applications in DOS.

Use of the Compact and Huge memory models is much less common than use of the Small, Medium, and Large memory models. The Compact memory model enables you to have a large amount of static data but  a relatively small (64KB or less) amount of code. Programs that fit this model are rare and are typically restricted to conversion programs that have large amounts of static translation tables that must be stored in memory. The Huge memory model is identical to the large memory model, except that the Huge memory  model allows static data to be larger than 64KB. Like the Compact memory model, the Huge memory model is rare, primarily because its usage imposes a significant performance hit. Because of its relatively inefficient performance, you should avoid using the Huge memory model unless you absolutely must have an array or some other static data that is larger than 64KB. Keep in mind that arrays and other programming constructs can be allocated dynamically at runtime by using functions such as malloc() and calloc(), and they do not necessarily have to be static in nature.
 
Cross Reference:

XVIII.2: What are the differences between the memory models?
XVIII.3: What are the most commonly used memory models?

No comments:

Post a Comment