Compare commits
4 Commits
1ef53884cd
...
ad88fb5b36
Author | SHA1 | Date | |
---|---|---|---|
ad88fb5b36 | |||
b95d6bf352 | |||
ae50ff0d73 | |||
fd6f1af7a7 |
@ -74,7 +74,7 @@ mount --mkdir /dev/sdX1 /mnt/boot
|
||||
sudo pacman -Syyu # 更新源
|
||||
sudo pacman -S yay # 直接安装 Yay
|
||||
```
|
||||
+ man
|
||||
+ man man-pages
|
||||
+ gdisk
|
||||
+ bash-completion
|
||||
+ fcitx5-im fcitx5-rime fcitx5-chinese-addons # 输入法
|
||||
|
@ -75,10 +75,12 @@ extract() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 快速编辑bashrc并重载
|
||||
alias editrc='vim ~/.bashrc'
|
||||
# 快速重载
|
||||
alias reload='source ~/.bashrc'
|
||||
|
||||
# 快速查看电池信息
|
||||
alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0'
|
||||
|
||||
# 错误纠正
|
||||
shopt -s cdspell # 自动纠正cd命令的目录名拼写错误
|
||||
|
||||
|
@ -1,8 +1,18 @@
|
||||
# c风格字符串
|
||||
|
||||
`#include <cstring>`
|
||||
|
||||
```cpp
|
||||
---
|
||||
|
||||
## INDEX
|
||||
|
||||
- [字符串库函数](# std-string)
|
||||
- [内存操作标准库函数](# std-mem)
|
||||
- [与string容器转换](# to-string)
|
||||
|
||||
---
|
||||
|
||||
## **std-string**
|
||||
```c
|
||||
strcpy(buf1, "hello"); // 用后面的字符串覆盖前面的字符串
|
||||
strncpy(buf1, "hello", 5); // 增加指定长度
|
||||
|
||||
@ -16,15 +26,26 @@
|
||||
strstr(buf1, "hello"); // 返回第一次出现子串的指针
|
||||
|
||||
strtok(buf1, " "); // 分割字符串,返回分割过的字符串
|
||||
|
||||
memset(buf1, 0, sizeof(buf1));//覆写内存块
|
||||
|
||||
memcpy(buf1, "hello", 5); //复制内存块
|
||||
|
||||
memmove(buf1 + 1, buf1, 4); //移动5个字节到前一个位置
|
||||
```
|
||||
___注:未进行长度指定的函数有溢出风险 例如:strcpy___
|
||||
|
||||
---
|
||||
|
||||
## **std-mem**
|
||||
|
||||
- 内存操作函数与str系的最大不同是不检测EOF
|
||||
```c
|
||||
memset(buf1, 0, sizeof(buf1));//覆写内存块
|
||||
|
||||
memcpy(buf1, "hello", 5); //复制内存块,不处理内存重叠
|
||||
|
||||
memmove(buf1 + 1, buf1, 4); //移动5个字节到前一个位置,可处理重叠
|
||||
```
|
||||
- `int memcmp(const void *ptr1, const void *ptr2, size_t num);`
|
||||
- 比较函数,返回0表示相等
|
||||
|
||||
---
|
||||
|
||||
## 两种转换
|
||||
```cpp
|
||||
string str1;
|
||||
|
52
program/c_cpp/universal/dynamic-memory.md
Normal file
52
program/c_cpp/universal/dynamic-memory.md
Normal file
@ -0,0 +1,52 @@
|
||||
# 动态内存分配
|
||||
|
||||
---
|
||||
|
||||
## INDEX
|
||||
|
||||
- [C](# C)
|
||||
- [malloc](# malloc)
|
||||
- [calloc](# calloc)
|
||||
- [realloc](# realloc)
|
||||
- [free](#) : 传入动态分配的指针地址,记得释放完赋NULL
|
||||
- [CPP](# CPP)
|
||||
- [new](# new)
|
||||
- [delete](# delete)
|
||||
|
||||
---
|
||||
|
||||
## C
|
||||
|
||||
所有的函数声明于`<stdlib.h>`头文件中
|
||||
|
||||
---
|
||||
|
||||
### **malloc**
|
||||
|
||||
- 原型 `void *malloc(size_t size);`
|
||||
- 功能 : 用于分配指定字节数的内存块
|
||||
- 特点
|
||||
- 分配的内存是未初始化的,内容随机
|
||||
- 返回指向分配内存起始地址的void指针
|
||||
- 如果分配失败,返回NULL
|
||||
|
||||
---
|
||||
|
||||
### **calloc**
|
||||
|
||||
- 原型 `void *calloc(size_t num, size_t size);`
|
||||
- 特点
|
||||
- 比起 `malloc` 多了自动初始化
|
||||
|
||||
---
|
||||
|
||||
### **realloc**
|
||||
|
||||
- 原型 `void *realloc(void *ptr, size_t new_size);`
|
||||
- 功能 : 用于调整之前分配的内存块的大小
|
||||
- 特点
|
||||
- 可以扩大或缩小内存块
|
||||
- 新增部分不初始化
|
||||
- 如果ptr为NULL,等同于malloc
|
||||
- 可能返回新的指针地址(内存块可能被移动到新位置)
|
||||
- 如果`new_size`为0且ptr不为NULL,等同于free
|
@ -2,20 +2,43 @@
|
||||
|
||||
|
||||
# 目录
|
||||
- [c stdio](# c stdio)
|
||||
- [c fio](# c fio)
|
||||
- [cpp stdio](# cpp stdio)
|
||||
- [cpp fio](# cpp fio)
|
||||
- [关闭缓冲区读取](# ../linux/ncurses.c)
|
||||
|
||||
- [标准IO流](# IOstream)
|
||||
- [C](# C)
|
||||
- [格式化IO](# 格式化IO)
|
||||
- [字符IO](# charIO)
|
||||
- [Cstring](# Cstring)
|
||||
- [C二进制IO](# Cbin)
|
||||
- [C文件操作](# C-FILE)
|
||||
- [error](# c-error)
|
||||
- [cpp stdio](# cpp stdio)
|
||||
- [cpp fio](# cpp fio)
|
||||
- [关闭缓冲区读取](# ../linux/ncurses.c)
|
||||
- [ANSI](# ./ansi.md)
|
||||
|
||||
## c stdio
|
||||
---
|
||||
|
||||
## **IOstream**
|
||||
|
||||
+ stdin
|
||||
+ stdout
|
||||
+ stderr
|
||||
|
||||
---
|
||||
|
||||
## **C**
|
||||
|
||||
> 头文件
|
||||
`#include <stdio.h>`
|
||||
|
||||
> scanf 格式化输入函数
|
||||
printf 格式化输出函数
|
||||
---
|
||||
|
||||
### 格式化IO
|
||||
|
||||
- scanf 格式化输入函数
|
||||
- printf 格式化输出函数
|
||||
- fscanf 格式化输入函数(第一个参数是`FILE *stream`)
|
||||
- fprintf 格式化输出函数(第一个参数是`FILE *stream`)
|
||||
|
||||
__控制符__
|
||||
+ %d %nd %ld %x %o 整数
|
||||
+ %f %n.mf %lf 浮点数
|
||||
@ -23,45 +46,61 @@ __控制符__
|
||||
+ %s
|
||||
+ %p ptr
|
||||
|
||||
> 文件流
|
||||
+ stdin
|
||||
+ stdout
|
||||
+ stderr
|
||||
---
|
||||
|
||||
`int getchar(void);`
|
||||
`int putchar(int c);`
|
||||
### charIO
|
||||
|
||||
`int getc(FILE *stream);` getchar() 文件版
|
||||
`int putc(int c, FILE *stream);`
|
||||
+ IN
|
||||
|function prototype |remark |
|
||||
|--------------------------|-------------------------------------|
|
||||
|`int getchar(void)` |宏,标准IO |
|
||||
|`int getc(FILE *stream)` |宏,所有流 |
|
||||
|`int fgetc(FILE *stream)` |真正的函数 |
|
||||
|`int ungetc(int c,FILE *stream)`|将一个字符返回流中 |
|
||||
|
||||
`char *fgets(char *s, int size, FILE *stream);`
|
||||
+ 换行符后(包含换行符)
|
||||
+ 文件尾
|
||||
+ size-1
|
||||
`fputs(const char *s, FILE *stream);` **不添加换行符**
|
||||
+ OUT
|
||||
|function prototype |remark |
|
||||
|--------------------------|-------------------------------------|
|
||||
|`int putchar(int c)` |宏,标准IO |
|
||||
|`int putc(int c,FILE *stream)`|宏,所有流 |
|
||||
|`int fputc(int c,FILE *stream)`|真正的函数 |
|
||||
|
||||
---
|
||||
|
||||
### Cstring
|
||||
|
||||
|function prototype |remark |
|
||||
|---------------------------------------------------------|--------------------------------|
|
||||
|`char *fgets(char *s,int size,FILE *stream)` |到达行尾(包含换行符),EOF,size-1返回|
|
||||
|`int fputs(const char *restrict s, FILE *restrict stream)`|不在后添加换行符 |
|
||||
|
||||
---
|
||||
|
||||
### Cbin
|
||||
|
||||
`size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);`
|
||||
+ size 读写的块大小
|
||||
+ nmemb 读写的块数量
|
||||
`size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);`
|
||||
|
||||
### C-FILE
|
||||
|
||||
|
||||
## c fio
|
||||
|
||||
`FILE *fp` 文件指针
|
||||
|
||||
`FILE *fopen(const char *pathname, const char *mode);` 错误返回NULL
|
||||
`int fclose(FILE *stream);` 关闭文件 返回0为正常关闭
|
||||
- `FILE *fp` 文件指针
|
||||
- `FILE *fopen(const char *pathname, const char *mode);` 错误返回NULL
|
||||
- `int fclose(FILE *stream);` 关闭文件 返回0为正常关闭
|
||||
- `FILE *tmpfile(void)` 创建一个临时文件,返回一个'wb+'模式的文件指针
|
||||
- `char *tmpnam(char *name)` 创建一个临时文件名,最长`L_tmpnam`
|
||||
- `int remove(const char *pathname)` 删除一个文件
|
||||
- `int rename(const char *oldname, const char *newname)` 修改文件名字
|
||||
|
||||
**mode**
|
||||
+ "r" 读
|
||||
+ "w" 写,截0,新建
|
||||
+ "a" 写,尾+,新建
|
||||
+ "r+" 读写
|
||||
+ "w+" 读写,截0,新建
|
||||
+ "a+" 读写,仅尾+,新建
|
||||
+ "b" 搭配表二进制,仅win
|
||||
+ "r" 读
|
||||
+ "w" 写,截0,新建
|
||||
+ "a" 写,尾+,新建
|
||||
+ "r+" 读写
|
||||
+ "w+" 读写,截0,新建
|
||||
+ "a+" 读写,仅尾+,新建
|
||||
+ "b" 搭配表二进制,仅win
|
||||
|
||||
`long ftell(FILE *stream);` 获取当前文件指针位置
|
||||
`int fseek(FILE *stream, long offset, int whence);` 移动文件指针
|
||||
@ -73,15 +112,26 @@ __控制符__
|
||||
|
||||
`int fflush(FILE *stream);` 刷新缓冲区
|
||||
`int setvbuf(FILE *stream, char *buf, int mode, size_t size);` 设置缓冲区,return 0
|
||||
+ buf 设置的缓冲区
|
||||
+ mode
|
||||
+ buf 设置的缓冲区
|
||||
+ mode
|
||||
- `_IOFBF` 完全缓冲
|
||||
- `_IOLBF` 行缓冲
|
||||
- `_IONBF` 无缓冲
|
||||
+ size 指定缓冲区的大小,无缓冲时无效
|
||||
+ size 指定缓冲区的大小,无缓冲时无效
|
||||
|
||||
`int ferror(FILE *stream);` 流异常时return非0
|
||||
---
|
||||
|
||||
## c-error
|
||||
|
||||
- `void perror(const char *message);`
|
||||
- 打印message并在后面根据errno错误码生成错误信息
|
||||
- errno全局变量定义于`<errno.h>`,仅在发生错误时被设置
|
||||
|
||||
- `int ferror(FILE *stream);` 流异常时return非0
|
||||
- `int feof(FILE *stream)` 文件尾时返回真
|
||||
- `void clearerr(FILE *stream)` 重置错误位
|
||||
|
||||
---
|
||||
|
||||
## cpp stdio
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user