Table of Contents

1 Reference in C and C++

int num = 3;
int* ptr = &num
printf("%d", *ptr); // 3

2 Function return by reference

  • Return a reference if a variable is not local to the function.
int num = 3;
int& test(){
  return num;
}       
test() = 5;
printf("%d", num); // num => 5

3 Get a pointer to std::vector

class Pt{
  int x;
  int y;
  public Pt(int x, int y){
     this.x = x;
     this.y = y;
  }
}
std::vector<Pt> vect;
vect.push_back(new Pt(1, 2));
int* ptr = &vect;
// ptr will point to vect.

4 Get a pointer to std::vector data

  • Get a pointer to the first address of the data. Test Example
class Pt{
public:
    int x;
    int y;
public:
    Pt(){}
    Pt(int x_, int y_){
        x = x_;
        y = y_;
    }
};
std::vector<Pt> vect;
Pt p1;
p1.x = 1;
p1.y = 2;
Pt p2;
p2.x = 3;
p2.y = 4;

vect.push_back(p1);
vect.push_back(p2);
int expected[] = {
    1, 2, 3, 4
};
int* ptrToData = (int*) vect.data();
REQUIRE(compareArray(ptrToData, expected, 4) == true);

5 Convert String to digit.

#include 
#include 

// convert string to digits
//
// gcc -o try1 try1.c   on MacOSX
// Fri Jul 13 00:14:39 PDT 2018
//  
// 
int main () {
  char str[30] = "2030300 This 444 is test";
  char *ptr;
  long ret;

  ret = strtol(str, &ptr, 10);
  printf("The number(unsigned long integer) is %ld\n", ret);
  printf("String part is [%s]", ptr);

  return(0);
}

6 Initialize Array in C and C++

//C and C++ initialize array
int array[] = {1, 2, 3};    

7 Pointer points to array

// now pointer pt points to array
int* pt = array; 

8 C++ print out array

// print out the array
for(int i=0; i<3; i++){
    cout<<"["<<i<<"]="<<pt[i]<<endl;
}

9 Print out array with pointer.

// print out using the pointer
for(int i=0; i<3; i++){
    cout<<"["<<i<<"]="<<*(pt+i)<<endl;
}

10 Allocate memory in C dynamically.

// dynamically allocate memory in C
int size = 3;
int* pt1 = (int*)malloc(sizeof(int)*size);

// dellocate memory in C
free(pt1);

11 Allocate memory in C++/Cpp dynamically.

// dynamically allocate memory in C++
int *pt2 = new int[3];

// dellocate memory in C++
delete pt2;

12 Allocate two dimensions array in C

  // allocate two dimension array in C
  int col = 4;
  int row = 3;
  int** ppt;

  // this is trick part
  ppt = (int**)malloc(sizeof(int*)*col);
  for(int j=0; j<col; j++){
      ppt[j] = (int*)malloc(sizeof(int)*row);
  }
  int c = 0;
  for(int i=0; i < col; i++){
      for(int j=0; j < row; j++){
          ppt[i][j] = c++;
      }
  }

// Deallocate memory 2 dimensions.
for(int j=0; j < col; j++){
    free(ppt[j]);
}

Sorry, your browser does not support SVG.

13 Inheritence

  • Polygon, Rectangle and Triangle examples.
     class Polygon{
 protected:
     int m;
     int n;
 public:
     Polygon(){
         m = 0;
         n = 0;
     }
     Polygon(int m, int n){
         this -> m = m;
         this -> n = n;
     }
 };

 class Rectangle : public Polygon{
 public:
     Rectangle(){}
     Rectangle(int m, int n){
         Polygon(m, n);
     }
 public:
     double area(){
         return m*n;
     }
 };

 class Triangle : public Polygon{
 public:
     Triangle(){}
     Triangle(int m, int n){
         Polygon(m, n);
     }
 public:
     double area(){
         return m*n/2;
     }
 };


Rectangle* pRect = new Rectangle(1, 2);
cout<<"Rectangle Area"<<pRect->area();
Triangle* pTri = new Triangle(1, 2);
cout<<"Triangle Area"<<pTri->area();

14 Virtual Function, Pure Virtual.

15 Smart pointer: unique pointer, shared pointer and weak pointer

  • What is smart pointer? Smart Pointer
    • Smart pointer is all about ownership, who owns the object
      • Unique pointer only allows ONE ownership
      • Shared pointer allow more than one ownership.
      • Weak pointer has no ownership.
    • Smart pointer can automatically deallocate memory.
  • What is unique pointer?
    • As the name implies, make sure exactly only one copy of object exists.
    • Unique pointer can not be copied. \( \Rightarrow \) no use_count() function.
    • You can transfer the pointer to other unique pointer using std::move(), then the origin unique pointer will be deleted.
  • What is shared pointer? sharedptr
    • Shared pointer is based on reference counting and can be used to store and pass reference beyond the current scope.

      class MyClass{
      public:
           MyClass(){}
      public:
           shared_pt<int> fun(){
               shared_pt<int> pt(new int(9));
               cout<<"fun pt.use_count()=" <<pt.use_count()<<endl; // pt.use_count() == 1
               return pt;
           }
      }
      
      // main
      MyClass* cpt = new MyClass();
      shared_pt<int> pt1 = cpt -> fun();
      cout<<"main pt1.use_count()=" << pt1.use_count()<<endl;  // pt1.use_count() == 1
      
      
  • What is weak pointer?

Author: cat

Created: 2019-07-27 Sat 14:58

Validate