Monday, June 28, 2010

Find Zeros and One

1: #include <stdio.h>
2: 
3: main()
4: {
5:   int val = 1;
6:   int data = 10;
7:   int OneCount = 0;
8:   int ZeroCount = 0;
9:   while(val)
10:   {
11:     if(data & val)
12:       OneCount++;
13:     else
14:       ZeroCount++;
15:       
16:     val = val << 1;  
17:   }
18:   printf("Number of ones %d \n",OneCount);
19:   printf("Number of zeroes %d \n",ZeroCount);
20:   getchar();
21: }

malloc(0), why is it working ?

1: #include <stdio.h>
2: #include <malloc.h>
3: 
4: char* reverse(char *data);
5: void my_Strcpy(char* dest,char* source);
6: main()
7: {
8:   char* p_Name = "Mithun P";
9:   char a_Name[] = "Mithun P";
10:   char *pd_Name = malloc(0);
11:   my_Strcpy(pd_Name,"Mithun P");
12:   
13: //printf("reverse of p_Name is %s \n",reverse(p_Name));
14:   printf("reverse of a_Name is %s \n",reverse(a_Name));
15:   printf("reverse of pd_Name is %s \n",reverse(pd_Name));
16:   
17:   getchar();
18: }
19: 

Sunday, June 27, 2010

char[] and char*

  1. char data[] = “data”;
    1. data: [D] [A] [T] [A] [\0]
  2. so , char data[] is a array holding the data\0
  3. the individual characters can be modified
  4. but data will always refer to the same storage

 

  1. char *data = “data”;
    1. [data] –> [D] [A] [T] [A] [\0]
  2. so, char *data is a pointing to a string constant
  3. the pointer can be modified to point to other location
  4. modifying the string constant gives undefined result (SIGSEGV in gcc)

Deep Copy

  1. needed when the class has dynamically allocated members
  2. always remember the explicit this pointer of c++
  3. copy constructor is implemented (algorithm)
    1. allocate memory for dynamically allocated members
    2. copy the data from const_argument.member to this.member
  4. assignment operator is implemented (algorithm)
    1. check for self assignment
    2. delete memory for this.member
    3. allocate new memory for this.member
    4. copy the data from const_argument.member to this.member
    5. return this
  5. when copy constructor is called
    1. doing initialization with objects, like
      1. class1 object1(“value”);
      2. class1 object2 = object1; //object to object assignment
    2. passing a object by value
    3. returning a object by value
  6. when assignment operator is called
    1. assigning a existing object(object with memory for its data members) a new object

Friday, June 25, 2010

Composition, a type of aggregation

  1. composition,  ( composite aggregation in UML), is part – whole relationship
  2. Winston et al describes describes several kind composition, with three basic properties 
    1. configuration  (functional or structural relationship between part and whole)
    2. homeomerous (parts are the same kind of as the whole or not)
    3. invariance (parts can be separated from the whole or not)
  3. based in these three properties the following kind of composition can be ascertained
    1. component – integral object composition (the integral whole is formed by the individual components, which can be removed but removing them will disintegrate the whole, like a laptop is made of different hardware components)
    2. material – object composition (the object is made is up the material, and the material cant be removed from the object, or it can be said that the object is made up of the material, like a cooking pan is made of steel)
    3. portion – object composition (portions are homeomeric with the object, like a link list is made up of individual nodes, in which each node can be considered as portion of the link list, each portion can be removed from the object)
    4. place – area composition (place within the area, and place cant be removed from the area, like pune is in India)
    5. member – bunch composition (members are just a collection which forms a bunch, like flowers in a bouquet)
    6. member – partnership composition (members are invariant part of the partnership, if parts are removed whole is destroyed)

Reference :

Thursday, June 24, 2010

UML Class diagram notation

  1. name
  2. attributes (data)
  3. behavior (messages and operations)
  4. constraints
  5. tagged values
  6. stereotypes

Giving name to a architecture

  1. before deciding on the style we need to find
    1. the components of the system
    2. the interaction between these components (what connects them)
    3. the constraints regarding the way components can combine and interact
    4. the underlying computational model
    5. essential invariants
    6. where it can be used, some examples
    7. advantages and disadvantages
    8. common specialization

Reference : cs.cmu.edu