Sign in

“extern” allow us using the same variable among different transition units(cpp files).

RegisterDay.cpp: here the count is declared & defined the first time in the whole program.

namespace gym {
int count;
void FirstTraining() {
count += 1;

TrainingDay.hpp: I also want to add up to the same variable count, to do so, I need to tell the program this count has already been declared somewhere (in our case, in RegisterDay.cpp).

namespace gym {
extern int count;

Now in TrainingDay.hpp, I have informed the existence of count, I’m able to use it in all the *.cpp files which include TrainingDay.hpp file. For example:


namespace gym {
void run() {
count += 1;

Macro acts more like scripts. Macro #define A bbb replace A with bbb in the code.

#define oldmax(x, y) ((x)>(y)?(x):(y))q = oldmax(abc, 123);

After the preprocessor goes through the code, it will look like this:

q = ((abc)>(123)?(abc):(123));

Macro operators


The ## operator takes two separate tokens and pastes them together to form a single token.

Following macro can be used to initialize the variables in the beginning.
(Note you can use \to type in new line.)

#define INITIALIZATION(type, varname, value) \
type orig_##varname = value;

After declaration, we can use it like the follows:

#include <string>
INITIALIZATION( std::string, name, “Cindy” );
INITIALIZATION( float, weight, 55.0f );
INITIALIZATION( float, height, 166.0f );

Test for output:

std::cout <<"orig_name = " << orig_name <<endl; 
std::cout <<"orig_weight = " << orig_weight <<endl;
std::cout <<"orig_height = " << orig_height <<endl;

Environment: macOS
C++ Standard: 17
IDE: CLion

This tutorial will use DuckDB as example. From their installation page I downloaded the zip file which includes the follows:

1. Put files under my project

Put the folder under [myproject]/dependencies. Here myproject=duckdb-test.

1. Vector Basics
- Vector definition
- Add an element to Vector
- Delete element in Vector
- Iterate Vector
- Clear Vector
- Pass Vector as a function parameter

2. Vector Optimization
- reserve()
- shrink_to_fit()
- emplace_back()
- capacity()

1. Vector Basics

std::vector is a dynamic array that doesn’t have a fixed size. From the perspective of memory, the dynamic array is stored continuously on memory.

Now, what happens when we extend the Vector?

Table 1

Vector definition

Create vector of simple data(int); vector of data structure(struct); vector of pointer(pointer of int)

std::vector<int> vectorOfInt;std::vector<MyStruct> vectorOfStruct;std::vector<int*> vectorOfPointerOfInt;

Add an element to Vector


Delete element in Vector

// delete the second element

Stack and Heap are 2 areas we have in our RAM.

  • Stack Allocation (a.k.a. Temporary Memory Allocation) — stores local variables, pointers to the local variables
  • Heap Allocation — stores global variables

1 min take away


A Template let the compiler “write” codes for you based on the given rules. Concrete code is only created in compilation ONLY when the template class/function is called(with the given argument).


To declare a template (of class/function), we use the following syntax:

template <parameter-list> declaration

The “declaration” could be a normal class or function declaration. A concrete example here:

The compiler generates a function out of the template with the specified type.

  • it increases the compilation time
  • generated function is locally scoped in the compilation unit
  • might lead to horrible compile-time-errors if incorrect template arguments are used


Inside struct/class

  • Case 1: without “static”
  • Case 2: with “static”
  • Case 3: with “static” — better way to call static member

Outside struct/class

  • Case 1: without “static”
  • Case 2: with “static”

— — — — — — — — — — — — — — — — — — — — — — — — — —

“static” could has 2 different meaning corresponding to it’s context:

  • inside struct/class
  • outside struct/class

“static” inside struct/class

The “static” member will share memory with all the instance of the struct/class. No matter how many instances of struct/class are created, there is only one “static” member.

[0] Unlike Java, the file-name of *.cpp doesn’t has to be identical to the class name. “File” for C++ is more like a simple container. And the suffix of the file “.cpp” or “.hpp” tells the compiler in which form this file is.

[1] One *.cpp file doesn’t always corresponding to one translation unit.
Some *.cpp file could include several other *.cpp files, but since the compiler is fed with one single *.cpp file, there will be ONLY one translation-unit.

[2] Unreadable Machine Code

[3] In order to see what’s happening here. We can convert *.o to *.asm …


🦠 🈲️ 🌎

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store