Skip to content

Instantly share code, notes, and snippets.

View itczl22's full-sized avatar
👏
listening

itczl22

👏
listening
View GitHub Profile
@itczl22
itczl22 / go-cmd-install.sh
Created July 4, 2016 05:09
Install Go and common cmd tools.
#!/bin/bash
TMP=`pwd`/go-cmd-`date +%s`
GOX=src/golang.org/x
mkdir -p $TMP/$GOX
cd $TMP/$GOX
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/net.git
@itczl22
itczl22 / big-little-endian.c
Last active October 30, 2016 08:11
大端小端存储
#include <stdio.h>
int main(void) {
// 储存方式的判断
unsigned int a = 0x12345678;
const char* buf = (char*)&a;
printf("%x\n%x\n", buf[0], buf[3]);
// 78 12, 所以是小端存储
const char* str = "12345678";
@itczl22
itczl22 / align-fill_out.c
Last active October 30, 2016 08:10
地址的对齐补齐
#include<stdio.h>
int main() {
char ch = 8; // 1 + 3
int it = 9; // 4 + 4
int* ptr = &it; // 8
printf("%p\n%p\n%p\n\n", &ch, &it, &ptr);
typedef struct {
short sit; // 2 + 6
@itczl22
itczl22 / uninitialized-local-variable.c
Last active November 5, 2016 07:52
未初始化的局部变量
#include<stdio.h>
void foo(void) {
int tmp;
printf("%d\n", tmp);
tmp = 999;
}
int main() {
foo(); // 0
@itczl22
itczl22 / pointer-array.c
Last active October 30, 2016 08:12
指针与数组
/*
* 指针的标准定义方法:
* type *ptr;
* int val; int *ptr = &val;
* int* val; int* *ptr = &val;
* 所以不论几级指针都一样, 指针就是*ptr, 一级指针type是int, 二级指针type是int*, 以此类推.
*
* 同理指向数组的指针:
* type *ptr;
* int arr[10]; int *ptr = arr;
@itczl22
itczl22 / variable-argument.c
Created October 30, 2016 08:08
c语言变长参数
/*
* 通过3个宏和一个类型实现:
* va_start va_arg va_end va_list
* 实现:
* 把参数在栈中的地址记录到va_list中, 通过一个确定参数first_num确定地址然后逐个读取值
* 通常也会在对第一个参数进行一些特殊处理以方便函数的实现
* 比如强制指定为参数个数、指定结束参数或者像printf一样使用格式占位符来
* 参数是如何获取的, 为什么要指定第一个参数:
* 函数的调用的参数会进行压栈处理, 而对参数的压栈是从右到左进行压栈
* 因此栈顶就是第一个有名参数, 而参数和参数之间存放是连续的
@itczl22
itczl22 / interview.txt
Last active November 12, 2016 09:29
c++面试小题
c++ 缺省参数注意事项:
1.如果一个参数带有缺省值,那么它后边的所有参数必须都带有缺省值
2.如果一个函数声明和定义分开,那么缺省参数只能放在声明中。因为编译器编译时看的是函数声明
3.缺省参数避免和重载发生歧义
c++ 中 ++i 和 i++的实现:
int operator++(); // 前++
int operator++(int); // 后++
用到哑元
@itczl22
itczl22 / stack-growth-direction.c
Last active November 5, 2016 07:51
判断栈的增长方向
/*
* 判断栈的增长方向
*
* 系统在调用函数时, 这个函数的相关信息都会出现栈之中, 比如参数、返回地址和局部变量
* 入栈顺序和函数内的变量申明顺序可能不一样, 这和编译器有一定的关系
* 所以不能通过一个函数内的多个变量在栈中的地址来判断栈的增长方向
*
* 当它调用另一个函数时, 在它栈信息保持不变的情况下会把它调用那个函数的信息放到栈中
* 两个函数的相关信息位置是固定的, 肯定是先调用的函数其信息先入栈, 后调用的函数其信息后入栈
* 这样通过函数栈来确定栈的增长顺序
@itczl22
itczl22 / placement_new.cpp
Created November 5, 2016 07:44
定位new
/*
* 定位new
*/
#include <iostream>
using namespace std;
char g_addr[100];
int main() {
// 把内存分配到全局区
  • 类里边 类成员变量只能声明不能定义,由构造函数对其定义
  • 如果类中含有常量(const属性不可改值)或引用型(定义时就得给值)的成员变量,必须通过初始化表对其初始化
  • 在类的声明中给定构造函数的缺省值,类的实现里边不能给定缺省值