User Tools

Site Tools


Sidebar

Dan's Wiki

DokuWiki Instructions (local) DokuWiki Manual
Site Checker (Orphans Wanted)

Edit Sidebar

programming:cplusplus:start

C++ Notes and Information

Helpful tutorials on YouTube are by TheChernoProject https://www.youtube.com/channel/UCQ-W1KE9EYfdxhL6S4twUNw

(Too much of a beginner) Trying course from https://www.youtube.com/watch?v=vLnPwxZdW4Y

include

Includes library of code, either from local (name is in double-quotes) or in the language-provided libraries (name is in angle-brackets).

#include <iostream>

Use angle brackets for built-in libraries.

using namespace

using namespace std;

Places this code into the std namespace. That way, we can use cout instead of std::cout. However, I don't use this namespace because TheCherno says not to, instead, always give explicit namespace value.

Linker

The C++ compiler creates and Object file, but separately. The Linker puts them together.

Pointers

https://www.youtube.com/watch?v=DTxHyVn0ODg&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=16

A pointer is an integer which stores a memory address. Types have nothing to do with that.

The value 0 is not a valid memory address. It means 0

int main()
{
   // void* ptr = 0; // or   void* ptr = nullptr;
   int var = 8;
   // Where are you in memory
   int* ptr = &var; // Get the location of var, and put it into ptr as an integer pointer.
 
   *ptr = 9; // int value pointed at by pointer "ptr"
 
   std::cin.get();
}
int main()
{
   char* buffer = new char[8];// This uses heap
   memset(buffer, 0, 8);
 
   char** ptr = &buffer;// Pointer to a pointer
 
   delete[] buffer;// delete afterward to be nice
 
   std::cin.get()
}

Reference

Pointers and references are about the same thing. How we write them is different. References are syntax sugar on top of a pointer.

References reference an existing variable.

int main()
{
   int a =5;
   int& ref = a;// the & is part of the int defintion
   // We have created an alias. It is not really a variable.
   // We can use ref as though it is "a"
 
   ref = 2;
   std::cout(ref);
}
// Works, but this is clumsy
 
void Increment (int* value)
{
   (*value)++;// Value at the pointer "value"
}
int main()
{
   // We want to pass the memory address.
   int a = 5;
   Increment(&a);
   LOG(a);
}
// Use a reference instead of a pointer
 
void Increment(int& value)
{
   value++;
}
int main()
{
   int a = 5;
   Increment(a);
   LOG(a)
}

Memory

There are two areas of memory, Stack, and Heap.

StackWhen scope for variable ends, the memory is freed.
HeapStays there until manually removed with delete

new keyword

The new keyword allocates memory from the Heap. It returns a pointer to it. The number of bytes to allocated is determined by the argument. int provides 4 bytes, for example.

int * a = new int;
 
*a = 2;
 
std::cout << *a << std::endl;
int * a = new int;
// is nearly identical to this:
int * a = malloc(4);

Visual Studio Formatting/Indenting C++ code

Use Control-K, followed by Control-F. 7/5/2019

Separate Classes into Headers and CPP files

C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of .h and contains class definitions and functions. The implementation of the class goes into the .cpp file. By doing this, if your class implementation doesn’t change then it won’t need to be recompiled….

separatecompilation.pdf

Difference between Dot and Arrow

Use Arrow when the value on the left is a pointer, use a dot when it is a class instance.

To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. Details

Troubleshooting while Building

Sometimes the *.h.gch will get corrupted or contain outdated information, so deleting that file and compiling it again should fix it. Details

programming/cplusplus/start.txt · Last modified: 2022/02/28 18:43 by dwheele