How and why to prevent class inheritance in C#/.NET

Question: In .NET/C#, how does one prevent a class from being inherited by another class? In other words can the inheritance of class be blocked? Also, what is the reason one might want to block the inheritance chain?

The sealed keyword/modified in .NET can be used to block derivation from a class. An attempt to inherit from a class marked as sealed will result in a compile time error. Also, a sealed class cannot be an abstract class, since abstract classes are meant to be inherited and cannot be instantiated as is. In C#, structs are implicitly sealed hence they cannot be inherited. You do not need to use the sealed keyword on structs.

public sealed class MySealedClass

The code below will not compile.
public class MyDerivedClass : MySealedClass

As to why one might want to block inheritance there are 2 main reasons. One is that the programmer does not want a class to be inherited as it might be the last class in the inheritance chain. Second is for runtime performace gains. Making a class sealed tells the compiler that there are no virtual functions for the class that will be overridden and the compiler can optimize the method invocations by transforming virtual function invocations into non-virtual invocations. This saves the time for lookup in the vtable.

Difference Between calloc, malloc, and realloc

Question: What is the difference between malloc(), calloc(), and realloc()

malloc, calloc, and realloc are functions used for memory allocation in C/C++ languages. There are some fundamental differences on how the above functions work.
First of all realloc() is actually a reallocation function. It is used to resize a previously allocated (using malloc(), calloc(), or realloc()) block of memory to the desired size. Depending on whether the new size if less or more than the original size the block may be moved to new location.

Usage and example of realloc()

Function Prototype for realloc():
void *realloc(void *pointer, size_t size);

malloc() vs calloc()

There two basic difference between malloc() and calloc() functions:
1. malloc() allocates memory in bytes. So the programmer specifies how many bytes of memory malloc should allocate and malloc will allocate that many bytes (if possible) and return the address of the newly allocated chunk of memory.
Function prototype for malloc():
void *malloc(size_t size); //size is the number of bytes to allocate
On the other hand calloc() allocates a chunk of memory specified by a block/element size and the number of blocks/elements. 
Function prototype for calloc():

void *calloc(size_t nelements, size_t elementSize);

2. malloc() does not initialize memory after it allocates it. It just returns the pointer back to the calling code and the calling code is responsible for initialization or resetting of the memory, most probably by using the memset() function. On the other hand calloc() initializes the allocated memory to 0. calloc() is obviously slower than malloc() since it has the overhead of initialization, so it may not be the best way to allocate memory if you don't care about initializing the allocated memory to 0.

Side note: Don't forget to free your allocated memory when you are done with it using the free() function.

Must Read Books for Technical/Programming Interview Preparation

Coding, Program Design and Interview Questions:
  • Programming Interviews Exposed: Secrets to Landing Your Next Job ~ John Mongan, Noah Suojanen, and Eric Giguère
  • Programming Pearls (2nd Edition) ~ Jon Bentley
  • Expert C Programming: Deep C Secrets ~ Peter van der Linden
  • Puzzles for Programmers and Pros ~ Dennis Shasha
  • More Programming Pearls: Confessions of a Coder ~ Jon Bentley
  • Programming Challenges ~ Steven S. Skiena, Miquek Revilla

Data-Structures and Algorithms:
  • Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition) ~ Robert Sedgewick
  • Head First Object-Oriented Analysis and Design ~ Brett D. McLaughlin, Gary Pollice, and Dave West

  • Head First Design Patterns ~ Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra
  • Head First Object-Oriented Analysis and Design ~ Brett D. McLaughlin, Gary Pollice, and Dave West

  • Writing Secure Code, Second Edition ~ Michael Howard and David LeBlanc
  • Code Complete: A Practical Handbook of Software Construction ~ Steve McConnel

  • How would you move mount fuji? ~ William Poundstone
  • Aha! Insight ~ Martin Gardner