Friday, February 26, 2016

C - structures, initialized and uninitialized variables


  1. Structure alignment
  2. Trailing structure padding
  3. Trailing padding in bit fields


Following is the best link to study "C Structures"

Lost art of C structure packing: http://www.catb.org/esr/structure-packing/

Initialized and uninitialized variables:

All local variables & pointers are "uninitialized"
Local variables declared as "static" are initialized to zero or NULL

All global variables & pointers are initialized

Difference between global variable and static global variable:

  • Normal global variable can be used in other C files by use of "extern"
  • Static global variable cannot be used outside the file
Multiple file C project:
  1. Architecture of such project should be like
    1. user_main.c
    2. user_header.h
    3. file1.c
    4. file1.h
    5. file2.c
    6. file2.h
    7. file3.c
    8. file3.h
  2. Variables declared by individual c file (file1.c, file2.c file3.c) should be defined in c files itself
  3. Variable declarations should be included as part of header files(file1.h file2.h file3.h) as "extern" if these varaibles are needed outside the c file.
  4. user_header.h should include all these header files(file1.h file2.h file3.h) in it, so as get access of all variables defined the source files(file1.c, file2.c file3.c)
  5. In case of functions, function definition in c files & function prototype in header files
Multiple file C project is best for situations where
  • You want to group methods depending on there functionality. And you are the one whole and sole developer for the project. Where you don't have to share any of your code to others.
Shared objects:
  • Situations where you have to share your code to others, but you want to keep your code functionality hidden
  • The executable prepared by using shared object is small in size, but needs the .so or .dll at the time of compilation and execution
  • The application(exe) is already prepared that uses shared object & we need to optimize the algorithm inside the dll with same function prototype, then we just need to compile new shared object and replace the old. By this exe doesn't need to be recompiled.
  • These techniques of modularization of code by shared objects is used for most softwares installed on PC
  • Code Architectures come into play as, the function prototypes need to be design as part of  main source code with lot of future vision and modular scope in mind. Hence to be a code architect, you need to have lot of experience at first.
  • If we need to link new shared object to exe, then the exe need to be recompiled with new dll linked to it.
Static objects:
  • Executable prepared with static linking, the size is larger as compared to shared object
  • They are with .a extensions in Linux
  • When a executable is prepared with linking to these object file, it is called static linking
  • The concept is used in preparing mobile application eg. Apple applications, where the rule is to prepare app with static linking, if it is intended to be available on AppStore
  • It provides security kind of feature, as at runtime exe doesn't depends on any external file like in case of shared objects
At runtime, the memory allocation (RAM) takes place in almost the same size in shared object and static linking. Just a difference of few bytes, as in shared objects the address of calling the function from required .so/.dll comes into picture
Plugins:
They are basically shared objects only, just the extension names are different.

No comments:

Post a Comment

PROFILE

My photo
India
Design Engineer ( IFM Engineering Private Limited )

Followers