Sunday, April 28, 2013

2D/3D vector constructor C++

http://stackoverflow.com/questions/7686745/3d-vector-constructor-c
vec = vector<vector<vector<T>>> (boardSize, vector<vector<T>>(boardSize, vector<T>(boardSize)));

2D:
vector<vector<T>> vec;
vec = vector<vector<T>> (boardSize, vector<T>(boardSize));

Passing Objects by Reference and By Pointer

http://people.cs.vt.edu/~kafura/cs2704/reference.html


??


   window.Display(box1);  // uses by reference
   window.Display(box2);  // uses by pointer
   window.Display(&box3);  // uses by pointer
   window.Display(*box4);  // uses by referen


http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c

Pointer to C++ STL vector

http://stackoverflow.com/questions/6946217/pointer-to-a-vector

Fortran data type

http://en.wikibooks.org/wiki/Fortran/complex_types


If you want double-precision complex numbers, you're pretty much stuck with specifying a precision, and hoping that the compiler likes that format. Here's where portability comes in: If, for instance, the machine has floating point types of lengths 4, 8, and 16, and I specify a complex length of 8, 16, or 32, I'm likely to get a pair of floating point numbers at the size I expect. If, however, I specify a length of some other value, for instance, 10, then I'm likely to get a pair of 4s or 8s, depending on what the compiler likes to do. So, in general, to get the values as double-precision, I'd code:
 COMPLEX*16 myVariable, anotherVariable, anArray(2,3)

Saturday, April 27, 2013

Two dimensional iteration over 1D vector C++

vector<T> vec1d

typename vector<T>::iterator row;
typename vector<T>::iterator col;

for (row = vec1d.begin(); row!=vec1d.end(); row++){
  for (col = vec1d.begin(); col!=vec1d.end(); col++){
              // do stuff
  }
}

iterating through 2d "vector" c++

http://stackoverflow.com/questions/1784573/iterator-for-2d-vector


Although your question is not very clear, I'm going to assume you mean a 2D vector to mean a vector of vectors:
vector< vector<int> > vvi;
Then you need to use two iterators to traverse it, the first the iterator of the "rows", the second the iterators of the "columns" in that "row":
//assuming you have a "2D" vector vvi (vector of vector of int's)
vector< vector<int> >::iterator row;
vector<int>::iterator col;
for (row = vvi.begin(); row != vvi.end(); row++) {
    for (col = row->begin(); col != row->end(); col++) {
        // do stuff ...
    }
}

Multi Dimensional Array

The more STL savvy coder will opt for a vector-of-a-vector approach to MD arrays, rather than Nested New:

1
2
3
4
// to make a [5][6] array:
vector< vector<int> > stillevil( 5, vector<int>(6) );

stillevil[2][3] = 1;


Not a lot to say about this one. Like Nested New it's incompatible with other MD array flavors. If you need to pass this array to a function, none of the above functions will work.

Also, like Nested New, it's inefficent in that is requires additional memory and has double indirection.

On the plus side, you don't need cleanup code, and the allocation code is not as ugly (but still pretty ugly!)


Wednesday, April 24, 2013

C static variable and global variable

A static variable provides a function with "memory" without introducing a global variable that might be accidentally accessed and corrupted by other functions.


In these languages, the term "static variable" has two meanings which are easy to confuse:
  1. A variable with the same lifetime as the program, as described above (language-independent); or
  2. (C-family-specific) A variable declared with storage class static.

As well as specifying static lifetime, declaring a variable as static can have other effects depending on where the declaration occurs:
  • Static global variable: a variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage").
  • Static local variables: variables declared as static inside a function are statically allocated, thus keep their memory cell throughout all program execution, while having the same scope of visibility as automatic local variables, meaning remain local to the function. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
  • static member variables: in C++, member variables declared as static inside class definitions are class variables (shared between all class instances, as opposed to instance variables).

Saturday, April 20, 2013

Circular buffer and Sliding Sindow Step Detection Implementation

Circular Buffer/Cyclic Buffer/ring buffer

Single fixed size buffer, end to end:

Easily to buffering data streams

FIFO, implementation strategy for a queue


http://en.wikipedia.org/wiki/Circular_buffer

Difficulties

Full/Empty Distinction


Library
http://websvn.hylands.org/filedetails.php?repname=Projects&path=%2Fcommon%2FCBUF.h&rev=0&sc=0%3E

c++ terminate called without an active exception producer/consumer


std::thread - “terminate called without an active exception”, don't want to 'join' it


30.3.1.3 thread destructor [thread.thread.destr]
~thread();
If joinable() then terminate(), otherwise no effects. [ Note: Either implicitly detaching or joining ajoinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. — end note ]
What this means is that you should not let threads go out of scope without first calling either join() ordetatch().



Concept of producer/consumer
http://stackoverflow.com/questions/7381757/c-terminate-called-without-an-active-exception-producer-consumer

Thursday, April 18, 2013

Display Number of Processors on Linux

http://www.howtogeek.com/howto/ubuntu/display-number-of-processors-on-linux/


cat /proc/cpuinfo | grep processor | wc -l
The command just looks in the /proc/cpuinfo file, pulls out the number of lines containing the word “processor” and passes them into wc (word count), which returns a count of the CPUs in the system.
Here’s what it returned on my remote server:
[root@root]# cat /proc/cpuinfo | grep processor | wc -l
4

Saturday, April 13, 2013

Unused parameter

You can cast function parameters to void to avoid "unused parameter" compile warnings.
iterator find(const Data& item) const {
 (void) item;
 return iterator(nullptr);
}

Assuming your question was asked given that everything already does compile, I would say code in an order that makes sense to you. Hint: think about incremental testability. The more pieces of functionality you can test along the way, the less time you will spend debugging.

nullptr Vs. NULL

1. We encourage you to use nullptr as we have C++11, the latest version;

2. Whenever a method receives a pointer as a parameter, it can check whether it is equal to nullptr and do the appropriate thing. This is part of good defensive programming. Don't assume other parts of your program are acting as they should. 

2) Are we going to assume that null data will never be inserted in the insert method in BST.hpp?

3. Note that we will not test for this corner case, as we didn't specify that you needed to handle the case in the writeup. 

(This is a general rule, we won't hold you responsible for details left unspecified)
However, testing this corner case could be helpful during code development, much as cassert can be useful

The reason you should use nullptr instead of NULL: http://www.devx.com/cplus/10MinuteSolution/35167

nullptr: A Type-safe and Clear-Cut Null Pointer

Whether you're using NULL or 0, your C++ apps are subjected to bugs and maintenance problems that are caused by the ambiguity of 0. Find out how to use the nullptr keyword instead of 0 to indicate a null pointer value.



Tuesday, April 9, 2013

g++ "nullptr" was not declared in this scope

http://stackoverflow.com/questions/11616539/g-nullptr-was-not-declared-in-this-scope

unrecognized command line option “-std=c++11” with g++

http://stackoverflow.com/questions/14674597/cc1plus-error-unrecognized-command-line-option-std-c11-with-g
In versions pre-G++ 4.7, you'll have to use -std=c++0x, for more recent versions you can use -std=c++11.

http://stackoverflow.com/questions/11616539/g-nullptr-was-not-declared-in-this-scope

Undefined reference to operator new

Possible solution under Linux

You probably need to link with the C++ support runtime library. This happens automatically when you invoke g++. On linux, this is achieved by adding -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.

‘cout’ was not declared in this scope


 ‘cout’ was not declared in this scope
if you forget to include iostream or to use the std namespace.
WRONG
 #include <iostream>  // no using namespace std;
 int main(void) 
 {
   cout << "A";
   return 0;
 }

ALSO WRONG
 using namespace std;   // no #include iostream
 int main(void) 
 {
   cout << "A";
   return 0;
 }
RIGHT
 #include <iostream>   // one of two
 using namespace std;    // two of two, yay!
 int main(void) 
 {
   cout << "A";
   return 0;
 }

Monday, April 1, 2013

Learn VIM Progressively

http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/

Lost bytes and AVR interrupt

http://robotics.stackexchange.com/questions/99/how-to-manage-interrupts-on-an-avr


Atmega bootloader programmer

http://www.gammon.com.au/forum/?id=11635


Daily reading; watchdog Timer and AVR/Arduino

Typically, single-stage watchdog timers are used to simply restart the computer, whereas multistage watchdog timers will sequentially trigger a series of corrective actions, with the final stage triggering a computer restart.

In embedded systems and control systems, watchdog timers are often used to activate fail-safe circuitry. When activated, the fail-safe circuitry forces all control outputs to safe states (e.g., turns off motors, heaters, and high-voltages) to prevent injuries and equipment damage while the fault persists. In a two-stage watchdog, the first timer is often used to activate fail-safe outputs and start the second timer stage; the second stage will reset the computer if the fault cannot be corrected before the timer elapses.

Watchdog timers are sometimes used to trigger the recording of system state information—which may be useful during fault recovery[3]—or debug information (which may be useful for determining the cause of the fault) onto a persistentmedium. In such cases, a second timer—which is started when the first timer elapses—is typically used to reset the computer later, after allowing sufficient time for data recording to complete. This allows time for the information to be saved, but ensures that the computer will be reset even if the recording process fails.


http://arduino.cc/forum/index.php?topic=128717.0

External Hardware WatchDog:
http://www.practicalarduino.com/news/id/471





#include <avr/wdt.h>

void setup ()
{
  Serial.begin (115200);
  Serial.println ("Restarted.");
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
 }  // end of setup

void loop ()
{
  Serial.println ("Entered loop ...");
  wdt_reset ();  // give me another second to do stuff (pat the dog)
  while (true) ;   // oops, went into a loop
}  // end of loop

The more severe issue is, that the bootloader does not de-activate the watchdog upon reset, so that one can end up with endless resets.
the official ATmega328 bootloader (optiboot) deactivates the watchdog on reset.
The official MEGA bootloader does not.
I'm not sure about Leonardo or Due.

AVR Watch Dog Macro

http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html

stackexchange Arduino Watch Dog Example
http://electronics.stackexchange.com/questions/18/watch-dog-timer-arduino



Watch dog AVR Library
Bootloader Selection
http://arduino.cc/forum/index.php?PHPSESSID=c322bf19e2be8fb4daedb3e931a0c8f0&topic=128717.0



A arguable solution? 
http://www.jasoncavett.com/2011/03/accessing-watchdog-timer-on-arduino-uno.html

Blocking service serial.print in WDT_ISR?