JavaScript closure inside loops – simple practical example

Well, the problem is that the variable i, within each of your anonymous functions, is bound to the same variable outside of the function. ES6 solution: let ECMAScript 6 (ES6) introduces new let and const keywords that are scoped differently than var-based variables. For example, in a loop with a let-based index, each iteration through … Read more

Do I cast the result of malloc?

TL;DR int *sieve = (int *) malloc(sizeof(int) * length); has two problems. The cast and that you’re using the type instead of variable as argument for sizeof. Instead, do like this: int *sieve = malloc(sizeof *sieve * length); Long version No; you don’t cast the result, since: It is unnecessary, as void * is automatically … Read more

PHP parse/syntax errors; and how to solve them

What are the syntax errors? PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can’t guess your coding intentions. Most important tips There are a few basic precautions you can always take: Use proper code indentation, or adopt … Read more

Why shouldn't I use mysql_* functions in PHP?

The MySQL extension: Is not under active development Is officially deprecated as of PHP 5.5 (released June 2013). Has been removed entirely as of PHP 7.0 (released December 2015) This means that as of 31 Dec 2018 it does not exist in any supported version of PHP. If you are using a version of PHP … Read more

What is an undefined reference/unresolved external symbol error and how do I fix it?

Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote]. Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters … Read more

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

Notice / Warning: Undefined variable From the vast wisdom of the PHP Manual: Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued … Read more

The Definitive C++ Book Guide and List

Beginner Introductory, no previous programming experience Book Author(s) Description review C++ Primer* * Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review. Stanley Lippman, Josée Lajoie, and Barbara E. Moo (updated for C++11) Coming at 1k pages, this is a very thorough introduction into C++ that covers just … Read more

How do I compare strings in Java?

== tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically “equal”). Objects.equals() checks for null before calling .equals() so you don’t have to (available as of JDK7, also available in Guava). Consequently, if you want to test whether two strings have the same value you … Read more