【linux C】基础

简介

        汇总linux C 基础知识点。来自B站李慧芹老师课程笔记。

浮点型失真问题

int func(float f){
    if(f < 0){
        return -1;
    }else if(fabs(f - 0) <= 1e-6){
        return 0;
    }else{
        return 1;
    }
}

位运算

        | 按位或; & 按位与; ^ 按位异或; ~ 按位取反

        将操作数中 第n位置1,其他位不变:num = num | 1 << n;

        将操作数中 第n位置0,其他位不变:num = num & ~(1 << n);

        测试第n位:if(num & (1 << n));

指针

        64位环境 指针类型占用8个字节 32位环境 指针类型占用4个字节

指针与指针变量

int i = 1;
int *p = &i;
int **q = &p;

   指针与一维数组

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int a[3] = {1, 2, 3};
    int *p = a;
    
    for(int i = 0; i < sizeof(a)/sizeof(*a); i++){
        printf("%p -> %d\n", p+i, *(p+i));
    }
}

函数与数组

#include <stdio.h>
#include <stdlib.h>

//注意这里的int *arr 的大小是8个字节,是一个普通的指针,不是数组,所以要传大小
void printarr(int *arr, int size){
    for(int i = 0; i < size; i++){
        printf("%d ", *(arr + i));
    }
    printf("\n");
}

int main(){
    int arr[] = {1, 2, 3, 4, 5};
    printarr(arr, sizeof(arr)/sizeof(*arr));
}

  函数指针

        函数指针:类型 (*指针名)(形参)

        函数指针数组: 类型(*数组名[下标])(形参)

        指向指针函数的函数指针数组: int *(*funcp[N])(int)

int pthread_create(pthread_t *restrict thread, 
                    const pthread_attr_t *restrict attr, 
                    void *(start_routine)(void *),
                    void *restrict arg);

构造类型 

结构体

struct day {
    int H;
    int M;
    int S;
};

struct student_st{
    char *name;
    struct day day;
};

// 可以使用下面的方法取消对齐,常用于网络通信
struct B {
    int i;
    char c;
    float f;
}__attribute__((packed));

共同体

N选一, 多个成员共用一块空间,取最大的成员的类型大小作为共同体的类型大小

union test_un{
    int i;
    float f;
    double d;
    char ch;  
};

//应用
//32位的无符号数的高16位和低16位相加
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

int main(){
    uin32_t i = 0x11223344;
    printf("%x\n", (i >> 16) + (i & 0xFFFF));
}

---------------------------------------------
//另一种写法
---------------------------------------------
union{
    struct{
        uint16_t i;
        uint16_t j;
    }x;
    uint32_t y;
};

int main(){
    uin32_t i = 0x11223344;
    printf("%x\n", (i >> 16) + (i & 0xFFFF));

    u.y = 0x11223344;
    printf("%x\n", u.x.i + u.x.j);
}

动态内存管理

malloc

calloc

realloc

free 谁申请谁释放

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

void func1(void *p, size_t size) {

  if(p == NULL) {
    return;
  }
  p = malloc(size);
}

void func2(int **p, size_t size) {

  if(*p == NULL) {
    return;
  }
  *p = malloc(size);
}

void *func3(void *p, size_t size) {
  if(p == NULL) {
    return NULL;
  }
  p = malloc(size);
  return p;
}

int main() {
  int num = 100;
  int *p = NULL;

  func1(p, num); // 内存会泄露

  func2(&p, num); // 传递二级指针

  p = func3(p, num); // 将申请的内存返回

  free(p);
  exit(0);
}

free的理解

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

void func2(int **p, size_t size) {

  if(*p == NULL) {
    return;
  }
  *p = malloc(size);
}

int main() {
  int num = 100;
  int *p = NULL;

  func2(&p, num); // 传递二级指针

  free(p);
  // p = NULL;
  
  *p = 123;
  printf("%d\n", *p); // 这个指针已经是野指针了 
  
  exit(0);
}
  • free代表着变量p不再拥有原来指向内存空间的引用权限
  • free后最好马上将指针置NULL

Makefile

工程管理 依赖管理

  • makefile(用户自定义 更高优先级)
  • Makefile(默认)
    OBJS=main.o tool1.o tool2.o
    CC=gcc
    
    mytool:$(OBJS)
      $(CC) $(OBJS) -o mytool
    
    main.o:main.c
      $(CC) main.c -c -Wall -g -o main.o
    tool1.o:tool1.c
      $(CC) tool1.c -c -Wall -g -o tool1.o
    tool2.o:tool2.c
      $(CC) tool2.c -c -Wall -g -o tool2.o
     
    clean:
      $(RM) $(OBJS) mytool -r
  • $^ 表示在上一句依赖关系中被依赖的所有文件

  • $@ 表示在上一句依赖关系中依赖项的目标文件

    CFLAGS=-Wall -g -c
    OBJS=main.o tool1.o tool2.o
    CC=gcc
    
    mytool:$(OBJS)
      $(CC) $^ -o $@
    
    main.o:main.c
      $(CC) $^ $(CFLAGS) -o $@
    tool1.o:tool1.c
      $(CC) $^ $(CFLAGS) -o $@
    tool2.o:tool2.c
      $(CC) $^ $(CFLAGS) -o $@
     
    clean:
      $(RM) $(OBJS) mytool -r
    CFLAGS=-Wall -g -c
    OBJS=main.o tool1.o tool2.o
    CC=gcc
    
    mytool:$(OBJS)
      $(CC) $^ -o $@
    
    %.o:%.c
      $(CC) $^ $(CFLAGS) -o $@
     
    clean:
      $(RM) $(OBJS) mytool -r

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值