Basi c Notes (Debt Payment)

Posted by nextman on Wed, 08 Dec 2021 00:41:42 +0100

c Language Basics Record

1. Data Types

Data type: Essentially an alias for a fixed-size memory block
Variable: An alias that is essentially a continuous memory space

int a; //Tell the compiler to allocate 4 bytes of space
int b[10]; //40 bytes
printf("b:%d, b+1:%d, &b:%d, &b+1:%d\n", b, b+1, &b, &b+1);
// B As with &b, b+1 is the address of b+4, &b+1 is the address of b+40 
// b represents the address of the first element of the array 
// &b represents the address data type of the entire array: an array with 10 int elements

sizeof: calculates the size of the data type in memory space and returns unsigned int
typedef: alias existing data types, customize types

typedef long long UU_LONG; //Used across platforms

2. Memory Model

1.A few districts


Stack area: Automatically requests automatic release, mainly local variables (function parameter parameters), arrays (char str[]= "hello world")

Heap area: Manual application, manual release

(1) c language: malloc/free malloc may not be allocated successfully, returning void *

Note: free frees up the memory space pointed to by the ret pointer, but the ret pointer itself does not change

 free(ret);
 ret=NULL;


Illustration:

(2) c++:new/delete (operator) new must be assigned successfully

Global Area:

int a = 100; //Global Area Default Same as
extern int a = 100; //Global External Links
static int b = 100; //Static global area internal links can only be used in the current file

2. Direction

(1) The direction in which the stack grows: depending on the direction of the opening
(2) direction of memory growth
Large End Mode: Low Bit Bytes - > High Bit Bytes (right below)
Small End Mode: Low Bit Bytes - > High Bit Bytes (left of figure below)

3. Pointer pointer variable

1. Concepts

  • Pointer is a data type used to store memory addresses
  • Pointer variables take up 4 bytes regardless of the level (* ** ***) and the type of data they point to
  • Meaning of Pointer: Indirect Assignment

2. Field Pointer

Field pointer: A pointer to an object that has been deleted or to a memory area where access has not been requested
Consequence: Memory leak
Specific circumstances:
(1) Pointer variable is not initialized

// Wrong practices
int* p;
printf("%d",*p);

So as soon as you get the pointer variable, you just leave it empty

(2) The memory pointed to by the pointer is freed, but the pointer is not empty
free(p) only releases the memory pointed to by p, but does nothing to p

// Correct practices
free(p);
p=NULL;

(3) Cross-border access

// error
char str[3] = "abc";

There is one\0 at the end of the string

3. Pointer step

Pointer step: Pointer + 1 (how many bytes added), depending on the type of data the pointer points to

char *p = NULL;
printf("%d\n",p); //0
printf("%d\n",p+1); //1

int *q = NULL;
printf("%d\n",q); //0
printf("%d\n",q+1); //4

char buf[1024] = { 0 }; //40 bytes
printf("%d\n",buf); //Array name= address of buf first element= address of buf array
printf("%d\n",&buf); //Address of buf
printf("%d\n",buf+1); //buf's first address + 1 (with an element char added)
printf("%d\n",&buf+1); //First address of buf + 1024 (add 1 buf=1024 char s)

Several common library functions:
Memcpy: void *memcpy(void *str1, const void *str2, size_t n) Copy n bytes from storage STR2 to storage STR1
Memset:void *memset(void *str, int c, size_t n) Copies the character c (an unsigned character) to the first n characters of the string pointed to by the parameter str

4. Characteristics of pointers

(1) Input characteristics

void p_intArray(int* arr /*in*/, int len /*len*/);

(2) Output characteristics

Allocate space

void allocateSpace(char** p /*out*/); //Output a allocated space from a function using a pointer as output

4. Reference (Alias of Variable) [c++]

int a;
int *p=&a;//The pointer has its own pointer name and address, and stores the address to the object (you can change the address to another object)
int &b=a;//Reference is an alias (which cannot be changed once declared) Reference b and reference a use the same address: two variable names in the symbol table point to the same address
(int &b=a and int* const pp=&a;You cannot change the object you are pointing to, but you can change the value you are pointing to)
//The difference is that the address of B is the address of a, &b == &a
//pp has its own address pp stores the address of a pp == &a
Symbol table holds:	Variable Name	Variable Address     [Symbol table will not change after generation]
int a			a		a Address
 Pointer p: 			Pointer Name	Pointer Address    And the pointer is pointing to a Address (stored data) can be changed		[Insecure)
Quote b: 			Reference Name b	Reference Object a Address→→→Therefore, the reference object cannot be changed		[Security)

1. Array References


Define array type before creating reference

typedef int ARR_TYPE[3]; //ARR_TYPE[3] is an alias for an int array with three elements
ARR_TYPE[3]& arrRef = arr; //ArRef is a reference to arr

Or alias the array directly:

int(&rf)[3] = arr

2. References in functions

References in functions: parameters of functions, return values of functions
(1) Used as a parameter in a function
Passing a reference must exist and be valid (secure but not pointer-fast, an address always passes faster than complex data). (Used)
Passing a pointer pointer is not necessarily valid [you need to decide if the passed pointer is not empty]

Topics: C network Back-end