C Programming Tips
Memory Efficient Programming Tips
Ø Basic Memory efficiency programming
1. Basic mistakes we all use to do that while writing program always forget to deallocate the memory allocated, it is very important to deallocate memory.
2. Avoid memory allocation inside recursive function or loop.
3. During program we request some resources like file descriptor interrupt etc after using these it's very important to free it or close it.
4. Actually c program contain different type of library, while linking library to your program avoid using static library this will make your program size bigger go for other option like shared or dll libraries.
Ø Local vs. Global Variables
Try to use local variables as much as possible, avoid using Global variables. If you need global variables, tried to group the relative global variable in a structure and use that structure. These structures are then guaranteed to be placed as a single unit at link-time and can be accessed using a single base pointer. When system memory was expensive, local variables were used to conserve memory. Variables were created on demand, used for a while, and then destroyed so that the memory used to store their values could be recycled for other variables. Because Local variables are stored in stack. Variables that are declared on the stack are only valid within the scope of their declaration. That means when the function func() (listed below) returns, i and x will no longer be accessible or valid.
func()
{
int i = 10;
float x = 55.555;
-------
-------
}
The above example states that, when the local variable gone out of scope (function returns) that memory will get free and can be reused by other local variables (next function). But in case of global variable, if it is created then that particular memory will get blocked and can't reuse.
int g_buff[100];
main()
{
----
----
}
From the above code the memory occupied by global variable g_buff can't destroyed. So by reducing global variables we can reduce (little bit) memory utilization. Added global variable will get stored in Heap, as mentioned earlier local variable get stored in stack. Stack operation is much faster than Heap, so accessing the local variable will be faster than the global variable.
Ø Static vs. Dynamic memory
Don't allocate static memory for huge buffers (like char buff[2000]). Allocate memory dynamically and allocate memory exactly how much size you want. And as it mentioned above clear the memory then and there once its operation completes. This is very important; this will avoid memory leakage in your application.
Ø Data Types
Use the available data types efficiently. If you want a variable to hold 8bits of data use BYTE or char (unsigned char) depends on the compiler and platform you are working. Don't use int and float for everything. This will also do a small part in memory efficiency and this will be a good programming practice.
Ø Writing Readable code
It is necessary to write a readable code. Readable code in the sense that the other programmers should easily understand your code flow and logic used. For this, first main thing programmers should take care about indentation. Proper indentation and spacing are critical in making programs easy for people to read. Then next important thing is Naming of variables and constants. Your name given to the variable need to exhibit some information about them, data type, why it is used etc.
Comments
Post a Comment