96. Programming - YukaKoshiba/MyknowledgeDocs GitHub Wiki

Programming @English Version
Create Date:2025/07/10
Last Update Date:2025/07/10

Contents

Programming

Terms  Data Types  Arrays  Regular Expressions  Escape Sequences

Programming Languages  main Function  Functions  Call Stack

Compilers and InterpretersCompiler (Compilation)  Interpreter

Executable File Extensions
File Extensions by Programming Language
Basic Software File Structure
Naming Conventions

Programming Tips1. Magic Number

Makefile  Header Files  Structs  Include Guards  Ajax 

Programming Paradigms
MVC Model
Object-Oriented Programming (OOP)
Differences between Structs and Object-Oriented Programming
Method Chaining

FrameworksMicroframework
LibrariesHandling Libraries in Object-Oriented Programming

Memory Management  Valgrind

Tips for Processing / Common Errors:
Exception Handling  Decimal Calculations  Variable Scope  Data Exploration  Function Prototype Declaration
System Termination  Exit Status

Programming

The process of writing code to solve a specific problem or implement a particular task.

Common misconceptions about programming:
While programming ⊃ writing code in a programming language,
it's important to note that programming ≠ writing code in a programming language.
Writing code in a programming language = simply coding.

The way to start programming is not just by writing code in a programming language. Instead, you determine what you want to build → what steps to take to solve it → and based on that, decide which programming language you need to learn before you can start coding.

Programming involves algorithms and problem-solving in addition to coding.
Once you master programming, coding becomes easy, even if the programming language changes.

You might encounter many unfamiliar terms, but all keywords in programming are English words.

Fundamental premise to understand when programming
Computers can only process binary code (0s and 1s).
Therefore, computers cannot directly understand the words, symbols, or values that humans code.

When you run a program, the computer doesn't understand the code written in a high-level programming language that humans can comprehend. Instead, the code is automatically converted into binary code that the computer understands, and then the instructions are executed on the device.

Important Terms

  • Code
    A set of instructions written by a programmer to tell a device (like a computer) what to do.
  • Bug
    An error in the logic or implementation of a program that causes unexpected or incorrect output (a mistake in the system design).
  • Error
    A human action that produces an incorrect result, caused by misjudgment from developers or designers, or misunderstanding of system specifications.
  • Debugging
    The process of finding and fixing bugs within a program.
  • Integrated Development Environment (IDE)
    A software development environment equipped with convenient tools necessary for creating computer programs, such as file editors, explorers, terminals, and useful menu options.
    Famous IDEs
    Visual Studio Code: A free IDE created by Microsoft.
    Eclipse: A free, open-source IDE often used for Java development, originally developed by IBM.
    Visual Studio: Created by Microsoft.
    Differences between Visual Studio and Visual Studio Code

Programming Languages

Languages that help humans communicate with computers.

Complete Guide

Main Function

As a programming convention, some programming languages require the `main` function to be written.
The `main` function plays a crucial role as the starting point of a program in compiled languages and is a special function.
While not mandatory in scripting languages, writing code equivalent to a `main` function is sometimes recommended for code structuring.

Examples of Languages with and without a Main Function

Languages with a `main` function: Compiled languages (C, C++, Java, C#, Go, etc.)
Languages that are converted into machine code by a compiler before execution.
Compilers require a `main` function to identify the program's starting point.

Languages without a `main` function: Scripting languages (Python, JavaScript, Ruby, PHP, Perl, etc.)
Languages that are executed by an interpreter, which translates and runs the code line by line at runtime.
Even without an explicit `main` function, code is executed sequentially from top to bottom.

Functions

When a function is called, the program allocates memory and executes it according to the following steps:

  1. Frame Generation
    When a function is called, a new "frame" is generated on the call stack.
    A frame is a memory area that stores information necessary for the function's execution.
    A frame includes the following information:
    function arguments, function local variables, the function's return address (where to return after the function finishes), and other information necessary for function execution.
  2. Memory Allocation
    Once a frame is generated, memory areas for local variables and so on are allocated within the frame.
    This memory is valid only during the function's execution.
  3. Function Execution
    The frame is placed at the top of the call stack, and the function is executed.
    Variables are read and written within the function.
  4. Function Termination
    When the function finishes, the frame is "popped" from the call stack.
    The memory area within the frame is deallocated and becomes unavailable.
    The program returns to the calling function according to the return address stored in the frame.

Call Stack

A memory area that sequentially records information about functions called during program execution.

A frame is pushed every time a function is called, and a frame is popped every time a function ends.
The call stack is used to manage the order of function calls and to implement recursion, among other things.

The call stack is allocated in the memory's "stack area."
Since the stack area is a limited memory region, "stack overflow" can occur if recursive calls go too deep.

The concepts of call stack and frames are common to many programming languages.
Most programming languages, such as C, C++, Java, Python, and JavaScript, use a call stack during function calls.
However, the structure and behavior of the call stack may vary depending on the language and implementation.
For example, some languages may perform tail recursion optimization for performance.

Compiler and Interpreter

Compiler

A compiler is a computer software that translates (compiles) source code into a set of machine language instructions that a digital computer's CPU can understand.
It converts the entire source code into an executable format, such as machine code, at once.
The converted code can be executed directly later and is generally faster.
However, compared to interpreters, it requires more effort for development and debugging, as recompilation is needed every time the source code is modified before execution.
Examples: C, C++, Java, C#, Go, etc.

Compilation
The process of converting source code written in a high-level programming language (understandable by humans) into binary code that a computer can understand.

Interpreter

Reads and executes source code line by line immediately.
Interpretation and execution occur every time the code is run, and generally, execution speed is slower compared to compilers.
However, development and debugging are easier for the following reasons:
・Since it reads and executes line by line, you can immediately check the execution results after code changes.
・The scope of errors is narrowed when they occur, making debugging easier.
・Many interpreter languages use dynamic typing, so there's no need to explicitly declare variable types.
 → This reduces the amount of code to write, accelerating development.
 → It allows for flexible code writing.
・If an interpreter is installed, the same code can be run on different platforms.
 → This facilitates cross-platform development.
・Many interpreter languages offer powerful debugging tools, such as debuggers, which can streamline debugging tasks.
 → You can trace code execution step-by-step and monitor variable values.
Examples: Python, JavaScript, Ruby, PHP, Perl

Many interpreter languages are used in web development, but the distinction between compilers and interpreters doesn't solely depend on whether it's a web application or a standalone application.
While there are some commonalities between the two, their relationship is more complex.

Tips for Learning Programming

・Practice constantly, even if you take breaks (always get hands-on).
・Practice not just with tutorials and copying, but by devising your own algorithms and creating programs.
・Participate in open-source projects.
・Take notes on what you are learning.
・Don't hesitate to ask questions on online forums when you don't understand.
 Stack Overflow
 Reddit
 [Tips for asking questions]
 - First, check the FAQ on the website.
 - State the language and version you are using, your OS type, and bitness.
 - Describe what you are trying to achieve.
- Describe what actions led to the error.
- Include the complete error message.
- State that you have attempted to solve the problem yourself.

Executable File Formats

The extensions for binary files (files that have been compiled/converted from human-readable source code into machine code that the CPU can process) are as follows:
They may also have no extension.
Binary files are fundamentally written in only 1s and 0s, and you cannot open and view their contents.
(i.e., humans can't understand them by looking at them.)

Extension Description
.exe Windows OS executable file
.out Linux OS executable file
.bin Linux OS executable file
.app macOS OS executable file

Batch files

Extension Description
.bat Batch file executable from the Windows command prompt
.sh Shell script executable on Unix-like OS (Linux, macOS, etc.)
= The version of .bat for other OSes

File Extensions by Programming Language

By looking at a file's extension, you can tell which programming language the source code is written in, which helps with code reading.

Extension Programming Language
.py Python
Displayed in the terminal when executed.
.pyw Python
Not displayed in the terminal when executed.
.js JavaScript
.html HTML
.css CSS
.java JAVA
.c/.cpp C/C++
.rb Ruby
.php PHP
.json JSON
.xml XML
.md Markdown
Dockerfile Configuration file for creating Docker images
.h C/C++ header file

Basic Software File Structure

Understanding the common file structure of software helps in predicting file locations and aids in code reading.

Directory Name Description Example Files Held
bin Stores binary files
(executable files)
= Converted to 0s and 1s machine code that computers can execute.
.exe, etc.
lib Stores library files
= Program components
Dynamic libraries (.dll, .so, .dylib, etc.)
Static libraries (.a, .lib, etc.)
src Stores source code files
= Written by programmers (people)
Main constituent files of the program
include Stores header files
= Files containing function declarations, constants, etc., for languages like C/C++.
doc Stores document files
= Files describing software usage, specifications, etc.
README, manuals, etc.
config Stores configuration files
= Files describing software operating settings, environment settings, etc.
data Stores data files
= Data required for software execution
Images, audio, text, etc.
test Stores test code
= Files for verifying program operation

Naming Conventions

Variable naming prerequisites:
・Uppercase and lowercase letters are often treated as different variables.
・Reserved words (e.g., function names) cannot be used.
・Special characters (e.g., ' or $) cannot be included.
・Cannot start with a number.
・While not incorrect, conventionally, variable names start with a lowercase letter.
・At a minimum, standardize the notation method within the same program.
・Global variables and local variables in different classes or functions, even if they have the same variable name, are treated as distinct by the program, so it's generally best to avoid reusing the same variable names.

Aphorism: "A foolish consistency is the hobgoblin of little minds." - Ralph Waldo Emerson

Naming Convention Usage / Commonly Seen Languages Example
Upper Camel Case
Pascal Case
Class names
React component names
PHP, JavaScript
MyClass
HttpServer
Lower Camel Case Variable names, function names myVariable
getUserName
Snake Case Variable names, function names
File names
DB, Python
snake_case
file_name
Kebab Case CSS class names
HTML attribute names
my-class
http-server
Screaming Snake Case Constant names MAX_VALUE
PI

Keywords

"is" or "has" before variable names
A common convention to indicate that the variable represents a boolean value.
let isRunning = true; let hasCompleted = false;

Regular Expression (regex)

A versatile notation for representing a set of strings (patterns).

By performing pattern matching using regular expressions, you can conduct flexible searches adapted to various search string patterns.

In programming, being able to use regular expressions is crucial for improving software responsiveness. If regular expressions are not used, you would need to write thousands of conditional branches to cover all patterns, whereas using regular expressions allows you to cover patterns in just one line.

It can be used in various languages such as Java, Perl, Ruby, JavaScript, and Python. Beyond programming, it can also be used in modern word processors like Microsoft Word and OpenOffice. It is not available in older programming languages like COBOL or C.

  • Email address extraction: Efficiently extract email addresses from web pages or text.
  • Log file analysis: Easily extract error messages and warning messages from log files.
  • Text processing: Replace specific strings or delete unnecessary strings.
  • Ctrl+F, etc.: In addition to text, there are pattern matching methods that use regular expressions.

Usage considerations:

  • Be careful with escape sequences.
  • Be careful with the handling of hyphens (-).
    In Python, for example, if a hyphen is used inside `[]`, it may be interpreted as a range, causing an error. You should place it at the very end or escape it.

Terminology:

  • Greedy Match
    The default behavior of the regular expression engine.
    It attempts to match the **longest** possible string.
  • Non-Greedy Match
    It attempts to match the **shortest** possible string.
    Adding a `?` after a quantifier (*, +, ?, {m,n}) makes the regular expression engine behave non-greedily.

Uppercase letters mean "anything but ~".

Regular Expression Meaning Example
\d
\D
A single digit from 0-9
Anything other than a digit from 0-9
Phone number: \d\d\d - \d\d\d - \d\d\d\d
(3 digits - 3 digits - 4 digits)
\w
\W
Word character (letter, number, _)
Anything other than a word character (letter, number, _)
\s
\S
Whitespace (space, tab, newline)
Anything other than whitespace (space, tab, newline)
[...] Character class
Any one of the characters inside the square brackets
[0-5] → (0|1|2|3|4|5)
[0-9a-z] or [a-z0-9] → All digits and lowercase letters
[^...] Caret in a character class
Negation of the character class
[^abc] → Any character except a, b, or c
^ (caret)
$ (dollar sign)
Matches the beginning of a string
Matches the end of a string
^abc → Strings starting with abc
$abc → Strings ending with abc
|
(pipe)
Matches one of multiple patterns hero_regex = re.complex(r'Batman | Tina Fey')
*If multiple matches occur, the first one found is returned as a Match object.
.
.*
Any single character
Any zero or more characters
*Does not include newlines
a.b → a(any 1 character)b
a.*b → a(any 0 or more characters)b
? Optional pattern
Non-greedy match
The preceding character can be absent
abc? → ab or abc
bat_regex = re.complex(r'Bat(wo)?man')
*The `(wo)?` part means it can match or not match.
*
+
Matches 0 or more times
Matches 1 or more times
a*b → a (0 or more times) b
a+b → a (1 or more times) b
( ) Grouping
Group numbers can also be specified
group(0): Entire match
group(1): First group
group(2): Second group, etc.
refix = (\d\d\d) - (\d\d\d-\d\d\d\d) text = "123-456-7890" match = re.search(pattern, text) if match: print(match.group(0)) # 123-456-7890 print(match.group(1)) # 123 print(match.group(2)) # 456-7890
{ } Specifies repetition count (H){3} → HHH
(H){3,5} → HHH|HHHH|HHHHH
By default, it's a greedy match, so HHHHH matches.
(H){3,5}? → HHH|HHHH|HHHHH
It becomes a non-greedy match, and HHH matches.
r'...' Raw string literal
Does not interpret backslashes as escape sequences
Python-specific feature
r'C:\Users\user\Documents'

Data Types

Typical Byte Sizes for Data Types

While the exact numbers vary depending on the programming language and compiler, common sizes are as follows:

Data Type Bytes
int 4
float 4
double 8
char 1
string/char* 4 or 8
long long 8

Comparison of char arrays and string types

char array

  • Stores a collection of characters in a contiguous memory region.
  • Must always be terminated with a `\0` (NULL) character.
  • You need to manage the string length yourself.
  • String manipulation functions must be implemented by yourself or by using standard library functions (e.g., strcpy, strcat).
  • Allows for lower-level operations but is more prone to errors.

string type

  • Treats strings as objects and automatically manages string length and operations.
  • No need to worry about `\0` (NULL) characters.
  • Provides a rich set of methods for string manipulation.
  • Enables higher-level and safer string operations.

Arrays

Arrays allow you to group multiple data items under a single name, simplifying program writing. Unlike declaring variables one by one, array elements are **stored contiguously in memory, enabling high-speed access.**

The size of an array is usually fixed, meaning that once the size is determined, it cannot be changed. Also, an array can only store elements of the same data type.

Each programming language has its own characteristics for arrays. For details, please refer to the respective programming language pages.


Escape Sequences

Characters that are difficult or impossible to write directly as strings in code. Generally, a backslash `\` is often prepended to the original symbol you want to escape.

Reference

Symbol Escape Sequence Notation Symbol Escape Sequence Notation
+ (addition) and * (multiplication) \+, \* (space) \s
(any whitespace character)
*Space, tab, newline, etc.
\s $ (dollar) \$
( ) (parentheses) \(, \) ' (single quote)
" (double quote)
` (backtick)
\', \", \`
\ (backslash) \\ ASCII Backspace (BS) \b
ASCII Form Feed (FF) \f ASCII Line Feed (LF) *Deprecated \n
ASCII Carriage Return (CR) \r ASCII Horizontal Tab (TAB) \t
ASCII Vertical Tab (VT) \v Unicode character name from database \N{name}
ASCII character represented in octal \ooo ASCII character represented in hexadecimal \xhh
16-bit Unicode character \uxxxx 32-bit Unicode character \Uxxxxxxxx

Basic escape sequences are often common across languages and environments, but it's important to note that they might not be supported or the syntax might differ.

  • Octal and hexadecimal character representation (\ooo, \xhh):
    Commonly used in languages like C and C++, and also available in Python, but may not be supported in other languages.
  • Unicode character escape sequences (\N{name}, \uxxxx, \Uxxxxxxxx):
    Can be used in languages and environments that support Unicode, such as Python. However, in languages like JavaScript and Java, while Unicode escape sequences are supported, the syntax might differ.

Tips for Writing Programs

1. Avoid Magic Numbers

A Magic Number is a numerical value written directly in the code whose meaning is not clear.

uint8_t buffer[512];

In the C language code above, it's unclear what the number 512 represents. This number is a Magic Number.

Magic Numbers should be avoided as they significantly reduce code readability and maintainability.

  • Reduced readability: It becomes difficult to understand the code's intent.
  • Reduced maintainability: Due to decreased readability, there's a risk of missed corrections or unintended modifications.
  • Reduced reusability: If a numerical value depends on a specific context, it becomes difficult to reuse it elsewhere.

Ways to avoid Magic Numbers:

  • Use constants.
  • Use enums (group related numerical values and define them as an enum).
  • Add comments.

For example, taking the code above, to avoid Magic Numbers, you could write it as follows:

int byte_num = 512; uint8_t buffer[byte_num];

Makefile

A configuration file for automating program compilation and building (the process of creating an executable program). It is frequently used with compiled programming languages like C and C++. Other programming languages also have files or modules with similar functionalities. For instance, Python's `.py` files and Java's `.java` files can, in a sense, serve roles similar to header files.

Roles of Makefile:

  • Automating compilation
    Describes how to compile and link (the process of combining multiple files into an executable) the various source files (e.g., .c or .cpp) that make up a program. This saves the effort of typing commands one by one and enables efficient building.
  • Dependency management
    Describes which files depend on which other files. This allows only the necessary files to be recompiled based on dependencies if some files are modified.
  • Streamlining tasks
    Can automate various tasks beyond compilation, such as running programs, testing, and cleaning (the process of deleting unnecessary files).

Basic structure of Makefile:

Makefile primarily consists of the following elements:
  • target
    The name of the file you want to create or the process you want to execute. For example, executable file names or object file names can be targets.
  • dependency
    A list of files required to create the target. If dependencies are updated, the target will be recreated.
  • command
    Commands to be executed to create or run the target. Compiler and linker commands are described here.  

Advantages of Makefile:

Makefile is a particularly useful tool when dealing with large-scale projects or complex build processes.
  • Reduced build time: Only modified files are recompiled, shortening build time.
  • Streamlined tasks: Automates complex build processes, improving work efficiency.
  • Improved portability: Using Makefiles allows consistent builds across different environments.

Header Files

Files used in C and C++ that contain declarations and definitions. Generally, the file extension is ".h".

Main roles:

Using header files improves code reusability and readability, making it easier to develop large-scale programs.

  • Function prototype declarations
    Describes the name, arguments, and return type of a function defined in another source file to call it.
  • Macro definitions
    Uses the `#define` preprocessor directive to define constants or short code snippets.
  • Structure and union definitions
    Defines composite data types that group multiple data types.
  • Type definitions
    Uses `typedef` to define new data types.
  • External variable declarations
    Declares global variables defined in other source files to reference them.

Structure

A composite data type that groups several variables of different data types into one. Using structures allows related data to be handled together and can represent complex data structures such as lists and trees. It is a widely used concept in C and C++. Other programming languages also have data types with similar functionalities. For example, Python classes and JavaScript objects can also group multiple data like structures.

Include Guards

An important mechanism to prevent multiple inclusions of header files and avoid compile errors.
This prevents a header file from being included multiple times in a single compilation unit, avoiding potential redefinition errors.
Proper use allows for the creation of safe and reliable programs.
In languages that read header files, such as C, C++, and Objective-C, include guards are used.


JSON (JavaScript Object Notation)

A text file with key-value pairs (a lightweight data interchange format).
It's written in plain text, making data exchange easy between different programming languages and systems.
It's widely used, especially in web applications, because it's human-readable and writable, and machines can easily parse and generate it.
It's used in various languages like JavaScript, Python, Java, PHP, and C#.

It's fundamentally similar to Python's dictionary type.
While JavaScript treats it as a type of object, it can be easily converted and used with Python's dictionary type (and vice versa).

When retrieving large amounts of data from databases in web applications, there's also a method called HTTP snippet, which retrieves only a portion.
However, an HTTP snippet is merely a series of text, and extracting necessary information may require complex processing like string manipulation or regular expressions.
On the other hand, data retrieval using JSON is a computer-friendly method that allows structured retrieval of large amounts of raw data from the server.
Basically, use JSON and avoid using HTTP snippets.

JSON Documentation

Ajax

A programming technique or method that uses HTTP (Hypertext Transfer Protocol), an existing internet communication mechanism, to perform asynchronous data communication between a web browser and a web server.


It's a general term for technologies that allow a web page to communicate asynchronously with a server to exchange data and update a part of the page without requiring a full page reload.
This means you can update parts of a website or perform some processing by communicating with the server without reloading the entire page, providing a more comfortable user experience.
For example, on a live sports score site, only the score might update in real time without the entire page reloading.

It's implemented using client-side scripting languages like JavaScript.


Programming Techniques

Programming techniques are systematized methods and approaches for creating computer programs efficiently and effectively.
They encompass not only writing code but also approaches throughout the entire software development lifecycle, including program design, development, testing, and maintenance.
Programming techniques are diverse, ranging from high-level abstract concepts to specific coding rules, existing at various levels.

MVC Model

One of the software design patterns within programming techniques.
MVC aims to streamline development and improve maintainability by dividing application logic into the following three elements:
・Model:
 Manages application data and business logic.
 Responsible for all data-related processes, such as saving, retrieving, updating, and deleting data.
・View:
 Handles the user interface.
 Displays data received from the model in a user-friendly format.
・Controller:
 Receives user input and controls the application, such as updating the model or changing the data displayed by the view.
 Acts as an intermediary between the model and the view.

Object-Oriented Programming (OOP)

Organizes code using objects and classes.

In object-oriented programming, data (attributes) and operations (methods) on that data are bundled together as "objects."

The blueprint for an object is a "class," and
the actual entity generated from a class is an "object" (or "instance").

It incorporates concepts such as inheritance, polymorphism, and encapsulation, allowing for the efficient development of more complex programs.

Differences Between Structs and Object-Oriented Programming

Structs and object-oriented programming are similar concepts and often confused, but they have the following differences.
Object-oriented programming is essentially an improved version of structs.

1. What they handle
 Structs: Data only
 Object-Oriented Programming: A combination of data and operations (methods)

2. Programming Paradigm
 Structs: Procedural programming
 Object-Oriented Programming: Object-oriented programming

3. Concept
 Structs: A collection of multiple data types
 Object-Oriented Programming: An instance (object) generated from a class, where the class is a blueprint that encapsulates data and methods.

Method Chaining (Function Composition)

One of the convenient techniques in object-oriented programming.
It allows methods of an object to be called sequentially, improving code readability.
It's a way to use functions continuously and in a chain.
It can be used in object-oriented programming languages like Python, Java, JavaScript, and C#.

# The example in Python result = " hello world ".strip().capitalize().replace("World", "Python") # Method chaining print(result) # Output: Hello Python

If method chaining becomes too long, it can make the code less readable, so use it in moderation.


Framework

A reusable foundational structure or skeleton designed to solve specific types of problems.
Frameworks provide common functionalities and design patterns in advance, allowing developers to efficiently proceed with development by adding their application-specific logic on top of these foundations.
They offer a wide range of various application features.

Main advantages of frameworks:
・Improved development efficiency: Common processes and structures are provided, eliminating the need to write everything from scratch and shortening development time.
・Enhanced code quality: Established design patterns and best practices are often incorporated, making it easier to write maintainable and extensible code.
・Easier team development: Development proceeds according to common rules and structures, preventing misunderstandings within the team and facilitating collaboration.
・Improved security: Many frameworks offer features to address common security vulnerabilities.
・Reduced learning curve: It becomes easier to acquire knowledge and skills specialized in specific types of development.

Microframework

Refers to frameworks that are particularly lightweight and offer limited functionality.
While large frameworks encapsulate many features (routing, ORM, templating engines, etc.),
microframeworks provide only the essential functions, giving developers more freedom of choice.


Library

In programming, a library refers to a collection of reusable code designed to perform specific functions.
Libraries are tools that help developers create programs efficiently.
By importing code that others have written and packaged as libraries into your own source code,
you can easily implement desired functionalities without writing them from scratch.
Libraries exist in various fields, such as image processing, data analysis, and network communication.

Handling Libraries in Object-Oriented Programming

In object-oriented programming (Java, Python, JavaScript, etc.), libraries are treated as objects.
Therefore, to use a library, in addition to importing the object,
you need to assign the library's object to a variable with the same name as the library (i.e., create an instance).
By doing so, you can call library methods (functions) using library_variable_name.method() to perform the desired operations.

On the other hand, in non-object-oriented programming languages like C,
when using a library, you can simply import the library and use the functions within it.
There is no concept of objects, and library functionalities are primarily provided as independent functions.


Memory Management

Memory management methods vary depending on the programming language.
In C and C++, programmers can free memory at any desired time.
This means memory management is required.
Java, Python, and C# have an automatic memory release feature called garbage collection, but humans cannot specify when it occurs.
Python, while having very simple memory management, is slow.
Programming always involves trade-offs.

Reference

Valgrind

A tool for checking and diagnosing memory issues in programs written in programming languages like C and C++.
It's like a health check tool for programs you've created.

When a program runs, it uses memory to temporarily store information.
For example, it might temporarily store calculation results or loaded files.

If a program doesn't properly manage/organize memory when using it, problems like the following can occur:
・Memory leak: When a program doesn't properly return memory it has finished using, leaving it allocated.
This is like continuously accumulating trash without cleaning your room.
Memory leaks can make programs increasingly slow, and in the worst case, cause them to crash.
・Invalid memory access: When a program tries to access a memory location it shouldn't.
This is like opening someone else's door and entering without permission.
Invalid memory access can cause programs to behave unexpectedly or crash.

Valgrind can be used to monitor how a program uses memory during execution and find problems like those described above:
・Where memory leaks are occurring
・Where invalid memory accesses are occurring
・Various other memory-related issues

Usage is very simple: just add valgrind before your normal execution command.

valgrind ./MyProgramName

Tips for Processing / Common Errors

・Implementing Exception Handling

Prevents programs from terminating abnormally when errors occur, improving stability.
It allows error handling code to be separated from regular processing code, improving code readability and maintainability.
This means you can avoid cluttering your code with invalid conditions, leading to cleaner code.

Common Exception Types

While the concept of exception handling is common across many programming languages, the types of exceptions and their syntax may vary slightly by language.

Exception Type Description
ZeroDivisionError Zero division error
Occurs when attempting to divide a number by zero.
TypeError Type error
Occurs when attempting an operation between incompatible data types or passing an argument of an inappropriate type to a function.
ValueError Value error
Occurs when an argument of the correct type is passed to a function, but the value itself is inappropriate.
IndexError Index error
Occurs when attempting to access a non-existent index in a sequence like a list or tuple.
KeyError Key error
Occurs when attempting to access a non-existent key in a dictionary.
FileNotFoundError File not found error
Occurs when attempting to open a file that does not exist.
NameError Name error
Occurs when attempting to use an undefined variable.
SyntaxError Syntax error
Occurs when the code's grammar is incorrect.
IOError Input/Output error
Occurs when an error happens during file reading or writing.
MemoryError Out of memory error
Occurs when a program attempts to allocate more memory than is available.
AttributeError Attribute error
Occurs when attempting to access a non-existent attribute.

・Floating-Point Calculations

Due to the way computers handle numbers, floating-point calculations can introduce errors in binary representation.
Always convert to integers for calculations, then convert back to decimals at the end.

・Pay attention to variable scope

There are two types of variables:
The scope differs depending on the variable type, affecting the range from which functions can access them.

(1) Global variables
Variables declared outside of any function, accessible from any function.
However, because they are accessible from anywhere, depending on the timing of access, they can exhibit unexpected behavior or cause errors, so caution is required when using them.
Avoid unnecessary global variable declarations.

(2) Local variables
Variables accessible only within the function where they are declared; they cannot be accessed from outside that function.

・Data Search

Consider the algorithm's processing time to improve system responsiveness.
For example, the processing times for common data search methods (algorithms), Linear Search and Binary Search, are as follows:
Linear Search: O(n)
Binary Search: O(log2n)

While Binary Search requires the dataset to be sorted in ascending/descending order beforehand, which might seem cumbersome when dealing with unsorted data,
as this graph shows, the larger the search data, the better it is to perform with Binary Search.

・Function Prototype Declaration

Mainly used in compiler-driven languages like C and C++, and unnecessary in interpreter-driven languages like Python.
A prototype declaration informs the compiler of a function's "type," including its name, parameter types, and return type, but does not contain the function's specific implementation (definition).

In compiler-driven languages, the compiler processes the source code from top to bottom, so it needs to be informed of the function's information (prototype) before the function is called.
This allows the compiler to check if the function call is correct (whether the argument types and count, and return type match).
Prototype declarations are especially important in large programs spanning multiple source files.

・System Termination

When you want to stop a program, for example, in an infinite loop, you can stop it with Ctrl + C.
This is provided by many operating systems and programming languages.

Mechanism of Ctrl + C:
Ctrl + C is a keyboard shortcut that sends an "interrupt signal" (usually SIGINT) to the operating system.
Upon receiving this signal, the program can interrupt its execution and perform termination processing.

The mechanism and implementation of signal handling vary by programming language:
・C/C++: You can use the signal function to catch the SIGINT signal and implement your own termination handling.
・Java: You can interrupt a thread using the Thread.interrupt() method.
 Also, you can register processes to be executed upon program termination using Runtime.getRuntime().addShutdownHook().
・JavaScript (Node.js): You can catch the SIGINT signal and write termination handling using process.on('SIGINT', ...).
・Python: A KeyboardInterrupt exception occurs, and by writing appropriate termination handling, you can safely stop the program.

・Exit Status

Exit status is a numerical value that a program or command transmits to the OS (Operating System) upon termination to indicate its result.
Using exit status, you can inform the OS or other programs whether it terminated successfully, or if an error occurred.
Additionally, if an error occurs, the exit status value can help identify the cause of the error.
Generally, an exit status of 0 means the program terminated successfully.
Any value other than 0 indicates that some error occurred, and different values are returned depending on the type of error.
The range of exit status values varies by system, but in many UNIX-like systems, integer values from 0 to 255 are used.

Getting the exit status:
Using the following command, mainly used in shell scripts of Unix-like operating systems (Linux, macOS, etc.), you can get the exit status (exit code) of the command that was executed immediately before.
In shell scripts, this exit status can be used for conditional branching and other operations.

echo $?

$? variable: A special variable in the shell that stores the exit status of the command executed immediately before.

Helpful Sites

#100DaysOfCode
You can participate and practice daily.

Stack Overflow
Like an online encyclopedia that contains answers to common programming questions.

⚠️ **GitHub.com Fallback** ⚠️