0% found this document useful (0 votes)
24 views

C C++

When declaring a pointer, it is good practice to initialize it to NULL if it does not point to a specific memory address. Otherwise, the pointer may point to random memory and cause unexpected issues. NULL initializes the pointer to address 0.

Uploaded by

Ân Võ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C C++

When declaring a pointer, it is good practice to initialize it to NULL if it does not point to a specific memory address. Otherwise, the pointer may point to random memory and cause unexpected issues. NULL initializes the pointer to address 0.

Uploaded by

Ân Võ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NULL Pointer:

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.

1. Memory partition on RAM


Text :
Access rights are only Read and it does not contain instructions to execute, so avoid
modifying instructions.

Contains declaration of constants in the program (.rodata)

Data:
Access is read-write.

Contains global or static variables with a non-zero initial value.

Released at the end of the program.

Bss:
Access is read-write.

Contains global variables or static variables with an initial value of zero or no initialization.

Released at the end of the program.

Stack:
Access is read-write.

Used to allocate local variables, input parameters of functions,...

Will be released when exiting the code/function block

Heap:
Access is read-write.

Used to allocate dynamic memory such as: Malloc, Calloc, ...

Will be freed when calling the free function,...

2. Stack and Heap?


• Heap memory and Stack memory are essentially the same memory area created and
stored in RAM when the program is executed.
Stack memory is used to store local variables in functions and parameters passed in. Access
to this memory is very fast and is executed when the program is compiled.

Heap memory is used to store memory for pointer variables dynamically allocated by the
malloc - calloc - realloc functions (in C).

Memory area size

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.

Memory area characteristics

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.

Note: automatic memory cleanup depends on the intermediate compiler.

The error problem occurs in the memory area

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.

Scope is global i.e. they can be used anywhere in the program.

Default value is unpredictable (garbage).

Default value is Zero (0).

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:

Pre-processor stage (Pre-processor)

Phase of translating NNBC to Asembly (Compiler)

Asembly translation phase into machine language (Asember)

Linker phase (Linker)

Compiler process
1. Preprocessor stage - Preprocessor

This phase will perform:

Get source code

Delete all annotations and comments of the program

Preprocessor directives (starting with #) are also processed

File inclusion directive (#include).

Definition directive for name (#define macro).

Conditional compilation directives (#if, #else, #elif, #endif, …).


For example, the #include directive allows the program code of a header file to be added to
the source code to be translated. Constants defined with #define will be replaced with
specific values at each use in the program.

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).

2. Add the High Level Language translation to Assembly

Syntax analysis of NNBC source code

Convert them to Assembly code, which is a low-level language (assembly language) close to
the microprocessor's instruction set.

3. Assembly translation stage

Translate program => Convert machine code 0 and 1

A machine code file (.obj) is then generated in the system

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).

You might also like