Musings

A random collection

Archive for June 2010

C++: Bookmarks

Secure C++ Coding
CERT Secure C++ Coding Guidelines
OO Tips
A lot of interesting stuff on Patterns, C++ etc.
Google C++ StyleGuide
Good advice on what not to do
C++ FAQ
Collection of frequently asked questions
But Uncle Bob
Robert Martin (Object Mentor) has an archive of interesting OO Design related website
Object Mentor Blog
A blog maintained by Object Mentor folks
Miss Manners
Taligent’s Guide to Designing Programs
Advertisements

Written by curious

June 30, 2010 at 9:32 am

Posted in C++

TECH: SELinux

  1. Check status of SELinux on your machine
    getenforce
    
  2. Want to just disable SELinux on your machine
    setenforce Permissive
    setenforce 0
    
  3. Want to just re-enable SELinux on your machine
    setenforce Enforcing
    setenforce 1
    
  4. Change SE Linux “context type” of a file/directory so that HTTPd can access it
    chcon -t httpd_user_content_t FILENAME
    
  5. Enable access to user’s home directory for HTTPd
    setsebool -P httpd_enable_homedirs=1
    

Written by curious

June 30, 2010 at 9:15 am

Posted in linux

C++: Virtual Functions

  1. Should Virtual Functions be declared inline? [C/C++ User’s Journal, Sep 2000, Josee Lajoie, Stan Lippman]
    Issues:

    1. Virtual functions are resolved at run-time, but inlining is done at compile-time, so no benefit of making them inline.
    2. On the contrary, because inline functions are expanded, this can result in multiple copies of the function being defined in the executable. So there is a price to pay in terms of space without any benefit.

    Not quite true:

    1. Virtual functions do not have to always be resolved dynamically. In many cases a virtual function is resolved statically.
      class LibraryMaterial
      {
      public:
          inline virtual void print(ostream& = std::cout) = 0;
          virtual ~LibraryMaterial() { /* inline */ }
      };
      
      class Book : public LibraryMaterial
      {
      public:
          virtual void print(ostream& os);
          virtual ~Book() { /* implicitly calls LibraryMaterial::dtor, statically (INLINE!) */ }
      };
      
      class AudioBook : public Book
      {
      public:
          virtual void print(ostream& os);
          ~AudioBook() { /* inline call to Book, LibraryMaterial dtors */ }
      };
      
      inline void LibraryMaterial::print(ostream& os) { os << "Library material"; }
      
      inline void Book::print(ostream& os)
      {
          LibraryMaterial::print();   // resolved statically (INLINE!)
          os << "A Book";
      }
      
      inline void AudioBook::print(ostream& os)
      {
          Book::print();   // resolved statically (INLINE!)
          os << "Audio Book";
      }
      
    2. Space is also not an issue. For each virtual function call requiring dynamic resolution, there is just one copy of its definition (address of which is put in the virtual table).
      LibraryMaterial* p = new AudioBook(...);
      ....
      p->print();  // dynamic binding, utilizes single copy of function
      ...
      p->print();
      ...
      p->print();
      ....
      p->print();
      
  2. Can not call pure virtual functions from inside constructors and destructors. Object is not fully constructed yet and if you call a pure virtual function, that is to be called on a derived class object which is not even fully constructed yet. Similarly, if you call it in destructor, you might find that part of the object is already destroyed.

    You will get an exception if you manage to execute a call to pure virtual function in an abstract class’s constructor or destructor.

  3. Never call a virtual function (not just pure virtual, do not call any virtual) from inside constructor or destructor[Effective C++, 3ed]

Written by curious

June 29, 2010 at 2:56 pm

Posted in C++

TECH: JavaScript 101

Write a basic HTML section

<script type="text/javascript">
document.write("<h2>Hello World!</h2>");
</script>

Define a function and a call to it:

<script type="text/javascript">
function message()
{
  document.write("Hello world!");
}

message();
</script>

Calling a javascript file

// somescript.js
function message()
{
  document.write("Hello world!");
}

message();
// end of script

<script type="text/javascript" src="somescript.js">
</script>

Javascript has:

  1. Variables: var x=5; y = 10; var z;
  2. Arithmetic, Assignment Operators
  3. Comparison operators: ==, ===, <, >, ≤, ≥, !=
  4. if, if else statements
  5. switch case statements
  6. Popup boxes with alert(), Confirm boxes with confirm(), Prompt boxes with prompt()
  7. Functions: function funcname(var1, …, varN) { … code … }, Function Calls: funcname(1,2,”abcd”)
  8. Loops: for (var = startval; loopcondition; loopincrement) {}, while (loopcondition) {}, do {} while (loopcondition)
  9. Break and Continue
  10. Array iteration loops: for (var1 in arrayObj) {}
  11. Array Objects: var myfruits = new Array(); myfruits[0] = “Watermelon”;
  12. Events can trigger Javascript: Page (onLoad, onUnload), Form Fields (onFocus, onBlur, onChange), Form (onSubmit), URLs (onMouseOver, onMouseOut)
  13. Exception Handling: try … catch, throw
  14. Special Characters (use backslash): single quote, double quote, ampersand, backslash, new line, carriage return, tab, backspace, form feed
  15. String properties: var t1 = “Hello”; t1.length; t1.toUpperCase(); t1.toLowerCase(); t1.match(); t1.replace(); t1.indexOf(); t1.big(); t1.bold(); t1.strike(); t1.sub(); t1.fontcolor(“green”); t1.fontsize(10); t1.link(“some url”); t1.blink()
  16. Date properties: var d1 = new Date(); d1.getTime(); d1.setFullYear(); d1.toUTCString(); d1.getDay(); if (d1 > d2) …
  17. Show a running clock:
    <html>
    <head>
    <script type="text/javascript">
    function startTime()
    {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    // add a zero in front of numbers<10
    m=checkTime(m);
    s=checkTime(s);
    document.getElementById('txt').innerHTML=h+":"+m+":"+s;
    t=setTimeout('startTime()',500);
    }
    
    function checkTime(i)
    {
    if (i<10)
      {
      i="0" + i;
      }
    return i;
    }
    </script>
    </head>
    
    <body onload="startTime()">
    <div id="txt"></div>
    </body>
    </html>
    
  18. Boolean objects: var flag = new Boolean(0);
  19. Cookies: document.cookie = “new cookie”;
  20. Timer: setTimeout(“code to run”, delayinmsec);

Members/Properties

this // Window
this.variables  // access variables defined in your javascript scope

document.location // URL for the document

Written by curious

June 27, 2010 at 11:58 am

Posted in javascript

C++: STL IOStream

Open a file for Reading

std::ifstream inputfile;
inputfile.open(filename.c_str(), std::ios::in|std::ios::binary);
if (!inputfile.good())
{
  // error in opening file
}

while (!inputfile.eof())
{
  // read from the input stream
  inputfile >> some_str;
  inputfile >> some_int;
}
inputfile.close();

Open a file for Writing

std::ofstream outputfile;
outputfile.open(filename.c_str(), std::ios::binary);
if (!outputfile.good())
{
  // error in opening file
}

outputfile << some_str;
outputfile << some_int;

outputfile.close();

Reference books:
Standard C++ IOStreams and locales; Angelika Langer, Klaus Kreft

Written by curious

June 27, 2010 at 11:41 am

Posted in STL

Protected: TECH: QuickFAST

This content is password protected. To view it please enter your password below:

Written by curious

June 26, 2010 at 9:20 pm

Posted in exchanges

GEN: Solar System

Solar System Facts

  1. Solar System was formed 4.6 billion years ago
  2. Solar System is made up of everything in space that travels around our Sun
  3. There are 8 planets in the Solar System, all very different from each other
    1. Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
  4. There are other objects too – comets, meteoroids, asteroids
  5. Sun makes up 99 percent of everything in the Solar System. 1 million earths  could fit inside the sun
  6. It takes Sun’s light a little over 8 minutes to reach Earth
  7. Temperature at Sun’s core is about 27 million F. Temperature at it’s surface is about 10,000 F.
  8. Gravity is an invisible force that pulls one object toward another object. Larger an object the stronger its pull
  9. Sun’s gravity keeps all the planets and other objects in Solar System together
  10. Earth’s gravity makes you fall when you jump up. Earth’s gravity also keeps moon in orbit around it
  11. Each planet travels around the Sun at a different speed
    1. Length of Orbit around Sun — Mercury – 88 days, Venus – 225 days, Earth – 365 days, Mars – 687 days, Jupiter – 12 years, Saturn – 29 years, Uranus – 84 years, Neptune – 165 years (all measured in earth days)
  12. Each planet also spins like a top. Spinning like a top causes Day and Nights
  13. Uranus is spinning
  14. Terrestrial planets – the 4 inner planets (Mercury, Venus, Earth, Mars). They are solid and made of rocks. They also have a layer of gas (atmosphere) around them
  15. Mercury: 36 million miles/58 million kms from the Sun. Temperature range: 870F to -300F. Smallest planet
  16. Venus: Hottest planet ~ 880F, atmosphere full of thick clouds which hold heat, reflect sunlight and make Venus shine. Has huge volcanoes
  17. Earth: Neither too hot nor too cold – Goldilock. Has lots of liquid water 71% of surface covered with water. Travels at around 18.5 miles per second/30 km per second around the Sun. It is about 93 million miles/150 million kms from the Sun
  18. Mars: Closest planet to Earth. Half the size of Earth. Looks like a dry desert but might have had rivers, lakes or oceans at one time. Looks red because it has a lot of iron oxide (rust) in its soil.
  19. Jovian planets – Jupiter and the rest of the outer planets. These have a core of rock and ice but outer layers are made of gas.
  20. Jupiter: Largest planet in the Solar System – can fit a thousand Earths inside it. Has a “Great Red Spot” which is bigger than Earth. It is a storm going on it for hundreds of years.
  21. Saturn: Second largest planet. Has biggest rings all made of rock and ice, 175,000 miles wide but only about 1 km thick.
  22. Uranus: Mostly made of thick clouds and gas – no surface. It spins on its side. Perhaps something knocked it hard around the time Solar System was beginning and it tilted on its side
  23. Neptune: The farthest planet (2.8 billion miles/4.5 billion kms) from the Sun, it is the coldest planet
  24. More stuff in the Solar System:
    1. Pluto. No more a planet because other objects discovered bigger than it, its been demoted to a “dwarf planets” category
    2. Meteoroids – bits of rocks and metal passing through the solar system. Sometimes they are pulled by gravity. A meteoroid falling toward Earth starts burning up in the atmosphere. The streak of light it creates is called a “Meteor“/Shooting Star – it is a meteoroid passing through Earth’s atmosphere. A meteoroid that manages to reach the surface of Earth is a “Meteorite
    3. Asteroids – rock bigger than 1 km wide. Most of them in the asteroid belt – space between Mars and Jupiter
    4. Comets – chunks of rock, dust and ice going around in space. Some comets go in an eccentric orbit around the Sun. When a comet is near the Sun’ due to heat it’s ice becomes foggy and Sun’s wind turns the fog into a long tail
    5. Kuiper Belt – Beyond Pluto the solar system is filled with billions of chunks of frozen gas and water that is also orbiting around the Sun. These objects are however much smaller
  25. Telescopes
    1. Galileo – 1610
    2. Newton – 1668 – reflecting telescope
    3. Hubble Space Telescope – 1990 – optical telescope in the sky
    4. Arecibo Observatory – Puerto Rico – world’s largest telescope – 305m wide dish
    5. James Webb Space Telescope – 2013 – senses infrared waves
  26. Spaceships
    1. New Horizons – on one way journey through Solar System – started from Earth in 2006, will reach Pluto in 2015. Takes 6 hours for signals to reach from near  Pluto to Earth
    2. Cassini spacecraft – measured water vapor on Saturn’s moons
    3. Mars Global Surveyor – mapping Mars surface since 1999

Written by curious

June 26, 2010 at 11:51 am

Posted in general