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