Tuesday, July 16, 2013

The static keyword in C/C++ programming

I will use a very simple project made by 2 files: main.c and extra.h  and their contents are reported below:

/* ****** extra.h ************* */

#ifndef EXTRA_H
#define EXTRA_H

/* Declaration of the static variable with file scope */
static int FileVariable=100;

void SetToOne()

void SetToTwo()

int GetValue()
    return FileVariable;

int CheckCounter()

     /* Declaration of the static variable within the function body */
    static int nCounter=0;
    return nCounter;   


/* *** *** *** main.c *** *** *** */

#include <stdio.h>
#include "extra.h"

int main()
    printf("Testing the static keyword... \n");
    printf("FileVariable is %d \n", GetValue());
    printf("FileVariable is %d \n", GetValue());

    printf("Counter is %d \n", IncreaseCounter());

    return 0;

On Linux/Mac OS X, to compile the program with gcc and run it, use the command:

cc -o main.c; ./a.out

If you made everything properly, the output will be:

FileVariable is 1
FileVariable is 2
nCounter is 3

What happened?
In C/C++, the static keyword has two basic uses.

1) To limit the visibility of a function or variable to the current file. 
This is the case for the variable FileVariable which is visible only to  the functions defined in extra.h, i.e. SetToOne, SetToTwo, GetValue and IncreaseCounter.
Just to be clearer, if you already know what global varibles are, well, static variables are just like global variables but only within the file they are defined.

If you attept to to do use or modify FileVariable within the main.c, you'll get an ERROR!!!
Try it!

FileVariable=100;  /* does not work! */

The only way to allow the previous expression is to remove the keyword static. Doing so,  FileVariable becomes a global variable and you can modify it freely both in main.c and extra.h

2) To let a variable to survive even if the function having it terminates.
Is it clear to you why nCounter is 3?
Within main.c, we called function 3 times! Check it out!
Without the keyword static, the variable Counter would have been created and re-set to zero at each function call.
Try it!
Remove the keyword static  and re-run the program.

In C++ the static keyword can also be used within a class, as  a counter. In this case, the static variable must be public and must be initialized outside the class.

class MyClass
    int    m_nVal;  // just one integer variable
    float m_fVal; // just one float variable

   MyClass(int nVal, float fVal)   // constructor for this class

  static int Counter;  // Our  static counter


// Initialization of the static variable...
static MyClass::Counter=0;

Monday, July 15, 2013

C++ and gcc on Linux/Mac: How to set your custom directory to include header files

Your profile:
You are using OSX or Linux and you are writing some code in C/C++  compiling it with gcc...
how can you modify the path in which gcc looks for your header files?

Simply enough you have to modify two environment variables: C_INCLUDE_PATH and CPLUS_INCLUDE_PATH, for C and C++ respectively.

Suppose you want to add the directory /opt/local/include for a C++ project in this case:

export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}:/opt/local/include

in a similar way you can modify the C_INCLUDE_PATH variable.

To make this modification permament for every new terminal session, the export line must be put into the hidden .bash_profile file in your home directory.

How to search files and folders with a given partial name in a terminal

If you are looking for a specific file or folder in your filesystem, you need to use the linux/mac/UNIX command find

Type in a terminal the following command:

find path -name "partial name"

Suppose, you want to run a search across the entire filesystem looking for a file called foo (or containing foo in its name), in this case your command becomes:

find / -name "*foo*"


1) If you really want to start the search from root, keep in mind that as normal user you could not access some directories, so put a sudo in front of the command to become superuser! :

sudo find / -name "*foo*"

2) If you start from root, your search will take some time....

3) You can save the results in a file called my_search.txt in this way:

sudo find / -name "*foo*"> my_search.txt

(the file my_search.txt will be created in your current directory,  the one you get with the command pwd)

Friday, July 5, 2013

From String to const char pointer or char array and... back!

You can operate on strings in C++ both via the string class or the old (i.e. C like) const char pointer.

Notes on string:

To use the class string you have to include the string header:
#include <string>

In addition, to avoid mentioning every time the std namespace, I use the using keyword:

using namespace std;

(without this instruction you'll need to type std::string variable_name instead of just string variable_name)

From char* or char array to string

This is easier

const char *pCh="foo char* ";  // pointer to const char

/* two possible conversion methods */ 
string strTemp(pCh);    // through constructor
string strTemp= pCh;   // through assignment operator

Since the strong similarity between pointers and arrays, the previous commands work also with char arrays...

char aCh="foo char[] "; 

/* two possible conversion methods */ 
string strTemp(aCh);    // through constructor
string strTemp= aCh;   // through assignment operator

From string to char*or char array

In this case, the conversion to the const char is very quick, while going to char array takes a bit longer... 

string to const char*

string strMyString="This is a string";  // creation of a string object

/* using the c_str() method of the string object*/
const char *pCh=str.MyString.c_str(); 

string to char array

/ *
In this case, we need:
 (1) to assign the correct size of the char array 
 (2) to copy the string to the char array (char after char or by using strncpy) 
 (3) a null character to terminate the char array.

string strMyString="This is a string";  // creation of a string object with size = 16!

/* using the size() method of the string object*/
char aCh[str.MyString.size()+1]; // the +1 is for terminating the char array with '\0', size=17!

// copying via the string strncpy...
strncpy(aCh, strMyString.c_str(), strMyString.size()); // SYNTAX: strncpy(destination, source, length)

// ... alternatively we can copy using a for loop:

for (int ii=0; ii<strMyString.size(); ii++)
     aCh[ii]=strMyString[ii];    //the loop stops at 15 and that's fine... we have copied 16 characters, since we started from 0!

// either way you use to copy the string, do not forget to add the null terminating character!

aCh[strMyString.size()]='\0';   // at position 16 in the char array

The trickiest part is always to take into account for the correct size of the char array, paying attention to the fact that in C/C++ arrays start at 0, not 1!!!