Pages

Friday, September 30, 2011

trees and data structure : skipping double pointer

many times it is felt to pass head pointers as parameter to functions...

v  simple solution is to pass by reference..
this way u dnt have to change internal syntaxes in side your insert functions and all other functions where modification in the data structure is required.


void insert ( struct T * & node, int data)
{
if (!node)
{ node = (struct T * )malloc(sizeof(struct T));
node->data = data;
node->left = node->right = NULL;
}
}

Monday, September 19, 2011

Sample Code : Constant functions and temporary objects | Return Optimization

1. Constant funtions with temporary objects :
it tells that temporary objects are by default constant in nature, that their values cannot be changed. and so only constant functions can be called using temporary objects.


2. bruce eckel says about return value optimization : that returning by values just by making an explicit constructor call.

This doesnt create a local object (if i were use Integer a; return a;) and so
no call of copy constructor at the time of return object

its a direct creation of temp object in the return address and compiler knows that u r creating this object only for return purpose and nothing else.



Ref : Sections : 
1. Return by value as constants, Pg 533
2. The return optimization, pg 534
Chp 12 Operator Overloading, : Thinking in C++, second edition, volume 1


#include
using namespace std;
#define about \
"this program demonstrates \
\n 1. that only constant functions should be and can be called from temporary objects \
\n 2. return value optimization : while returning object by value using operator+, \
\n this way doesnt call any form of constructor"




class Integer
{
int i;
public:
Integer(int i):i(i){}
~Integer(){
cout<<"\ndestructor called:"<
}


const Integer operator+(const Integer & right)
{
return Integer(i + right.i);
}
void g()
{
cout<<"gOnce called g-one"<
}
int getValue()const
{ return i;
}

};


int main()
{
Integer a(2), b(4);
// ! (a+b).g(); //this line is a compile time error because g() in a non const function
cout <
cout<<(a+b).getValue()<
return 0;
}

Friday, September 16, 2011

Confused Constants : C and C++


Following code is erroneous in C++ and is works fine in C (with warning)

int *p;
const int i =3;
p = &i;


trying to point out a non constant pointer to a constant variable. 


Thursday, September 15, 2011

confused constants

my understanding says constant checking is done in symbol table level .. as
const int i =90;
int *p= & i;
*p = 0;
  is allowed, had it been on memory level, it would have said no.





Question : Search in progress: what is the use of constant return type in fuctions
const int foo()
{
return 3;
}
Answer : page 366, Chapter 8, Thinking in C++, second edition, volume 1



Wednesday, September 14, 2011

dynamic memory allocation without using heap

Mind Mapping :
Operator Overloading, Thinking in C++ Vol 1( Bruce Eckel) ->  doubts in Constant functions ->
browser -> times of india -> early enterpeuner ->
www.sourcebits.com (MBBS, MD tunred into a techie, started as one man company and now a leading mobile app firms with one of its app went to top 3 in 3 days), Impressed by the graphic design, I gave a pause, world is progressing in a accelerated way and i m shitting here on Operator Overloading.

Before turning page in Ops Overloading, I first googled to see if c++ still in the market compared the new up coming technologies. There I found c++ recently updated it self this month Sept, 2011. first look link Under division constant expression, I figured out introduction of new keyword constexpr.


Wiki started as
int get_five() {return 5;}
 
int some_value[get_five() + 7]; //create an array of 12 integers. Illegal C++


and c++ version 11 has a solution


constexpr int get_five() {return 5;}


int some_value[get_five() + 7];


As per my understanding after spending 15 minutes to this section I realized similar things can be done using const keyword. Which in turn allowed me to do dynamic memory allocation without using malloc.


const int getSize(int i){return i;}


int main(int argc, char ** argv)
{
int a[ 1 + getSize( atoi(argv[1]) ) ];
cout<
return 0;
}


I was amazed.