Tuesday, July 30, 2013

Null pointer in C/C++

http://stackoverflow.com/questions/7016861/null-pointer-in-c-and-c

C++ deleting a pointer

The right way:
myPointer = new int;
delete myPointer; //freed memory
myPointer = NULL; //pointed dangling ptr to NULL

http://stackoverflow.com/questions/13223399/c-deleting-a-pointer

Pointer to Pointer and Reference to Pointer

http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer

Understanding lvalues and rvalues in C and C++

lvalue: locator value represents an object that occupies some identifiable location in memory

i.e. has an address

an rvalue is an expression that does not represent an object occupying some identifiable location in memory.

http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/


Conversions between lvalues and rvalues

All lvalues that aren’t arrays, functions or of incomplete types can be converted thus to rvalues.


Difference between const & const volatile


An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) - at least through that particular name/pointer.
The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object.
In an embedded system, this is typically used to access hardware registers that can be read and are updated by the hardware, but make no sense to write to (or might be an error to write to).

unsigned int const volatile *status_reg; // assume these are assigned to point to the 
unsigned char const volatile *recv_reg;  //   correct hardware addresses


#define UART_CHAR_READY 0x00000001

int get_next_char()
{
    while ((*status_reg & UART_CHAR_READY) == 0) {
        // do nothing but spin
    }

    return *recv_reg;
}
If these pointers were not marked as being volatile, a couple problems might occur:
  • the while loop test might read the status register only once, since the compiler could assume that whatever it pointed to would never change (there's nothing in the while loop test or loop itself that could change it). If you entered the function when there was no character waiting in UART hardware, you might end up in an infinite loop that never stopped even when a character was received.
  • the read of the receive register could be moved by the compiler to before the while loop - again because there's nothing in the function that indicates that *recv_reg is changed by the loop, there's no reason it can't be read before entering the loop.
The volatile qualifiers ensures that these optimizations are not performed by the compiler.

Saturday, July 27, 2013

Untar all files in a directory

#!/bin/bash
for a in `ls -1 *.tar.*`
do
        tar -zxvf $a
done

http://castyour.net/untar-all-files-directory

Bash for loop examples
http://www.cyberciti.biz/faq/bash-for-loop/

Other arguments
http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/


tar error - 'Child returned status 1'
http://www.linuxquestions.org/questions/linux-newbie-8/tar-error-'child-returned-status-1'-208083/

You used "tar -zxvf" for the second command. the 'z' option tells tar to use gzip to uncompress the file. Since you already uncompressed it in the first command, gzip doesn't know what to do with it, and it consequently croaks. Tar stops because gzip encountered a problem. So, with the file you have now, you would extract it with:
tar -xvf xxxxxx.x.x.tar

I would go back and re-gzip the tar file though (to save space):
gzip xxxxxx.x.x.tar
tar -zxvf xxxxxx.x.x.tar.gz


The first step uncompresses the tar ball.
In the second step, tar is expecting a compressed tar ball (.tar.gz).

You can either do:
Code:
tar -xvf fileName.tar
for an uncompressed tar ball
or 
Code:
tar -xzvf fileName.tar.gz
for a compressed tar ball, as the z switch uncompresses the tar ball using gzip.

I hope this helps
--Ian

Friday, July 19, 2013

Share folders between windows and ubuntu using VMWare

http://theholmesoffice.com/how-to-share-folders-between-windows-and-ubuntu-using-vmware-player/

Quick Guide to Using DoxyGen in Ubuntu


PhpDocumentor was easy to setup with the pear , but I just couldn’t get past the “nothing parsed” error. So I moved on to Doxygen
It might not be as mature, but hey, it works and thats what matters
1. sudo apt-get install doxygen doxygen-gui
2. in terminal doxygenwizard& will launch the gui
2. cd project directory
3. doxygen -g
4. edit Doxyfile

Comparison Python, Numpy, MatLab, Fortran

Useful info for optimization

https://modelingguru.nasa.gov/docs/DOC-1762

debugging and optimization flags

http://software.intel.com/sites/products/documentation/doclib/stdxe/2013/composerxe/compiler/fortran-mac/GUID-1DB4628E-2171-47B1-83CB-1D26FF71C5A0.htm

Thursday, July 18, 2013

Fortran Garbage Collector

http://stackoverflow.com/questions/13966955/does-fortran-have-a-garbage-collectorgc/13967374#13967374

Modern Fortran has many ways of declaring variables. Items simply declared will exist for the duration of the scope of the entity. So "real, dimension (N) :: array" declared in a procedure will automatically disappear when that procedure returns. Naturally variables declared in the main program or module variables or common (outmoded) will persist for the duration of the program.

allocate -- dynamic allocate 

Variables can be dynamically allocated with "allocate" (to do so, they have to be declared with the allocatable attribute). Since Fortran 95 allocatable variables that are local to a procedure are automatically deallocated when the procedure returns! They will not leak memory! (Some programmers might consider it good practice to explicitly deallocate the variables anyway, even though it isn't strictly necessary.) (Of course, you can waste memory in the sense of not explicitly deallocating a variable that you know that you don't need anymore.)

t is possible to leak memory with pointers. You can allocate memory with a pointer, then assign the pointer to to another variable, losing the previous association. If you didn't deallocate that memory you have a leak. The need for pointers is less in Fortran than in some other languages ... many things can be done with allocatable variables, which are safer -- no memory leaks

http://stackoverflow.com/questions/2300199/allocatable-arrays-or-pointer-arrays
Allocatable arrays Vs Pointer arrays?

Allocatable arrays can result in more efficient code because the arrays will be contiguous. Particularly if the array is passed to a subroutine, being contiguous can prevent the need for the compiler creating a temporary copy. For local variables in subroutines (without the SAVE attribute) (for Fortran 95 and higher), allocatable arrays are automatically deallocated upon exit from the subroutine, avoiding a memory leak. Memory leaks aren't possible with allocatables, except in the sense of the programmer not deallocating an array that is no longer need. With pointers, you can reassign a pointer, leaving some memory inaccessible and lost -- one form of a leak. If an allocatable will do the job, I recommend using that method instead of a pointer. Some reasons to use pointers: taking a section of an array, or creating a data structure such as a linked list. For the purpose of creating an array of size determined at run time, I'd use an allocatable.

Tuesday, July 16, 2013

Error: Unterminated character constant beginning at (1)

http://gcc.gnu.org/ml/fortran/2007-12/msg00212.html

Error: Unterminated character constant beginning at (1)
aaa.f:4.72:

           print *, 'Try one of "Skip", "Test", "Verbosity" or "Cleanup"
                                                                       1
Warning: Line truncated at (1)

Fixed-form Fortran has only 72 characters per line. What happens with
longer lines is implementation dependent. However, many
programmers/programs assume that everything after column 72 is ignored
(which gfortran does).

Solution: Fix the program by splitting the line or by using the 
-ffixed-line-length-n (n is a non-negative integer).




Fortan90 Symbols

"What we consider here, is the use in the
FORMAT of the dollar symbol $ in order to remove the generation of a new
line (Line Feed/Carriage Return),"
http://www.urz.uni-heidelberg.de/Dok/Fortran90forF77.html


"By defining a new data type or operator, and overloading operations and
procedures (so that you can also use the plus + as the symbol of
addition of intervals and not only of ordinary numbers)"
http://www.urz.uni-heidelberg.de/Dok/Fortran90forF77.html



Password-less login

expect and automatic login

"expect" command is simply expecting a text from terminal, and send it when it meets expectation.
sudo apt-get install expect
Installation is done.

Sample usage.

Let's make simple shell program
vi abc.sh

Type into shell shell program.
#!/usr/bin/expect -f
set timeout 100
spawn ssh userid@servername 
expect "/home/devtrigger/.ssh/id_rsa" 
send "password\r"
expect "servername:"
send "bash\r"
interact



http://devtrigger.blogspot.com/2012/02/install-expect-on-ubuntu-11.html

http://greg-n-blog.blogspot.com/search/label/ssh

http://stackoverflow.com/questions/12202587/ssh-script-that-automatically-enters-password

http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command