Skip to main content

Posts

Array parsing in C++

Array parsing in  C++ is always done by reference, and in std there's no concept of parsing by value. From a stakoverflow question I summarize this below. The array size information is lost here. void by_pointer(int *p, int size); void by_pointer(int p[], int size); void by_pointer(int p[7], int size);   // the 7 is ignored in this context! void by_reference(int (&a)[7]); // only arrays of size 7 can be passed here! template void by_reference(int (&a)[size]); void TestMethod(char cArray[], int size) {          const char* pTest = "test";     strncpy(cArray, pTest, size);     cArray[ size-1] = '\0';     }   Usage: char cMyArray[7] =  {a}; TestMethod(cMyAray, 7}  However this can be quite tricky to understand. Take the problem of dynamic resizing of an array. unsigned int iArrSize = 10; int *iArr = new int[iArrSize]; for (int i = 0; i < 100; i++) { ...

C++ tips - I

Static members in .h and .cpp A static variable in C++ when defined in a global function belongs to the file scope. If a C++ static variable is defined as a member variable, regardless of how many objects we instantiate we only have one static variable across all of the instances. The static variable can be accessed via the instance or the class variable. If a static variable is defined as member in a class declared in a .h file, the initialization should be done as [variable_type] ClassName::[variable_name] = [value] A static function can only access static variables - makes sense huh?? A static function doesn't have the this pointer class_name::static_function() - usage Friend keyword

The Mediator Pattern

Mediator pattern is used to loosely couple object interaction. If objects are tightly coupled, resuability disappears and maintenance becomes harder. A real life scenario will explain the pattern the best. Think about an airport control tower. It's task is to moderate the flight landings. If many aircrafts arrive simultaneously, only one can land at a time and the rest have to stay out from landing until signaled by the tower. This behavior exactly matches that of the mediator pattern. The aircrafts don't communicate with each other but yet they communicate through the control tower so that they can avoid disaster. The C++ code simulating the landing of planes, helicopters and private jets at an airport is simulated in this code. Assume only one airstrip. The output: Mediator Will Take Over Landing Plane Landing 1 Heli Out of the way... 2 Jet Out of the way... 3 Jet Out of the way... 4 Mediator Will Take Over Landing Heli Out of the way... 2 Jet Landing 3 Jet Ou...

Using scp for file transfer

From remote host to local host $ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .  From local host to remote host $ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar  These commands were wimple extracted from the elaborate guide http://www.hypexr.org/linux_scp_help.php

Creating simply correlated data in R

Although it might appear as a trivial task to a R user with 1 or 2 weeks of R experience, I thought that this might help to a total noob like me. It's fascinating how you can avoid using loops in R to get the jobs done in a few lines of code (or commands as one might argue...) So you end up with a simple 2-D data set with some correlation, nothing fancy at all. Anyway, I needed this to play around with Principal Component Analysis (PCA), more on that later. Rendering...