Archive

Archive for the ‘C/C++’ Category

Stack and Heap

September 16, 2012 Leave a comment

When we define some class like :

class Vector3 {
public:
 float x,y,z;
 Vector3(){ x=0.0f ; y=0.0f; z=0.0f};
}

and then create some code to use it :

1. use stack allocation

Vector3 myVect;
myVect.x = 1.0f;
myVect.y = 2.0f;
myVect.z = 3.0f;

or

2. use heap allocation

Vector3 *myVect = new  Vector3;
myVect->x = 1.0f;
myVect->y = 2.0f;
myVect->z = 3.0f;

which one is better ?

we have to consider several things anyway.

  1. Using stack is faster but very very finite size
  2. Heap is slower because needed to search for available free memory
  3. If using heap,  then have to do clean up manually ( which lead to memory leak problem)
  4. Consider about variable life time
Advertisements
Categories: C/C++, Programming

Using Iterator

September 7, 2012 Leave a comment

Consider if we have Different Object type but have same parent class. for example Triangle and Square are in herite Shape. you create object from those class and want to draw them on the screen.


class Shape {
protected:
Color color;
public:
virtual void draw();
}

class Triangle {
private:
Vector3 top;
Vector3 bottom_left;
Vector3 bottom_right;
public:
virtual void draw(
// draw Triangle
);
}

class Rectangle {
private:
Vector3 top_left;
Vector3 top_right;
Vector3 bottom_left;
Vector3 bottom_right;
public:
virtual void draw(
// draw Rectangle
);
}

 

we can store them into list


std::list<Shape*> my_list
std::list<Shape*>::iterator it

Shape *my_triangle = new Triangle();
Shape *my_rectangle = new Rectangle();

my_list.push_back(my_triangle);
my_list.push_back(my_rectangle);

 

Then perform iteration to draw

 

for(it=my_list.begin() ; it!= my_list.end() ; it++)
{
(*it)->draw();
}

 

Categories: C/C++

Pointer

September 3, 2012 Leave a comment

Spending weekend with Pointer.

1. Increment

#include <stdio.h> 
#include  
using namespace std; 

int main() 
{ 
 int *ptr = NULL ; 
 int i; 

 int my_array[] = { 1,2,3,4}; 
 ptr = my_array; 

 for(int i=0; i < 4; i++) 
 { 
   cout << "Address : " <<  ( ptr + i)  << " Value : " <<  *(ptr + i)  << "\n" ; 
 }  
 return 0; 
}

2. Copy String

#include <stdio.h>
char *my_strcpy(char *dst, char const *src)
{
  char *p;
  p = dst;

  while( *src != ''  )
  {
    *p++ = *src++ ;
  }

 *p = '';
 return dst;

}

int main()
{
  char strDst[80];
  char strSrc[80] = "this is string";

  my_strcpy(strDst, strSrc);
  puts(strDst);
}

3. Struct

#include <stdio.h>

struct tag {
  char first_name[30];
  char last_name[30];
  int age ;
};

char *my_strcpy(char *dst, char const *src)
{
  char *p;
  p = dst;

  while( *src != ''  )
  {
    *p++ = *src++ ;
  }

 *p = '';
 return dst;

}

void print_employee(struct tag *a)
{

  printf("-----------------------------------\n");
  printf("First Name: %s \n",  a->first_name);
  printf("Last Name: %s \n",   a->last_name);
  printf("Age: %d \n",   a->age); 
  printf("-----------------------------------\n");

}

struct tag employee;

int main()
{

  struct tag *ptr_employee;
  ptr_employee = &employee;

  my_strcpy(employee.first_name, "Tonny");
  my_strcpy(employee.last_name, "Kusdarwanto");
  ptr_employee->age = 27;   

  print_employee(ptr_employee) ; 

}

2. Dynamic Array 1D

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

int *ptr;

int main()
{
 ptr = (int *)malloc(SIZE*sizeof(int)) ;
 if(ptr == NULL)
 {
   printf("Error! \n");
   return (-1);
 }

 for(int i=0; i< SIZE ; i++) 
 {
   ptr[i] = 2;
 }

 for(int i=0; i< SIZE ; i++) 
 {
   printf("%d \n", *(ptr + i) );
 }

 return 0;
}

2. Dynamic Array 2D

#include <stdio.h>
#include <stdlib.h>

#define COLS 10

typedef int RowArray[COLS];

RowArray *ptr_array;

int main(void)
{
 int nRow = 10;
 ptr_array =  (RowArray *)  malloc( nRow * COLS * sizeof(int) );

 if (ptr_array == NULL)
 {
  printf("Error");
  return (-1);
 }

 for(int i=0;i<nRow;i++)
 {
  for(int j=0;j<COLS;j++)
  {
   ptr_array[i][j] = i*j ;
  }
 }

 for(int i=0;i<nRow;i++)
 {
  for(int j=0;j<COLS;j++)
  {
   printf("%2d ", ptr_array[i][j]) ;
  }
  printf("\n");
 }
 return 0;
}

 

Reference:

Categories: C/C++, Programming