CS50x 2024 - Lecture 4 - Memory

00:00:00 - Introduction

00:01:01 - Pixel Art

将从更低的层面来了解计算机的工作原理

00:06:57 - Hexadecimal 16进制

在这里插入图片描述
表示255
使用两个十六进制数字来表示单个字节

00:14:23 - Memory

在这里插入图片描述

00:21:43 - Pointers

指针实际上只是一个地址,某个变量的地址

指针通常使用8个字节,而不仅仅是四个
在这里插入图片描述

#include <stdio.h>
int main() {
    int n = 50;
    int *p = &n;
    printf("%p\n",p);
    printf("%i\n",*p);
}

如果是8个可以数的更高

00:30:43 - Strings

在这里插入图片描述
string 实际上是char*
几十年前,没有决定创建一个名为string的实际数据类型,字符串实际上只是第一个字符的地址

#include <stdio.h>
int main() {
    char *s = "hi!";
    printf("%s\n",s);
}

在cs50的头文件中,有

typedef char *string;

以这种方式使用自定义数据类型

00:48:27 - Pointer Arithmetic

首先引入数组语法,是因为他是语法糖

00:52:05 - String Comparison

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main() {
    char *s = get_string("s:");
    char *t = get_string("t:");
    if(strcmp(s, t) == 0) {
        printf("same\n");
    }
    else {
        printf("different\n");
    }
}

01:04:52 - Copying

malloc采用一个参数,即希望为您查找的字节数,并返回该内存块的地址.

如果malloc返回null,则说明没有足够的可用内存

free与malloc相反,用完内存后需要还给操作系统

NULL只是一个地址,实际就是地址零来表示错误

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main() {
    char* s = get_string("s:");
    if (s == NULL) {
        return 1;
    }
    char* t = malloc(strlen(s) + 1);
    if (t == NULL) {
        return 1;
    }
    strcpy(t, s);
    // for (int i = 0, n = strlen(s); i <= n; i++) {
    //     t[i] = s[i];
    // }
    if (strlen(s) > 0) {
        t[0] = toupper(t[0]);
    }
    printf("%s\n",t);
    printf("%s\n", s);

    free(t);
    return 0;
}

01:16:49 - malloc and Valgrind

valgrind:检查内存的使用情况

01:24:11 - Garbage Values

01:29:10 - Pointer Fun with Blinky

01:32:00 - Swapping

#include <stdio.h>

void swap(int* a, int* b);
int main() {
    int a = 2;
    int b = 3;
    printf("before: a = %i, b =%i\n", a, b);
    swap(&a, &b);
    printf("after: a = %i, b = %i\n", a, b);
    return 0;
}

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

在这里插入图片描述
如果按值传递不能成功,要通过引用传递(passing by reference)

01:46:27 - Overflow

01:49:36 - scanf

segmentation fault 分段错误:意味着和内存相关的问题出现了,这些内存段不属于你,没有为其分配空间,例如通过数组甚至通过malloc

02:02:11 - File I/O

在这里插入图片描述

csv文件,是一个轻量级的电子表格

每次函数返回指针时,应该检查它是否为空,如果是,根据文档应该退出

#include <cs50.h>
#include <stdio.h>

int main() {
    FILE* file = fopen("phonebook.csv","a");
    if (file == NULL) {
        return 1;
    }
    char* name = get_string("Name: ");
    char* number = get_string("Number: ");
    fprintf(file, "%s,%s\n", name, number);
    fclose(file);
}

```c
#include <stdio.h>
#include <stdint.h>

typedef uint8_t BYTE;
int main(int argc, char* argv[]) {
    FILE* src = fopen(argv[1], "rb");
    FILE* dst = fopen(argv[2], "wb");

    BYTE b;
    while (fread(&b,sizeof(b), 1, src) != 0) {
        fwrite(&b, sizeof(b), 1, dst);
    }
    fclose (dst);
    fclose(src);
}


  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值