Thread: Global variables usage

  1. #1
    Registered User
    Join Date
    Feb 2012

    Global variables usage

    I have a limitation of ram in my embedded application. I wanted to know if i can implement the complete solution without global variables or very less. Do I need to change my programming style? Will this method increase the flash memory?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Using either local or global variables means little for the system, since all of the running program has to be loaded unto RAM.

    No, what you need are memory-efficient algorithms. Worst case scenario, you could even use less variables if there are some you could reuse. But first work on your algorithms and find the best compromise of RAM vs CPU usage.
    Devoted my life to programming...

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    There are many factors to take into consideration when trying to optimize for memory usage. At the top should be a good understanding of C storage classes. Get a good book that explains to you the concept of scope, linkage and storage duration. C Primer Plus, by Stephen Prata should be a good option. You also need to know your system datatypes sizes and limitations. Take a look at the contents of <limits.h> so you can better understand if an int should be enough and a long is too much.

    It's not just global variables that should worry you. Variables declared at the so-called "global" scope are variables with static duration declared at the file scope. They will live throughout the entire duration of the program regardless of whether they have external or internal linkage. But variables declared at block scope with the static keyword will too. Avoiding file scope variables and static local variables is thus a good step.

    You should also try to locate functions in your code that are perhaps making use of too many variables and that can be optimized. Despite their memory being automatically made available as soon as the function ends, during the function lifetime they will consume their required space, which sometimes may be a little too much. And remember that functions that call functions that call functions, end up having all their combined variables in memory until the last function ends and the stack unwind begins.

    You should also get a good grasp of pointer usage and notation. Making good use of pointers will save you memory. char mystr[] = "This is a string." will actually require at least 36 bytes of memory, since the string literal that resides in static memory needs to be copied to the array (that is two copies of the same string and is wasteful). Whereas const char *mystr = "This is a string." will occupy 18 bytes plus whatever the size of a pointer is on your system (just one copy of the string plus a pointer). Of course, the pointer won't allow you to change the string, whereas the array will. So if you need to edit the string, the array option is required.

    The same is true of arrays of strings. Use pointers if you can. Not just because of the above, but also because in the case of string arrays, pointer notation allows you to create sparse arrays and declared arrays of strings won't. Get a good knowledge of arrays and pointers and their differences. Particularly in the case of strings this is essentially knowledge since strings usually represent a great chunk of runtime memory.

    Finally get also a good knowledge of allocated memory. The Stack (where most variables live) is of limited size. More so on systems with little memory. The other region of memory, the Heap where dynamically allocated variables live is also limited, but represent a whole new area of memory that isn't available to the Stack and that you can make use of. The trick is knowing how to take advantage of functions like malloc, when it makes sense to do so, and what the cares you must need to have when making use of this memory.

    Lastly, but probably not least, you need to learn some C idioms that can help you reduce the use of global variables while still being able to maintain state throughout your entire application. These are usually learned on a case by case basis.

    Others may have some additions opr corrections to make to my post. So stick around this thread and don't take my word alone for gospel.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Feb 2012
    My program is a microcontroller based so not sure if malloc and other features are valid since i don't use them.

  5. #5
    Registered User
    Join Date
    May 2010
    And remember that global variables are not always "evil" when dealing with memory constrained systems. Using a global buffer may be acceptable in some circumstances.

    Also if you're controller has multiple memory regions, don't forget to use those regions as well. If available your compiler will probably have keywords to allow creating variables in those regions.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  2. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 02:30 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  5. Global variables
    By Buckshot in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 12:53 PM

Tags for this Thread