new, No Memory Allocation

via Dynamic memory – C++ Tutorials.

When new throws exception

#include<iostream>
#include<new>

using namespace std;

int main(void)
{
     try
     {
          int * ptr = new int[100000000];
     }
     catch(bad_alloc &err)
     {
          cerr<<"Error: "<<err.what();
     }
}

When memory cannot be allocated further, then returning NULL from new expression

...
int *ptr = new (nothrow) int;
if(ptr == NULL) {cerr<<"Memory cannot be allocated.";}

If in the first case no try-catch would be used then program will crash. The first one is better than the second, because it is not required to check explicitly for the ptr to be NULL.

Advertisements

C vs. C++: Pass by value and reference

Recently I was reading insertion in a linked list in C and C++.
(Assuming that reader has some knowledge of basic coding of linked list)
If start pointer is passed in the function then double pointer is used in C and in C++, & is used to change start pointer(double pointer can also be used in C++, as C++ has many features of C).
C:

void insert(struct node** start, int val)
{
    ... //do something
    *start = somethingNew;
}
... //function will be called as
insert(&start);

C++:

void insert(struct node*& start, int val)
{
     ... //do something
     start = somethingNew;
}
... //function will be called as
insert(start);

By closely observing, it can be seen that in C there is nothing like passing by reference. C will always copy the argument(s) that will be passed to functions. In the above case(of C), the reference to start pointer is copied and the changes are done that will remain even after the function exits with respect to start node(extra: changes to the reference of start won’t be accounted outside). While C++ is genuine here, when it uses ampersand, it passes that object by reference and changes it.

To make it clear, lets take the case of insertion somewhere in the list, in that case the node after which some new node is to be inserted is passed to the function, in both languages single pointer does the job, Why?

void someFunction(struct node* someNode)
{
     someNode->data = 1;
     someNode->link = NULL;
     //someNode = someNewValue;
}

When some pointer is passed to this function, then copy of the address is made and when someNode->data (extracted as *someNode.data) is executed then the address would be dereferenced first, and data value is changed (similar for someNode->link).  Third line, if decommented, won’t make any difference outside this function (still not making any difference) as the copied address will change only.

#define and typedef

#define is a macro in C++ used as:
#define MACRO_NAME VALUE

For example, `#define SIZE 100`,
Macro makes the replacements at the time of preprocessing, before compilation. All the values are put in expanded code before usual compilation starts.

typedef gives another name to a type and it is used as:
typedef TYPE NEW_TYPE;

Unlike #define it is interpreted by compiler and works as a new name for `TYPE`

See they are looking similiar, if I write #define TYPE int or typedef int INT what’s the difference here.

Difference: #define CHAR_PTR char* and typedef char* CHAR_PTR, lets take them one by one:

#define CHAR_PTR char*
...
CHAR_PTR a, b; //typeid(a).name = char* and typeid(b).name = char
               //because it is simply a replacement
_________________________________________________________________

typedef char* CHAR_PTR;
...
CHAR_PTR a,c; //typeid(a).name() = typeid(b).name = char*

Sources: StackOverflow, Let Us C, The C Programming Language -K&R