原文:Exercise 16: Structs And Pointers To Them
在这个练习中你将会学到如何创建struct,将一个指针指向它们,以及使用它们来理解内存的内部结构。我也会借助上一节课中的指针知识,并且让你使用malloc从原始内存中构造这些结构体。
struct
malloc
像往常一样,下面是我们将要讨论的程序,你应该把它打下来并且使它正常工作:
#include <stdio.h> #include <assert.h> #include <stdlib.h> #include <string.h> struct Person { char *name; int age; int height; int weight; }; struct Person *Person_create(char *name, int age, int height, int weight) { struct Person *who = malloc(sizeof(struct Person)); assert(who != NULL); who->name = strdup(name); who->age = age; who->height = height; who->weight = weight; return who; } void Person_destroy(struct Person *who) { assert(who != NULL); free(who->name); free(who); } void Person_print(struct Person *who) { printf("Name: %s\n", who->name); printf("\tAge: %d\n", who->age); printf("\tHeight: %d\n", who->height); printf("\tWeight: %d\n", who->weight); } int main(int argc, char *argv[]) { // make two people structures struct Person *joe = Person_create( "Joe Alex", 32, 64, 140); struct Person *frank = Person_create( "Frank Blank", 20, 72, 180); // print them out and where they are in memory printf("Joe is at memory location %p:\n", joe); Person_print(joe); printf("Frank is at memory location %p:\n", frank); Person_print(frank); // make everyone age 20 years and print them again joe->age += 20; joe->height -= 2; joe->weight += 40; Person_print(joe); frank->age += 20; frank->weight += 20; Person_print(frank); // destroy them both so we clean up Person_destroy(joe); Person_destroy(frank); return 0; }
我打算使用一种和之前不一样的方法来描述这段程序。我并不会对程序做逐行的拆分,而是由你自己写出来。我会基于程序所包含的部分来给你提示,你的任务就是写出每行是干什么的。
包含(include)
include
我包含了一些新的头文件,来访问一些新的函数。每个头文件都提供了什么东西?
struct Person
这就是我创建结构体的地方了,结构体含有四个成员来描述一个人。最后我们得到了一个复合类型,让我们通过一个名字来整体引用这些成员,或它们的每一个。这就像数据库表中的一行或者OOP语言中的一个类那样。
Pearson_create 函数
Pearson_create
我需要一个方法来创建这些结构体,于是我定义了一个函数来实现。下面是这个函数做的几件重要的事情:
sizeof(struct Person)
assert
NULL
x->y
strdup
name
译者注:x->y是(*x).y的简写。
(*x).y
Person_destroy 函数
Person_destroy
如果定义了创建函数,那么一定需要一个销毁函数,它会销毁Person结构体。我再一次使用了assert来确保不会得到错误的输入。接着我使用了free函数来交还通过malloc和strdup得到的内存。如果你不这么做则会出现“内存泄露”。
Person
free
译者注:不想显式释放内存又能避免内存泄露的办法是引入libGC库。你需要把所有的malloc换成GC_malloc,然后把所有的free删掉。
libGC
GC_malloc
Person_print 函数
Person_print
接下来我需要一个方法来打印出人们的信息,这就是这个函数所做的事情。它用了相同的x->y语法从结构体中获取成员来打印。
main 函数
main
我在main函数中使用了所有前面的函数和struct Person来执行下面的事情:
joe
frank
%p
请仔细阅读上面的描述,然后做下面的事情:
man 2 function
man 3 function
在你使用描述性注释扩展程序之后,要确保它实际上能够运行,并且产生下面的输出:
$ make ex16 cc -Wall -g ex16.c -o ex16 $ ./ex16 Joe is at memory location 0xeba010: Name: Joe Alex Age: 32 Height: 64 Weight: 140 Frank is at memory location 0xeba050: Name: Frank Blank Age: 20 Height: 72 Weight: 180 Name: Joe Alex Age: 52 Height: 62 Weight: 180 Name: Frank Blank Age: 40 Height: 72 Weight: 200
如果你完成了我要求的任务,你应该理解了结构体。不过让我来做一个明确的解释,确保你真正理解了它。
C中的结构体是其它数据类型(变量)的一个集合,它们储存在一块内存中,然而你可以通过独立的名字来访问每个变量。它们就类似于数据库表中的一行记录,或者面向对象语言中的一个非常简单的类。让我们以这种方式来理解它:
int
x.y
如果你不使用结构体,则需要自己计算出大小、打包以及定位出指定内容的内存片位置。实际上,在大多数早期(甚至现在的一些)的汇编代码中,这就是唯一的方式。在C中你就可以让C来处理这些复合数据类型的内存构造,并且专注于和它们交互。
使这个程序崩溃的办法涉及到使用指针和malloc系统的方法:
CFLAGS
-g
Valgrind
valgrind
who->name
在这个练习的附加题中我想让你尝试一些有难度的东西:将这个程序改为不用指针和malloc的版本。这可能很困难,所以你需要研究下面这些东西:
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8