Anuja Changede
Years of experience : 3
Location : Pune
Key skills :S/W Testing, C, C++, Networking
|
|
C++: new operator and delete operator
Describe new operator and delete operator.
new and delete operators are provided by C++ for runtime memory management. They are used for dynamic allocation and freeing of memory while a program is running. The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new. The general form of using them is: p_var = new type; delete p_var; new allocates memory on the heap. If there is insufficient memory, then new will fail and a bad_alloc exception will be generated. The program should handle this exception. Consider following program: #include <iostream> #include <new> using namespace std; int main() { int *p; try { p = new int; //dynamic allocation of memory } catch (bad_alloc x) { cout << “Memory allocation failed”; return 1; } *p = 100; cout <<”P has value”<<*p; delete p; //free the dynamically allocated memory return 0; }
|
C++: C-string
What is C-string? Explain with an example.
What is C-string? Explain with an example. C-string is null terminated string which is simply a null terminated character array. C-string is essentially C style string. It is different than the way C++ handles strings-by providing string class. Its general form of declaration is: char var_name[size]; var_name is name given to the array variable Please note while declaring a C-string, we need to declare it to be one character longer that the largest string that it is to hold. For example, to declare a C-string str that can hold 10 characters, we need to write: char str[11]; One extra location is reserved to store the null char at the end of the string. Some of the most commonly used C-string functions are: strlen(s1) returns length of s1 strcpy(s1, s2) copies s2 into s1 strcmp(s1,s2) returns 0 if s1 & s2 are same, less than 0 if s1< s2, greater than 0 if s1>s2.
|
C++: Iterators
What are Iterators?
Iterators are the glue that hold the container and algorithms together. They provide an abstract view of data so that the writer of algorithm need not be concerned with concrete details of a myriad of data structures. At the same time, the standard model of data access provided by iterators relieves the containers from having to provide a more extensive set of access operations. An iterator is simply an abstract of the notion of a pointer to an element of a sequence. The key concepts of iterator are: - The element currently pointed to - Point to next element - Equality The iterator classes and functions are declared in namespace std and found in <iterator>. The iterator is not a general pointer. Hence there is no concept of ‘null iterator’. The test is to determine whether an iterator points to an element or not is done by comparing it against the end of its sequence. An iterator which points to an element is said to be valid and can be dereferenced using *, [] or ->.
|
C++: Const qualifier
What is const qualifier? Explain with an example.
The qualifier const can be applied to the declaration of any variable to indicate that its value will not be changed. Once a variable is declared with const qualifier, it no longer remains a variable (i.e. its value can not be changed). A const must be initialized with some value. e.g. const TYPE x = 10; //where TYPE is any data type. const int x = 5; // This will define x as constant with value 5 Note that once const is defined, its value can not be changed by arithmetic operations or assignment.
|
C++: Typecasting
What is Typecasting? Explain with examples.
Typecasting: C++ is very strict about type compatibility. Different variable types must be cast when their values are assigned to each other. Type cast operator is used for explicit type conversion of variables. A type name behaves as if it is a function for converting values to a designated type. E.g. average = sum/float(i); The variable ‘i’ is converted to float and used to calculate the average.
|
1 |
|