Programming‎ > ‎C‎ > ‎

Misc

1. sizeof() is an operator.
    - To reduce function call overhead.
    - The argument Type is different, so overloaded functions are required.
    - int a[sizeof(int)]. Need runtime size.
    - If something can be done at compile time, do it there.
    - Run time does not have type info, to add type info in object data file, size is more. RTTI supporting language have larger size.
    - Derived *d = new Derived;
      Base *b = (Base *) d;
      sizeof(b) won't give size of Derived, so not done at function with runtime.
    - sizeof(array) returns the amount of memory used by all elements in array.
    - sizeof(pointer) only returns the amount of memory used by the pointer variable itself.
    - sizeof operator works on following kind of operands.
      sizeof(typename)
      sizeof(expression) Expression is not evaluated.
      int i = 5;
      int int_size = sizeof(i++);  // i is 5, int_size is compiler dependent.

2. malloc(0)
    If the size of the space requested is zero, the behavior is implementation defined, Null ot not Null. (memory should not be accessed)
    realloc(ptr, 0) returns Null.

3. How to detect corruption.
    Add magic value and used\free flag in header of malloc.

4. Execute code before\after main.
    #pragma startup show
    show();
    
    #pragma exit show
    main() 
    {
      atexit(show);
    }

5. Include header once.
    #pragma once
    
    #ifdef FILE_NAME
    #define FILE_NAME
    #endif

6. Little endian L.S.B is the smallest address 1000 CD
    Big endian M.S.B is the smallest address 1000 90
   Value = 90AB12CD
   
   int x = 1;
   if(*(char *) &x == 1)
     Little endian
   else
     Big endian

7. The C preprocessor (cpp) is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). 
  #include <stdio.h>
  int main (void)
  {
    printf("Hello, world!\n");
    return 0;
  }
  The entire text of the file 'stdio.h' replaces the #include directive.
    This can also be written using double quotes, e.g. #include "stdio.h". If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source directory. 

8. I'm trying to define a few simple little function-like macros, but they're not always working.
    #define square(x) x * x
    Macro expansion is purely textual.
    - The macro expansion must always be parenthesized to protect any lower precedence operators from the surrounding expression.
      #define square(x) (x * x)
    - All occurrences of the parameters must be parenthesized.
      #define square(x) ((x) * (x))
    - If the parameter appears several times in the expansion, the macro may not work  properly.
      square(i++)

9. I'm splitting up a program into multiple source files for the first time. What should I put in .c files and what should I put in .h files.
    As a general rule, put these things in header (.h) files.
    - Macro definitions (preprocessor #defines)
    - Structure, union, and enumeration declarations
    - Typedef declarations
    - External function declarations
    - Global variable declarations
    Put actual code and global variable definition in source (.c) files.
    Compile source files separately and use the linker to link the resultant object files together.
    Don't try to "link" all of your source files together with #include, the #include directive should be used to pull in header files, not other .c files.

10. The program crashes before it even runs!
    You probably have one or more very large local arrays.
    Your program has been linked incorrectly, that run-time dynamic library linking is failing for some reason.

11. I have a program that seems to run correctly, but it crashes as it's exiting, after the last statement in main().
    - If a semicolon in a previous declaration is missing, main might be inadvertently declared as returning a structure, conflicting with the run-time startup code's expectations.
    - If setbuf or setvbuf is called and if the supplied buffer is local to main, the buffer may not exist any more by the time the stdio library tries to perform the final cleanup.
    - A cleanup function registered by atexit may have an error. Perhaps it is trying to reference data local to main or to another function that no longer exists.

12. This program runs perfectly on one machine, but I get weird results on another. Stranger still, adding or removing debugging printouts changes the symptoms.
    - uninitialized local variables.
    - interger overflow.
    - undefined evaluation order.
    - omitted declaration of external functions.
    - dereferenced null pointers.
    - improper malloc\free use.
    - pointer problems in general.
    - mismatch between printf format and arguments, especially trying to print long ints using %d.
    - array overflow problems.

13. Find the next multiple of 8 of a number using only bitwise operator.
#include <stdio.h> int nextmul8(int n) { if(n & 7) //if it is not multiple of 8 { n &= ~7; //turn off last 3 bits //Add 8 to n int m = 8; while(n & m) { n &= ~m; m <<= 1; } n |= m; } return n; } 14. What is the difference between if-else and switch-case.
      switch-case uses only integer as condition, while if-else can use any condition.
      switch-case directly jumps to matching case for integer value, while if-else has to evaluate all intermediate if-else to reach the last if-else.
      case only uses constant value, integer is not allowed.


15. If the label is present in func1() and func2() calls goto label, will it work? If not, will it be compile or run time error?

      As per stack of func1(), the current stack contains func1() data.
      If the execution jumps to func2(), the stack of func1() is considered as func2() stack and hence invalid data access or return address will happen from func2().
      So, ideally this should be stopped by compiler and give compiler time error.
      Also, specific compiler might not give an error and the execution crashes at run time.

Comments