Welcome to EGSoftWeb

Programming
Blogs and Tutorials

Blogs on programming, technology and programming tutorials.

Desktop
Application

Antivirus, encryption/decryption, folder locking tools and miscellaneous desktop applications.

Programming Library

C++ drawing library for plotting shape in 2D, 3D, animation and many other functions.

Android
Application

Just gambling around the android programming and need to make a few useful android applications and tricky android puzzle games.

namespace in C++

 c++ tutorials |  Admin

For example:

namespace mycode {

     float pi = 3.1415;

    

     int factorial(int number)

     {

           if (number)

                return factorial(number - 1) * number;

           else

                return 1;

     }

}

 

int factorial(int number)

{

     int ret=1;

     for (int i = 1; i <= number; i++)

           ret = ret * i;

     return ret;

}

 

#include <iostream>

int main()

{

     std::cout << "factorial of 7 using recursive method = " << mycode::factorial(7) << "\n";

     std::cout << "area of circle of radius 1 is " << mycode::pi * 1 * 1 << "\n";

 

     std::cout << "factorial of  7 using for loop = " << factorial(7) << "\n";

     return 0;

}

 

Here we defined the namespace named as mycode and inside of it we defined a float variable pi and a function factorial and used these in the main function. A same named function of factorial is defined outside the namespace and we can easily call this without any name collision.

There is not any feature of namespace in C.

Multiple namespace with same name can be used in the code, all will follow same namespace and all declaration of identifiers can be used with the same name. For example:

namespace const_pi {

     float pi = 3.14156;

     float multiply2(float n) { return 2 * n; }

}

 

namespace  const_pi {

     float pi_2 = multiply2(pi);

}

 

namespace const_pi {

     float pi_4 = multiply2(pi_2);

}

 

#include <iostream>

int main()

{

     std::cout << "value of pi = " << const_pi::pi << "\n" ;

     std::cout << "value of 2*pi = " << const_pi::pi_2 << "\n";

     std::cout << "value of 4*pi = " << const_pi::pi_4 << "\n";

     return 0;

}

 

namespace can be defined only at global scope. (We cannot define a namespace in function/class etc.

Nested namespace are allowed. For example:

namespace n1 {

     int value = 1;

     namespace n2 {

           int value = 2;

           namespace n3 {

                int value = 3;

                namespace n4 {

                      int value = n1::value * n2::value * n3::value;

                }

           }

     }

}

 

#include <iostream>

int main()

{

     std::cout << "value in the namespace n4 is " << n1::n2::n3::n4::value;

     return 0;

}


using namespace:

See below 2 examples:

//Example1

#include <iostream>

int main()

{

     std::cout << "we are using std namespace without \"using\" keyword :)";

     return 0;

}

 

//Example2

#include <iostream>

using namespace std;

int main()

{

     cout << "we are using std namespace with \"using\" keyword :)";

     return 0;

}

Note: The definition of std namespace is in the header file <iostream>

Both example compile and execute without any problem.

Now see below examples:

//Example1

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

int main()

{

     print(ns1::value);

     print(ns2::value);

 

     return 0;

}

 

//Example2

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

using namespace ns1;

using namespace ns2;

 

int main()

{

     print(ns1::value);

     print(ns2::value);

     return 0;

}

 

//Example3

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

using namespace ns1;

using namespace ns2;

 

int main()

{

     print(value);

     return 0;

}

 

The Example1 and Example2 compile and execute without any problem, but Eample3 will not compile and show error as "value" is ambiguous”.

 

Here the compiler cannot identify “value” from which namespace, which is causing problem to identify which namespace is used.

Dynamic memory allocation

 c tutorials |  Admin

If size of array we declared is not fix, then we can allocate memory manually during run-time using inbuilt function like malloc, alloc etc.

This is known as dynamic memory allocation.

There are 4 library functions defined in <stdlib.h> makes dynamic memory allocation in C programming. These are malloc(), calloc(), realloc() and free().


1) malloc() - The name "malloc" stands for memory allocation.

The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of type void which can be casted into pointer of any form.

Syntax of malloc():

ptr = (cast_type*) malloc(byte_size)

 

Considering the size of int is 4 bytes, this statement allocates 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.

However, if the space is insufficient, allocation fails then returns a NULL pointer.

 

2) calloc() - The name "calloc" stands for contiguous allocation.

The malloc() function allocates a single block of memory. Whereas, calloc() allocates multiple blocks of memory and initializes them to zero.

Syntax of calloc()-

ptr = (cast_type*)calloc(n, element_size);

 

This statement allocates contiguous space in memory for n elements each with the size of element_size.

 

3) free() - Dynamically allocated memory created with either calloc() or malloc() doesn`t get freed on their own. We must use free() to release the space.

Syntax of free()-

free(ptr);

 

This statement frees the space allocated in the memory pointed by ptr.

 

4) realloc() - If the dynamically allocated memory is insufficient or more than required, we can change the size of previously allocated memory using realloc() function.

Syntax of realloc()-

ptr = realloc(ptr, x);

Here, ptr is reallocated with new size x.

Examples: