There are two differences. First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
Here are more opinions and answers:
- Calloc(m, n) is essentially equivalent to p = malloc(m*n); memset(p, 0, m*n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values or floating-point zero values. 'Free' is useable to release the memory allocated by malloc or calloc.
- Malloc(s); returns a pointer for enough storage for an object of s bytes. Calloc(n,s); returns a pointer for enough contiguous storage for n objects, each of s bytes. The storage is all initialized to zeros.
- Simply, malloc takes a single argument and allocates bytes of memory as per the argument taken during its invocation. Where as calloc takes two aguments, and calculates their product.
- A vector is a set of sequentially arranged data that can grow or shrink dynamically; a list is a set of linked items that are not necessarily sequentially arranged in memory.
- With a vector, you have random access to any element in constant time, as well as adding elements to the end of the vector in constant time. However, insertions and deletions take more time, proportionate to the number of subsequent or previous elements that need to be 'pushed down' or 'moved back' to make room for the new element or to contract the vector into the just 'emptied' slot.
With a list, you don't have random access (you must 'follow the links' to reach any element), but insertions and deletions are done in constant time. - List support bi-directional iterators while vector uses random access iterator.
- Vectors grow dynamically as items are added to them. When a vector grows, it doubles the number of items it can contain. This can force the entire vector to be moved to another spot in memory, which can be computationally expensive. Lists do not need to store their elements contiguously in memory, and can therefore grow or shrink one item at a time without ever needing to copy the entire list to a new location.
- In C++ it is allowed to define functions as part of a struct where as you can not define function in C structures.
- C++ struct supports inheritance where as C struct don't have the concept of inheritance
- C struct dont't have access limitations(data hiding) such as public, private or protected. Where as C++ struct have access limitations , the default access specifier is public
- struct keyword is required in C while creating a structure variable, whereas in C++ it is not required.
- In C sizeof empty struct is 0, whereas in C++ sizeof empty struct is 1.
Difference between C++ struct and class
- Members of a class are private by default, whereas members of a struct are public by default.
- Inheritance between classes is also private by default, and inheritance between structs is public by default.
- Note: The kind of inheritance is determined by the derived class being declared as a class or struct when an access specifier for the base class is absent.
- An abstract class is a class that has at least one pure virtual function, thus cannot be instantiated directly. An interface class would basically be an abstract class that is composed entirely of pure virtual functions.
- An interface defines no methods (just declares them) and holds absolutely no private data while an abstract class can do them both.
- An Abstract class lets you define some behaviors and force your subclasses to provide others. An interface is a contract, a specification that concrete classes MUST follow. It defines method signatures but cannot have any implementations; the latter must be provided by the classes that implement the interface.
The C language does not have a boolean type. Instead, it uses integer under the following general rules:
- An operation which requires a boolean value takes an integer and treats zero as false and any other value as true.
- An operation which produces a boolean value generates 1 or 0 for true and false.
- When an integer is used in a boolean context, it is converted under similar rules: zero becomes false, and nonzero becomes true.
- When a bool is used in an int context, it is converted to 1 or 0, for true or false.
Inline functions are similar to macros because they both are expanded at compile time, but the macros are expanded by the preprocessor, while inline functions are parsed by the compiler. There are several important differences:
- Inline functions follow all the protocols of type safety enforced on normal functions.
- Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
- Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.
- Macros are always expanded by preprocessor whereas compiler may or may not replace the inline definitions.
- Inline functions respect scope and can be put in a namespace.
- Inline functions can be overloaded.
- You can use the address of an inline function (e.g. as a functor).
- macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions.
- Because macros are expanded by the preprocessor, if incompatible types are passed compiler error messages will refer to the expanded macro, rather than the macro definition itself.