Thursday, September 9, 2021

More C++: Datatypes. (Qualifiers, Bit Fields, ...)

Note: Much of my programming has been done in JS, so you could say I think in JavaScript. So, from that perspective...

Based on Module 3: Datatypes of Bill Weinman's LinkedIn course.

Covered in this chapter: 

Overview of data types
Integer Types
Integer Sizes
Fixed-Size Integers
Floating-point types
Characters and strings
Character escape sequences
Qualifiers
References
Structured data
Bit fields
Enumerations
Unions
Defining types with typedef
The 'void' type
The 'auto' type
Unambiguous null pointer constant
Challenge: A library card data structure
Solution to challenge

Qualifiers

CV Qualifiers

const -- makes program throw an error if reassignment is attempted

mutable

volatile


Storage Duration Qualifiers

static -- lets a variable persist outside of the block in which it was defined

register

extern


Bit Fields

Allow you to pack multiple values into less space
Avoid in threaded programming
Use when storage space comes at a premium
Can behave differently on different OSs

Enumerations

Type safe "alternative" to preprocessor macros
Behave kind of like constants 
Use mostly for grouping scalar constants together when they have a common usage
Enumerated types have integer values

Unions

Data structure that allows you to use the same memory space for different types
Sometimes used for crude polymorphism (multiple representations of same data)

Defining Types: TypeDef

A typedef can be used as an alias for a type


Quiz

Any valid type may be a member of a struct
The 'cstdint' header allows you to specify size of integers, both signed and unsigned
You would use the 'int32_t' type when you need a signed int with a fixed size of 32 bits (other options are 8, 16, 64)
The primitive integer types are not guaranteed to be specific sizes

correct format to concatenate two primitive strings in C++: const char * cstring = "String" " and another string";

Once defined, a reference can never be re-assigned.
The types defined in the 'cstdint' header are guaranteed to be consistent sizes.

Floating point types sacrifice precision for scale.

The special nullptr value is used to provide an unambiguous null value for pointers.


Monday, September 6, 2021

Return to C++: Basics

 Notes based on Module 2 of Bill Weinman's LinkedIn course.

Pointers

int * ip = &x      //designates ip as a pointer (ip = int pointer), and gets memory address of x and stores it to ip

int & y = x    //Makes y a reference to x (?)


References

const int & y = x //Prevents changing x's value by changing y's value


"receding the pointer" = changing what it points at


A REFERENCE cannot be redefined to refer to a different variable.



Primitive Arrays

Size is set upon initialization and can't be changed thereafter

int array[] = {1,2,3,4,5};

int x = array[2];

//x will now return 3




Strings

Primitive string or c-string = a special (null-terminated) array of characters

char my_string[] = {'a', 'b', 'c', 0};




Structs

Which statement would correctly declare a variable s1 for the structure S declared in the following code?


struct S {

    int i;

    const char * s;

};


Answer: S s1 = { 3, "string one" };


Struct members default to public access.




Functions

example: 

void no_return(int i)

{

  puts(i);

}





Classes

Class members default to private access



Quiz notes

A REFERENCE cannot be redefined to refer to a different variable.

A c-string is an array of characters

Use break to exit out of a switch

Any amount of whitespace is equivalent.

Semicolons must be used to terminate statements.

An expression is anything that returns a value.

Pointers are type-aware


What is one advantage of using cout instead of printf or puts?

>> The cout class is type aware, and can string together different data types.


Disadvantage:

The `cout` class tends to create LARGER executable files, which is one of its disadvantages.


The cout class is found in the iostream header


What are the necessary parts of a C for loop?

>> an expression, a condition, and post-loop control


What is a primitive string in C++?

>> Primitive strings in C++ are arrays that end in 0, so they can be iterated through like other arrays.


The for loop uses three expressions to control flow. 


When using a range-based for loop with a c-string you must test for the null terminator.


The "main" function is the entry point of the program, called by the OS when the program launches


While struct members default to public access, class members default to private.


Initialized arrays have values defined in the array, and uninitialized arrays do not have values defined.

Saturday, May 30, 2020

Q-Po (Javascript game project) in retrospect

I made a JavaScript game called Q-Po in 2016. It involved thousands of lines of original code. It was a strategy game.

I wrote up most of what I did in a journal here (Google Drive) which I am now making public. 

The project repository is here (Github).

Videos of the game are here (Youtube playlist). 


I have since let the server expire since I didn't want to keep paying for the domain. I could re-deploy it if I wanted to. In the process of making the game, I learned a lot about development, about JavaScript, and about project management. I recruited a few people to help with the project in various ways. A close friend helped me brainstorm and did a little cross-platform development. I paid someone to make music for it. I learned about neural networks to implement an adaptive AI. Someone I met at a JavaScript meetup inserted a few crucial lines of code into the back end to make the project deployable, then got hired somewhere after mentioning the project in an interview. (I very naively thought he was in the project for the long haul.) 

What's a Sourcemap?

Unpublished post from 2016/2017:

http://blog.keithcirkel.co.uk/why-we-should-stop-using-grunt/

http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ :

Basically it's a way to map a combined/minified file back to an unbuilt state. 

Chrome Canary?

But why care about source maps?

Right now source mapping is only working between uncompressed/combined JavaScript to compressed/uncombined JavaScript, but the future is looking bright with talks of compiled-to-JavaScript languages such as CoffeeScript and even the possibility of adding support for CSS preprocessors like SASS or LESS.
In the future we could easily use almost any language as though it were supported natively in the browser with source maps:
  • CoffeeScript
  • ECMAScript 6 and beyond
  • SASS/LESS and others
  • Pretty much any language that compiles to JavaScript

BTW, what's Traceur? Takes future-JS and turns it into now-JS. (If you like to be ahead of the curve when writing code, but riding the wave with your deployment build.)

Friday, April 29, 2016

Git Branch, Diff, and Merge for Noobs

Prerequisites: terminal basics, git basics (e.g. git add, git commit)

Branching helps you try out new features while leaving a copy of your existing codebase intact. This is nice for working on teams, because it lets each team member work on a different section of code, without them having to sift through the changes the other team members are making at the same time (as displayed by "git diff".)

Diff is for difference. When you tell your terminal " git diff 'master' ", you're telling git to show you the differences between your working directory (the branch you've checked out) and the master branch.

The process goes something like this. In the terminal, from your working directory:

git checkout -b "name_of_new_branch"

[Go to text editor, make changes to your code]

git diff "master"

At this point, the terminal summarizes the differences for you. A few helpful hints:

  • @@ marks one changed section. (In addition to the lines you changed, diff shows you the lines surrounding them, to give you some context.)
  • - marks the lines you removed (or made changes to)
  • + marks the additions or changes you made (on your new branch)

When you're done making changes (you've completed your new feature or whatever,) you switch back to the branch you were on before -- say, "master" :

git checkout "master"

And use "git merge [branch]" to apply your changes:

git merge "name_of_new_branch"

If you're done using the branch you made, you can delete it:

git branch -d "name_of_new_branch"

Rinse and repeat.

Monday, April 18, 2016

Grunt for Noobs

Prerequisite knowledge: Node.js, npm

Grunt is a JavaScript minification tool. It can take a bunch of separate .js files, pack them all into a single .js file, and make that file really small by doing things like removing comments and unnecessary whitespace, and turning long variable names into single characters.

Conveniently, this also serves to obfuscate your code, making it trickier to hack around in or repurpose without permission.

This can be useful when you've reached the stage where you have a bunch of code in lots of different files, and you want to put them all into one file to stick them on your server (this is part of deployment.)

I used the following video to learn Grunt. It gets right to the point with a nice variety of examples.



Sunday, January 3, 2016

My 8 Favorite Keyboard Shortcuts

The environment I use is probably important to know, here...

CMD + tab -- switch between windows/apps (IDE, Terminal, Finder, etc.)
CMD + w -- close tab or program
CMD + ALT + arrow keys -- snap window to left-half/right-half/top-half/bottom-half [via Spectacle]
CMD + shift + F -- fullscreen/presentation mode (nice for maintaining focus and reducing visual clutter)
CMD + R -- reload (in Chrome), compile/build/run (in XCode)
CMD + 1/2/3/... --  go to 1st/2nd/3rd/... tab
CMD + shift + N  -- new folder (Finder)
CMD + N -- new window

I expect this list to grow with time.

Got a favorite of your own? Leave it in the comments!