C語言

1.定義一個宏#define swap(x,y)交換x,y的值

#include "stdio.h"
#define swap(x,y) {\
y = x + y ;\
x = y - x ;\
y = y - x ;\
}
int main()
{
        int a = 10;
        int b = 20;
        swap(a,b);
        printf("a:%d,b:%d\n",a,b);
        return 0;
}

2.堆棧溢出的原因:
 

1.函數調用太深(函數調用時會先入棧,也就是保護現場的產生的變量)(棧溢出)
2.動態申請的空間使用後沒有釋放(堆溢出)
3.數組訪問越界
4.指針非法訪問
5.局部數組過大

3.堆棧的釋放

1.堆:由malloc分配,由free釋放,也就是說程序員自己申請的空間自己釋放
2.棧:由編譯器釋放

4.ifndef/define/endif 的作用

防止該頭文件被重複引用

5.

void test1()
{
  char string[10];
  char* str1 = "0123456789";
  strcpy(string, str1);
}
編譯器報錯,原因:str1實際佔11個字節,數組越界

6.

void test1()
{
        int *p = NULL;
        char *p1 = NULL;
        char string[10];
        char *str1 = "01234567890";
        strcpy(string, str1);
        printf("int *:%ld,char *:%ld\n",sizeof(p),sizeof(p1));
        printf("string size:%ld,str1 size:%ld\n",sizeof(string),sizeof(str1));
        printf("string:%s\n",string);
}
int main()
{
        int a = 10;
        int b = 20;
        swap(a,b);
        printf("a:%d,b:%d\n",a,b);
        test1();//error
        return 0;
}

輸出結果:
a:20,b:10
int *:8,char *:8
string size:10,str1 size:8

7.set & clear

set:|=
clear:&=~

8.內存對齊

https://blog.csdn.net/sssssuuuuu666/article/details/75175108

9.進程間通信的幾種方式

無名管道、有名管道、消息隊列、信號、信號量、共享內存

10.大端小端

大端:高字節低地址

小端:高字節高地址

11.

暫時記錄

測試程序:

#include "stdio.h"
#include "string.h"
#define swap(x,y) \
y = x + y ;\
x = y - x ;\
y = y - x ;

struct s1
{
  int i;
  int j;
  int a;
  double b;
};

struct s2
{
  int i;
  int j;
  double b;
  int a;
};
void test2()
{
	int a[5]={1,2,3,4,5};
	int *ptr=(int *)(&a+1);
	printf("%d,%d\n",*(a+1),*(ptr-1));
}
void test1()
{
	int *p = NULL;
	char *p1 = NULL;
	char string[10];
	char *str1 = "01234567890";
	strcpy(string, str1);
	printf("int size:%ld\n",sizeof(int));
	printf("int *:%ld,char *:%ld\n",sizeof(p),sizeof(p1));
	printf("string size:%ld,str1 size:%ld\n",sizeof(string),sizeof(str1));
	printf("string:%s\n",string);
}
void test4()
{
	int i=0;
	switch(i){
		case 1:i=2;
		case 3:i=4;
		case 5:i=6;
		default:i=8;
	}
	printf("i = %d\n",i);
}
void test5()
{
	char c = 130;
	char c2 = 8;
	printf("(int)c = %d,c2 = %d\n",(int)c,(int)c2);//c = -126 原碼 = 反碼 + 1;-130 = 1000 0010 反:1 111 1101 + 1 = 1 111 1110(-126)
}
int main()
{
	int a = 10;
	int b = 20;
	struct s1 s_1;
	struct s2 s_2;
	swap(a,b);
	printf("a:%d,b:%d\n",a,b);
	printf("test1\n");
	test1();//error
	printf("test2\n");
	test2();
	printf("test3\n");
	printf("sizeof(s1)= %ld\n",sizeof(s_1));
	printf("sizeof(s2)= %ld\n",sizeof(s_2));
	printf("test4\n");
	test4();
	printf("test5\n");
	test5();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章