CS50_week4 Memory

2024-09-23 09:26:43 浏览数 (1)

代码语言:c复制
int n = 50;

pointer 变量占 8bytes

代码语言:c复制
string s = "HI!";

代码语言:c复制
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    string s = "HI!";
    printf("%pn", s);
    printf("%pn", &s[0]);
}

s是string "HI!"的指针;那么s[0], s[1], s[2]...组成的array s也是指针?

代码语言:c复制
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    string s = "HI!";
    printf("%pn", s);
    printf("%pn", &s[0]);
    printf("%pn", &s[1]);
    printf("%pn", &s[2]);
    printf("%pn", &s[3]);

}

每个元素s[i]只占一个byte每个元素s[i]只占一个byte
代码语言:c复制
string s = "HI!";
代码语言:c复制
 char *s = "HI!";

string s ↔ char *s 等效

代码语言:c复制
#include <stdio.h>

int main(void)
{
    char *s = "HI!";
    printf("%sn", s);

}

代码语言:c复制
typedef uint8_t BYTE;
代码语言:c复制
typedef char *string;

pointer arithmetic

代码语言:c复制
#include <stdio.h>

int main(void)
{
    char *s = "HI!";
    printf("%c", *s);
    printf("%c", *(s   1));
    printf("%cn", *(s   2));

}

s[1] ↔ *(s 1) compiler translate it as

代码语言:c复制
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    string s = get_string("s: ");
    string t = get_string("t: ");

    if (s == t)
    {
        printf("Samen");
    }
    else
    {
        printf("Differentn");
    }
}

代码语言:c复制
char *s = get_string("s: ");
char *t = get_string("t: ");

代码语言:c复制
string s = get_string("s: ");
string t = s;

t[0] = toupper(t[0]);

printf("%sn", s);
printf("%sn", t);

代码语言:c复制
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    char *s = get_string("s: ");
    char *t = malloc(strlen(s)   1);

    for (int i = 0, n = strlen(s); i <= n; i  )
    {
        t[i] = s[i];
    }

    if (strlen(t) > 0)
    {
        t[0] = toupper(t[0]);
    }

    printf("%sn", s);
    printf("%sn", t);
}

代码语言:c复制
char *s = get_string("s: ");
char *t = malloc(strlen(s)   1);//memory allocate

if (t == NULL)//not enough memory available
{
    return 1;
}
代码语言:c复制
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    char *s = get_string("s: ");
    if (s == NULL)
    {
        return 1;
    }

    char *t = malloc(strlen(s)   1);//memory allocate

    if (t == NULL)//not enough memory available
    {
        return 1;
    }

    strcpy(t, s);

    if (strlen(t) > 0)
    {
        t[0] = toupper(t[0]);
    }

    printf("%sn", s);
    printf("%sn", t);

    free(t);//opposite of malloc,释放memory
}

NULL is the adrress zero.

valgrind

代码语言:c复制
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int *x = malloc(3 * sizeof(int));
    x[0] = 72;
    x[1] = 73;
    x[3] = 33;
    free(x);
}

代码语言:c复制
int main(void)
{   
    int *x;  
    int *y; 

    x = malloc(sizeof(int));                    

    *x = 42;
    *y = 13;    

    y = x;        

    *y = 13;   
}

y没有assign a value, so y maybe 0, or 1000, or etc.

*y就到了某个不知道的memory的地方。

passing by reference

代码语言:c复制
#include <stdio.h>

int main(void)
{
    char s[4];
    printf("s: ");
    scanf("%s", s);
    printf("s: %sn", s);
}

File I/O

代码语言:c复制
#include <cs50.h>
#include <stdio.h>
#include <string.h>

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

    fprintf(file, "%s,%sn", 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);
}
代码语言:c复制
#include <stdio.h>

int main() {
    int num = 1;
    printf("dn", num); // 使用d格式化输出,表示至少打印3位,不足的前面补零
    return 0;
}

0 人点赞