20120728

Simple Program for malloc in C

How to use malloc in C. Simple Program for malloc in C.

->malloc function allocates required amount of memory in heap (instead of stack) and
returns the pointer to the address of allocated block of memory.
->We need to type cast the returned pointer by malloc as it returns a void pointer.
->memory allocated by malloc in heap is deallocated by free, so that memory in heap can be
  allocated by other malloc functions.
->Syntax of malloc in C
datatype *p;
p=(datatype *)malloc(sazeof(datatype));
example of malloc:
int *p;
p=(int *)malloc(100*sizeof(int));

Explanation:
Here, malloc function allocates 400( 100*size of integer ie 100*4=400 ) bytes in heap and
assigns the address of the allocated memory in integer pointer p.
How to use malloc in C.
Simple Program for malloc in C.
#include<stdio.h>
int main
{
int *p;
p=(int *)malloc(sizeof(int));
*p=10;
printtf("%d",*p);
free(p);
return 0;
}
Previous                             Home                               Next

Is it necessary to accept an object by reference in copy constructor?

Is it necessary to accept a reference in copy constructor?
Yes, It is necessary to accept an object by reference in copy constructor because if an object is accepted by value then the copy constructor will fall in recursive loop.
C++ program for copy constructor:

#include<iostream>
using namespace std;
class copyDemo
{
int x;
public:
       copyDemo(copyDemo v) //taking object as value
        {
         x=v.x;
        }
};

int main()
{
copyDemo c;
copyDemo c1(c);// this will invoke copy constructor
return 0;
}

//explanation

The statement copyDemo c1(c);  causes the copy constructor to get called.
The value of c is passed If the copy constructor collects c in v by value then
the statement  copyDemo v would become as
copyDemo v=c;
here v is getting created and initialized so again copy constructor will get called and will go in recursive call loop.
Previous                             Home                               Next

Multiplication of two numbers using BITWISE operators

//How will you multiply two numbers using BITWISE operators
#include<stdio.h>
main()
{
   int a,b,result;
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
      if (b&01)               // Logical ANDing of the value of b with 01
      result=result+a; // Update the result with the new value of a.
      a<<=1;              // Left shifting the value contained in 'a' by 1.
      b>>=1;             // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}

//program to set and show bit at nth position

20120727

New Features in ANSI C++ Standard

How to Use New Features in ANSI C++ Standard.

1)      Newly added  data types in ANSI C++:
a)bool  data type
b)wchar_t data types
c) wide_character Literals
a)bool  data type:
bool  bb; //declarion of bool data type
bb=true;  //assignment of true value
bool  bb2 = false; //declaration and initialization(to true) simultaneously

b)wchar_t data types:
To represent the character sets of the languages that contains more than 255 characters.e.g. Japanese Language wchar_t data type is used that hold 16-bit wide characters. 
c) wide_character:
                                Wide character Literals begin  with the letter  L . It has 2 byte memory.
                                e.g.    L  ‘ms’ //example of wide character Literal.
         2)      New Operators:
A)     Static_cast operator
B)      Const_castoperator
C)      Reinterpret_cast operator
D)     Dynamic_cast operator
E)      Typeid operator

       3)      Namespace
       4)      Some old style header files are renamed.for example:
           Math.h file renamed as cmath in the new standard
Previous                             Home                               Next

20120718

Bitwise operation for division

// Bitwise operation for division

int main(void) {
int L, leftShift, i, changeMask;

leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;

for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}

printf("%i", i);

return 0;
}

Previous                             Home                               Next

20120706

Compiler Terminologies



  • Compiling:  converting source code  into an executable.
  • Linking: converting  the compiled code into an executable
  • Building: Creating the end executable. Tools exist to help to reduce the complexity of the build process--makefiles, for instance.
  • Compiler: refers to both a compiler and a "linker"
  • Linker The program that generates the executable by linking

  • Previous                             Home                               Next