admin@Cranes Varsity

Difference between constant pointer, the pointer to constant and both pointer to constant and constant pointer

Constant Pointers: A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable. Declaration of constant pointer: <type of pointer> * const <name of pointer> An example:                 int * const ptr; An example program: #include<stdio.h> int main() {     int n1 = 0, n2 = 0;     int *const ptr = &n1;     *ptr = 100;//valid     ptr = &n2;//Invalid     printf(“%d\n”, *ptr); } In the above example: We declared two variables n1 and n2 and constant pointer ‘ptr’ was declared and made to point n1. Next, ptr is made to point n2,   then print the value at ptr, but as per the constant pointer the pointer pointing an address cannot be change, so we will get the error mentioned below “ error: assignment of read-only variable ‘ptr’            ptr = &n2; ” but we can change the value at the pointer. Pointer to Constant: A pointer to a constant is a pointer that cannot change the value of the address its holding through the pointer. Declaration of pointer to a constant: <type of pointer> const * <name of pointer>                                     or const <type of pointer>* <name of pointer> An example:                 int const*ptr;                                 or                 const int *ptr; An example program: #include<stdio.h> int main() {     int n1 = 0, n2 = 0;     const int *ptr = &n1;     *ptr = 100;//Invalid     printf(“%d\n”, *ptr);//Invalid     ptr = &n2;//valid     printf(“%d\n”, *ptr); } In the above example:                 We declared two variables n1 and n2 and a pointer to constant ‘ptr’ was declared and made to point n1. Next, ptr is made to dereference, then print the value at ptr, but as per the pointer we cannot dereference, so we got the error mentioned below   error: assignment of read-only location ‘*ptr’      *ptr = 100;//Invalid But we can change the address of the pointer. Both pointer to constant and constant pointer: Both pointer to constant and constant pointer is a pointer that cannot change the address its holding and cannot dereference the value also. Declaration of constant pointer: const <type of pointer> * const <name of pointer> An example:                 const int * const ptr; An example program: #include<stdio.h> int main() {     int n1 = 20, n2 = 10;     const int *const ptr = &n1;     printf(“%d\n”, *ptr);//20     *ptr = 100;//Invalid     ptr = &n2;//Invalid } In the above example:     We declared two variables n1 and n2 , here pointer to constant and constant pointer ‘ptr’ was declared and made to point n1. Next, ptr is made to point n2,   then print the value at ptr, and change the address and dereference the value, both are violating the rules, so we will get the error mentioned below  “ error: assignment of read-only location ‘*ptr’      *ptr = 100;//Invalid           ^  error: assignment of read-only variable ‘ptr’      ptr = &n2;//Invalid ”

Difference between constant pointer, the pointer to constant and both pointer to constant and constant pointer Read More »

Data Types in C

Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These data types have different storage capacities. C language supports 2 different types of data types: Primary data types(primitive): These are fundamental data types in C namely integer(int), floating-point(float), character(char) and void. Derived data types: Derived data types are nothing but primary data types but a little twisted or grouped like array, structure, union and pointer. These are discussed in detail later. Here we will study primary or primitive datatype Primary data types: The regular integer that we use has 2 bytes size (16 bits) on a 16-bit machine. However, most the modern systems have 32 or 64-bit configurations. The size of an integer in such an environment is 4 bytes. Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is the list of ranges, with the memory requirement and format specifiers on the 32-bit GCC compiler.  DATATYPE: Int TYPE OF DATA: Integer MEMORY SIZE: 2 Bytes RANGE: -32768 to 32767 DATATYPE: Char TYPE OF DATA: Character MEMORY SIZE: 1 Byte RANGE: -128 to 127 DATATYPE: Float TYPE OF DATA: Floating point number MEMORY SIZE: 4 Bytes RANGE: 3.4e-38 to 3.4e+38 DATATYPE: Double TYPE OF DATA: Floating point number with higher precision MEMORY SIZE: 8 Bytes RANGE: 1.7e-308 to 1.7e+308 Program to check size of an datatypes: #include <stdio.h> int main() {          int a = 1;          char b =’G’;          float f = 7.77;          double c = 3.14;           printf(“Hello World!\n”);           printf(“I am a character. My value is %c and “          “my size is %lu byte.\n”, b,sizeof(char));           printf(“I am an integer. My value is %d and “          “my size is %lu bytes.\n”, a,sizeof(int));       printf(“I am a float with single floating point variable.”” My value is %f and   my size is %lu       bytes.\n”,f,sizeof(float));       printf(“I am a double floating point variable.”” My value is %lf and my size is %lu         bytes.\n”,c,sizeof(double));       printf(“Bye! See you soon. :)\n”);          return 0; } The output of the program: Hello World! I am a character. My value is G, and my size is 1 byte. I am an integer. My value is 1, and my size is 4 bytes. I am a float with a single floating point variable. My value is 7.770000, and my size is 4 bytes. I am a double floating-point variable. My value is 3.140000, and my size is 8     bytes. Bye! See you soon. 🙂

Data Types in C Read More »

Pass by Value And Pass by Address

There are two ways to pass arguments to a function — Pass by Value and Pass by Address. The major difference between Pass by Value and Pass by Address is in the pass by value copy of actual arguments is passed to respective formal arguments. While, in the call by reference, the location (address) of actual arguments is passed to formal arguments. Hence, any change made to formal arguments will also reflect in actual arguments. Pass by Value: A copy of actual arguments is passed to formal arguments of the called function, and any change made to the formal arguments in the called function does not affect the values of actual arguments in the calling function. Incall by value, actual arguments will remain safe they cannot be modified accidentally.  #include<stdio.h> void swap (int a, int b) {           int temp;           temp=a;           a=temp;           b=temp;           printf(“a=%d,b=%d”,a,b); } int main() {           int a=10,b=20;           swap(a,b);           printf(“a=%d\n,b=%d\n”,a,b); } output:           a=20,b=10(in function swap)           a=10,b=20(in main function) In the above program, a and b values are updated only in the function and not swap in the main function. Pass by Address: In Pass by address, the location (address) of actual arguments is passed to formal arguments of the called function. That means by accessing the addresses of actual arguments we can alter them within the called function. Alteration to actual arguments is possible within from called function. Therefore, the code must handle arguments carefully else you get unexpected results. #include<stdio.h> void swap (int *a,int *b) {           int temp;           temp=*a;           *a=temp;           *b=temp;           printf(“a=%d,b=%d”,*a,*b); } int main() {           int a=10,b=20;           swap(&a,&b);           printf(“a=%d\n,b=%d\n”,a,b); } output:           a=20,b=10(in function swap)           a=20,b=10(in main function) In the above program, a and b addresses are passed and in the function swap those addresses are received by the pointers updated values are reflected in the main function.

Pass by Value And Pass by Address Read More »

Macros vs Functions

Macros are preprocessed, meaning that all the macros would be executed before the compilation stage. However, functions are not preprocessed but compiled. Example of Macro: #include<stdio.h> #define  A 10 int main() {      printf(“%d”,A);      return 0; }  OUTPUT=10; Example of Function: #include<stdio.h> int A() {     return 10; } int main() {     printf(“%d”, A());     return 0; } OUTPUT=10; Now compile them using the command: GCC –E file_name.c This will give you the executable code as shown below: #include<stdio.h> #define  A 10 int main() {      printf(“%d”,A);      return 0; } #include<stdio.h> int A() {     return 10; } int main() {     printf(“%d”, A());     return 0; } The first program shows that the macros are preprocessed while functions are not. In macros, no type checking (incompatible operand, etc.) is done, and thus, the use of macros can lead to errors/side-effects in some cases. That is not the case with functions. Macros do not check for a compilation error. Macros are usually one-liners. However, they can consist of more than one line, and there are no such constraints in functions. The speed at which macros and functions differ. Macros are typically faster than functions as they don’t involve actual function call overhead. MACRO FUNCTION Macro is Preprocessed  Function is Compiled No Type Checking is done in Macro Type Checking is Done in Function Using Macro increases the code length Using Function keeps the code length unaffected Use of macro can lead to side effects at later stages Functions do not lead to any side effects in any case Speed of Execution using Macro is Faster Speed of Execution using Function is Slower Before Compilation, the macro name is replaced by macro value During function call, transfer of control takes place Macros are useful when small code is repeated many times Functions are useful when large code is to be written Macro does not check any Compile-Time Errors Function checks Compile-Time Errors

Macros vs Functions Read More »

Carbon Nanotube Field Effect Transistor

The present VLSI electronic systems rely on the Silicon MOS (metal oxide semiconductor) technology which advances will soon come to saturation. Carbon nanotubes represent an advancement in the materials technology with the potential for providing switching devices that may be faster and smaller than the present MOS devices. Carbon nanotubes are miniature tube structures with intriguing characteristics. The tube, in the normal untwisted state, conducts electricity. When twisted, the tube acts as a semiconductor.  This transistor is considered one of the greatest inventions of the twentieth century. It has helped to bring about both the information and computing age. One reason for success is its ability to decrease in size and increase in speed. This property is summarized in Moore’s law. It states that the transistor’s size will decrease exponentially while the speed will increase exponentially. Moore’s law has allowed the technology sector to progress and remain competitive. The physical barriers arise due to the continued shrinking of the current transistor used today, the Metal-Oxide Field Effect Transistor or MOSFET. As the size shrinks, the thickness of the insulators reduces. Insulators are used to electronically isolate parts of the transistor. With the thinner insulation, the carriers can quantum-mechanically tunnel across the insulation. The result is a short circuit allowing current to flow directly from the source to drain and then drain to the body. And even though the thin gate oxide, that separates the gate from the channel. In addition, doping becomes a problem since it relies on percentages. If the total amount of atoms gets very small, then a fractional dopant atom might be required, which of course, is impossible. In addition, economic problems arise from producing and maintaining the fabrication lines. One proposed solution is the use of carbon nanotubes instead of silicon to make the transistors. The construction and operation of CNFET are similar to the MOSFETs that we use today, thus giving them the name Carbon Nanotube Field-Effect Transistor or CNFET. Three of the most important characteristics of any transistor are speed, scalability, and power. Speed: The carbon nanotubes unique one-dimensional nature; can utilize ballistic transport. Ballistic transport means that the mean free path is longer than the path. Thus, the charge carriers do not collide, reducing resistance to negligible levels. The result is a capability to achieve speeds of Terahertz or more, compared to today’s processors that operate at 3 gigahertz. Scalability: A group in IBM discovered an interesting property of the CNFETs scalability. While the CNFETs improve with scaling, it is not conventional. They seem to follow the behaviour of Schottky barrier MOSFETs instead of regular MOSFETs. For this reason, the group at IBM feels that the CNFETs limits for scaling are unclear. However, they do note that, in a structured array, the CNFETs will produce enough gain and fan out for real-life applications. In addition to the CNFETs murky limits of scaling, it still will outperform silicon MOSFETs limits of scaling. Power: The same group at IBM compared some properties of the CNFET to both a high-performance silicon MOSFET and a newer MOSFET design that utilize Silicon-On-Insulator (SOI) technology. The results are displayed in table 1. Table 1: Comparison between MOSFET and CNT Circuit FET Delay (in pico second) Power (in micro watt) Inverter CMOS 16.58 9.81 CNT 3.78 0.25 2 Input NAND CMOS 24.32 20.67 CNT 5.98 0.69 2 Input NOR CMOS 39.26 22.13 CNT 6.49 0.48 One important difference is in I(OFF). The CNTFET has a drop of about 70% as compared to the conventional MOSFET. That emphasis on power being wasted while the transistor is off is greatly reduced. In addition, we notice that I(ON), or drive current, is larger than both technologies. In fact, it is three to four times larger. Normally, we would think that this is a bad thing. As our first instinct would mean higher power consumption. However, since the nanotube has ballistic conductance, it actually has a smaller resistance. Thus, the power consumption is the same if not smaller than the current MOSFET design. This is also supported by the two to four times increase in trans-conductance. The real big surprise is that the CNTFET is able to outperform both the current and newer technologies, despite the large gate length and gate oxide thickness. So naturally, when the CNFET design is optimized, the CNTFET will surely outperform the current technology. For these reasons, the CNTFET is a very strong contender to replace the current technology.

Carbon Nanotube Field Effect Transistor Read More »

C++ Friend function & Friend Class

If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function. By using the keyword, the friend compiler knows the given function is a friend function. For accessing the data, the declaration of a friend function should be done inside the body of a class, starting with the keyword friend. Declaration of friend function in C++ Class class_name { friend data_type function_name(argument/s); // syntax of friend function. }; In the above declaration, the friend function is preceded by the keyword friend. The function can be defined anywhere in the program like a normal C++ function. The function definition does not use either the keyword friend or scope resolution operator. Characteristics of a Friend function: C++ friend function Example Let’s see the simple example of the C++ friend function used to print the length of a box.             #include <iostream>             using namespace std;             class Box             {                         private:                         int length;             public:             Box() : length(0) {}             friend int printLength(Box); // friend function             };             int printLength(Box b)             {                         b.length + = 10;                         return b.length;             }             int main()             {                         Box b;                         cout  << “Length of box: “ << printLength(b) << endl;                         return 0;             } Output: Length of the box: 10 C++ Friend class A friend class can access both private and protected members of the class in which it has been declared as a friend. Let’s see a simple example of a friend class.             #include <iostream>             using namespace std;             class A             {                         int x =5;                         friend class B; // friend class             };             class B             {                         public:                         void display(A &a)                         {                                     cout << “value of x is : “ << a.x << endl;                             }             };             int main()             {                         A a;                         B b;                         b.display(a);                         return 0;             } Output: Value of x is: 5 In the above example, class B is declared as a friend inside the class A. Therefore, B is a friend of class A. Class B can access the private members of class A.

C++ Friend function & Friend Class Read More »

C++ Copy Constructor

A Copy constructor is an overloaded constructor used to declare and initialize an object from another object. Copy Constructor is of two types: Default Copy constructor: The compiler defines the default copy constructor. If the user defines no copy constructor, the compiler supplies its constructor. User-Defined constructor: The programmer defines the user-defined constructor. Syntax Of User-defined Copy Constructor: Class_name(const class_name &old_object); When Copy Constructor is called Copy Constructor is called in the following scenarios: Two types of copies are produced by the constructor: Shallow Copy #include <iostream>      using namespace std;       class Demo      {          int a;          int b;          int *p;          public:          Demo()          {              p=new int;          }          void setdata(int x,int y,int z)          {              a=x;              b=y;              *p=z;          }          void showdata()          {              std::cout << “value of a is : ” <<a<< std::endl;              std::cout << “value of b is : ” <<b<< std::endl;              std::cout << “value of *p is : ” <<*p<< std::endl;          }      };      int main()      {        Demo d1;        d1.setdata(4,5,7);        Demo d2 = d1;        d2.showdata();          return 0;      }  Demo d2 = d1; calls the default constructor defined by the compiler. The default constructor creates the exact copy or shallow copy of the existing object. Thus, the pointer p of both the objects point to the same memory location. Therefore, when the memory of a field is freed, the memory of another field is also automatically freed as both the fields point to the same memory location. This problem is solved by the user-defined constructor that creates the Deep copy. Deep copy Deep copy dynamically allocates the memory for the copy and then copies the actual value both the source and copy have distinct memory locations. In this way, both the source and the copy are distinct and will not share the same memory location. Deep copy requires us to write the user-defined constructor. #include <iostream>      using namespace std;      class Demo      {          public:          int a;          int b;          int *p;          Demo()          {              p=new int;          }          Demo(Demo &d)          {              a = d.a;              b = d.b;              p = new int;              *p = *(d.p);          }          void setdata(int x,int y,int z)          {              a=x;              b=y;              *p=z;          }          void showdata()          {              std::cout << “value of a is : ” <<a<< std::endl;              std::cout << “value of b is : ” <<b<< std::endl;              std::cout << “value of *p is : ” <<*p<< std::endl;          }      };      int main()      {        Demo d1;        d1.setdata(4,5,7);        Demo d2 = d1;        d2.showdata();        return 0;      }  Demo d2 = d1; calls the copy constructor defined by the user. It creates the exact copy of the value types data and the object pointed by the pointer p. Deep copy does not create the copy of a reference type variable.

C++ Copy Constructor Read More »

Bootstrapping in UNIX / LINUX

Bootstrapping in computer science is the technique for producing a self-compiling compiler. That is compiler/assembler written in the source programming language that it intends to compile. An initial core version of the compiler is generated in a different language mostly assembly language. Successive developed versions of the compiler are developed using this minimal subset of the language. When a computer is turned on is booted up with the code that is stored in ROM. The same code tries to figure out how much to load and start your kernel. The kernel verifies all the system’s hardware and initializes the system’s init process, which is always PID 1. A lot of things will happen before a login prompt can appear for the user to log in. File systems must be checked and mounted, and the system daemons started. These procedures are managed by a series of shell scripts that are run in sequence in init. The above entire process is called as Booting process. There are two ways where a UNIX / LINUX OS can boot, ‘Automatic mode’ & ‘Manual mode’ Without any external assistance if the system performs the whole boot procedure, then it is called ‘Automatic Mode’. In ‘manual mode’, at first, the system follows the automatic procedure up to a point before most initialization scripts have been run, And then turns control over to an operator. Up to this point, the computer will be running in ‘Single User mode’. Most of the system process will not be running, while other users cannot log in as well. It is involved in six different steps:

Bootstrapping in UNIX / LINUX Read More »

Enquire Now

Enquire Now

Enquire Now

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download





    [group student clear_on_hide]

    [/group]

    [group graduated clear_on_hide]

    [/group]

    [group wp clear_on_hide]

    [/group]

    [group student-course clear_on_hide]

    [/group]

    [group graduated-course clear_on_hide]

    [/group]

    [group work-course clear_on_hide]

    [/group]


    Enquiry Form