====== c cpp common case ====== ===== Compile, make ===== make and search for error. make -n ===== Create and delete array ===== source: http://en.wikibooks.org/wiki/C_Programming/Memory_management http://www.cprogramming.com/tutorial/c-vs-c++.html ==== c array ==== These will declare and allocate memory for the arrays: int int_arr[100]; char char_arr[100]; int *array = malloc(10*sizeof(int)); int* tree = new int[10]; This only create a pointer: char *char_arr; Memory allocated with xalloc must be freed by free(): int *myStuff = malloc( 20 * sizeof(int)); if (myStuff != NULL) { /* more statements here */ /* time to release myStuff */ free( myStuff ); } Array created with new[] must be freed by delete[]: delete []tree; ===== Pass by reference ===== * https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr233.htm ==== C only ==== #include void swapnum(int *i, int *j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swapnum(&a, &b); printf("A is %d and B is %d\n", a, b); return 0; } ==== C++ only ==== #include void swapnum(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swapnum(a, b); printf("A is %d and B is %d\n", a, b); return 0; } In order to modify a reference that is const-qualified, you must cast away its constness with the const_cast operator. The following example demonstrates this: #include using namespace std; void f(const int& x) { int* y = const_cast(&x); (*y)++; } int main() { int a = 5; f(a); cout << a << endl; } ===== Casting const char* and char* ===== The c_str() method of the std::string class returns a pointer to const char so that you can't modify the result. Unfortunately, there's no good solution, and you can thank the author of the library who didn't write const-correct code. Now, if you know that the function will not modify the string, you can use const_cast to allow the conversion: char *FileExt = const_cast ( path.c_str() ); If you're not sure whether the function will modify the string or not, you have no choice but to create a C-style string copy: char *FileExt = new char[path.size() + 1]; std::strncpy ( FileExt, path.c_str(), path.size()+1 ); ===== Call C functions from C++ ===== * http://www.thegeekstuff.com/2013/01/mix-c-and-cpp/ ===== Pass class method as reference ===== E.g. we need to pass a thread method, which is a member of a C++ class to pthread_create. 1. Make the method static. Every C++ member function has an implicit, hidden, first parameter, this. So the method double cls_lasvm::kernel(int i, int j, void* kparam) is really: double cls_lasvm::kernel(cls_lasvm* this, int i, int j, void* kparam), and it is inappropriate/impossible to use it as a function-pointer parameter. To make progress, convert your method to be a static-member-method. That will remove the this pointer. You may still have other issues to overcome, but that is a good start. .h: static void stop_mac_link(int sig); .cpp: void NFCClient::stop_mac_link(int sig) <---- without static 2. Make global variable in the method also static. static struct mac_link *mac_link; 3. Define the static variable again to avoid undef refrence to variable: Aaa.h class Aaa { protected: static Aaa *defaultAaa; }; Aaa.cpp // You must define an actual variable in your program for the static members of the classes Aaa *Aaa::defaultAaa; <-------- no static ===== Working with string ===== ==== C string ==== ==== C++ string ====