C C++
C C++
When you declare a pointer, if you don't assign a specific memory address to it, it's a good
practice to declare the pointer as NULL (address = 0, value = 0).
If you don't initialize a pointer with a specific address, it will point to a random memory
address, which can lead to unexpected issues.
Note: It's always recommended to initialize a pointer as NULL if it doesn't point to any
specific address.
Data:
Access is read-write.
Bss:
Access is read-write.
Contains global variables or static variables with an initial value of zero or no initialization.
Stack:
Access is read-write.
Heap:
Access is read-write.
Heap memory is used to store memory for pointer variables dynamically allocated by the
malloc - calloc - realloc functions (in C).
Stack: the size of Stack memory is fixed, depending on the operating system, for example,
Windows operating system is 1 MB, Linux operating system is 8 MB (note that the number
may vary depending on architecture). your operating system architecture).
Heap: the size of Heap memory is not fixed, it can be increased or decreased to meet the
data storage needs of the program.
Stack: Stack memory area is managed by the operating system, data stored in Stack will
automatically be destroyed when the function finishes its job.
Heap: The Heap memory area is managed by the programmer (in C or C++), the data in the
Heap will not be destroyed when the function is completed, which means you have to
manually destroy the memory area with the free statement (in C), and delete or delete [] (in
C++), otherwise a memory leak will occur.
Stack: because the Stack memory is fixed, if your program uses too much memory beyond
the Stack's storage capacity, Stack overflow will definitely occur, cases such as you initialize
too many local variables, infinitely recursive functions,...
Those variables which are defined within some function and are
accessible to that function only are called Local Variables.
Those variables which are defined outside of function block and are
accessible to entire program are known as Global Variables.
Scope is local to that block or function where they are defined.
The translation process is the process of converting from a high-level language (NNBC)
(C/C++, Pascal, Java, C#...) to a target language (machine language) so that the computer
can understand and execute. The C programming language is a compiled language. A
program written in C that wants to run on a computer must go through a compilation process
to convert from source code to executable code. The process is divided into 4 main stages:
Compiler process
1. Preprocessor stage - Preprocessor
Note
The difference between #include and #include “filename” lies in the preprocessor header file
search before the compilation process.
#include : the pre-processor will only search for the header file (.h) in the directory containing
the header file of the C language library (usually the directory in the IDE installer).
#include “filename”: First, the pre-processor searches for the header file (.h) in the directory
where the C/C++ project is located. If not found, the preprocessor searches for the header
file (.h) in the directory containing the header file of the C language library (usually the
directory in the IDE installer).
Convert them to Assembly code, which is a low-level language (assembly language) close to
the microprocessor's instruction set.
4. Linker phase
In this phase, the machine code of a program compiled from multiple sources (.c files or .lib
library files) are linked together to form a single target program. The machine code of the
library functions called in the program are also included in the final program during this
period.
Therefore, errors related to calling functions or using global variables that do not exist will be
detected. Even the error of writing the main program without the main() function was
detected in the link.
At the end of the process, all objects are linked together into a unified executable program
(executable or .exe).