前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CS50_week4 Memory

CS50_week4 Memory

原创
作者头像
greatak
修改2024-09-23 09:26:43
920
修改2024-09-23 09:26:43
举报
文章被收录于专栏:cs50
代码语言:c
复制
int n = 50;

pointer 变量占 8bytes

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

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

int main(void)
{
    string s = "HI!";
    printf("%p\n", s);
    printf("%p\n", &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("%p\n", s);
    printf("%p\n", &s[0]);
    printf("%p\n", &s[1]);
    printf("%p\n", &s[2]);
    printf("%p\n", &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("%s\n", 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("%c\n", *(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("Same\n");
    }
    else
    {
        printf("Different\n");
    }
}

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

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pointer arithmetic
  • valgrind
  • passing by reference
  • File I/O
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档