C++: Difference between revisions

From David's Wiki
Line 71: Line 71:
===Memory===
===Memory===
==== Garbage Collection ====
==== Garbage Collection ====
Traditional C++ does not have garbage collection.
Traditional C++ does not have garbage collection.<br>
After using <code>new</code> to allocate an object, use <code>delete</code> to deallocate it.   
After using <code>new</code> to allocate an object, use <code>delete</code> to deallocate it.  <br>
You can also use C allocation with <code>malloc</code>, <code>calloc</code>, <code>alloca</code>, and <code>free</code>, though it is [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new not recommended] since these are not type-safe.
You can also use C allocation with <code>malloc</code>, <code>calloc</code>, <code>alloca</code>, and <code>free</code>, though it is [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new not recommended] since these are not type-safe.<br>
In C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection.
In C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection.



Revision as of 13:21, 7 October 2019


Usage

How to do things using the C++ standard library (stdlib).

Compilation

g++

g++ my_driver.c [-Iincludefolder] -o my_program.out

Misc optimizations

  • -std=c++17 for C++17 support
  • -O3 for level 3 optmizations

Strings

String Interpolation

Reference

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string a = "a", b = "b", c = "c";
    // apply formatting
    std::stringstream s;
    s << a << " " << b << " > " << c;
    // assign to std::string
    std::string str = s.str();
    std::cout << str << "\n";
}

Filesystem

Reading and Writing

Reading and writing is done using fstream.
If you don't need r/w, use istream for reading or ostream for writing.

#include <iostream>
#include <fstream>

int main() {
  std::istream my_file("my_file.txt");
  std::string line;
  # Read line by line
  # You can also read using <<
  while (getline(my_file, line)) {
    std::cout << line << std::endl;
  }
  return 0;
}

Regular Expressions

Reference

Threading

Sleep

std::this_thread::sleep_for(std::chrono::milliseconds(1));

Memory

Garbage Collection

Traditional C++ does not have garbage collection.
After using new to allocate an object, use delete to deallocate it.
You can also use C allocation with malloc, calloc, alloca, and free, though it is not recommended since these are not type-safe.
In C++14, you can use shared pointers which does have automatic garbage collection.

Programming Styles

Modern C++

List of resources
Prefer newer std functions available in C++17.
Use shared pointers instead of new and delete.

  • Use clang-format.

Orthodox C++

Reference
Somewhat opposite of modern C++.
Basically only use C++ for its classes. Do everything else C-style. The main benefit is compatibility with older compilers/libraries and easier understanding for people less familiar with newer C++ features.

  • Don't use C++ runtime wrapper for C runtime includes (<cstdio>, <cmath>, etc.), use C runtime instead (<stdio.h>, <math.h>, etc.)
  • Don't use stream (<iostream>, <stringstream>, etc.), use printf style functions instead.
  • Don't use anything from STL that allocates memory, unless you don't care about memory management.

Boost

STL