C language programming by OSTEP.intro Vitualizing Memor

代码内容

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char *argv[]) {
    if (argc != 2) { 
	fprintf(stderr, "usage: mem <value>\n"); 
	exit(1); 
    } 
    int *p; 
    p = malloc(sizeof(int));
    assert(p != NULL);
    printf("(%d) addr pointed to by p: %p\n", (int) getpid(), p);
    *p = atoi(argv[1]); // assign value to addr stored in p
    while (1) {
	Spin(1);
	*p = *p + 1;
	printf("(%d) value of p: %d\n", getpid(), *p);
    }
    return 0;
}
wby@ubuntu:~/Documents/ostep-code-master/intro$ ./mem  
usage: mem <value>
wby@ubuntu:~/Documents/ostep-code-master/intro$ ./mem a
(2503) addr pointed to by p: 0x562f242e9260
(2503) value of p: 1
(2503) value of p: 2
(2503) value of p: 3
(2503) value of p: 4
(2503) value of p: 5
^C
wby@ubuntu:~/Documents/ostep-code-master/intro$ ./mem 1
(2667) addr pointed to by p: 0x55ed75c45260
(2667) value of p: 2
(2667) value of p: 3
(2667) value of p: 4
(2667) value of p: 5
^Z
[1]+  Stopped                 ./mem 1
wby@ubuntu:~/Documents/ostep-code-master/intro$ ./mem 5
(2668) addr pointed to by p: 0x559a69254260
(2668) value of p: 6

代码分析

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

#include <unistd.h>由字面意思,unistd.h是unix std的意思,是POSIX标准定义的unix类系统定义符号常量的头文件,
包含了许多UNIX系统服务的函数原型,例如read函数、write函数和getpid函数。

int main(int argc, char *argv[]) {
    if (argc != 2) { 
	fprintf(stderr, "usage: mem <value>\n"); 
	exit(1); 
    } 

和上一篇文章一样,判断参数数量

    int *p; 
    p = malloc(sizeof(int));

stdlib.h中定义的void *malloc(size_t size)()函数,size指字节,比如malloc(15)就是分配15字节的内存空间

    assert(p != NULL);

定义在头文件assert.h中,判断表达式的逻辑真假,如果为false,就会在stderr上面打印一条包含“表达式,文件名,行号”的错误信息,然后调用abort结束整个程序。
(在common.h头文件中定义的)

    printf("(%d) addr pointed to by p: %p\n", (int) getpid(), p);

这是printf()函数的使用方法,%是一个格式化标志,可以是%d %p %x。 参考资料
%p表示输出以内存中实际存储一个变量格式(十六进制、32位(视机器而定))的值。

    *p = atoi(argv[1]); // assign value to addr stored in p

在C标准库<stdlib.h>中定义的函数,把参数指向的字符串转换为一个整数(类型为int型)
例如:

字符串值 = 98993489, 整型值 = 98993489
字符串值 = xxx.com, 整型值 = 0
    while (1) {
		Spin(1);
		*p = *p + 1;
		printf("(%d) value of p: %d\n", getpid(), *p);
    }
    return 0;
}

循环输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular Intro.js 是一个 AngularJS 模块,用于集成 Intro.js 库,以提供网站或应用程序的新手引导和演示。下面是使用步骤: 1. 安装 Angular Intro.js:通过 npm 安装 Angular Intro.js 模块。 ``` npm install angular-intro.js --save ``` 2. 引入 Intro.js 库:在项目中引入 Intro.js 库,可以通过 npm 安装或直接使用 CDN 引入。 ``` <script src="https://cdnjs.cloudflare.com/ajax/libs/intro.js/2.9.3/intro.min.js"></script> ``` 3. 引入 Angular Intro.js 模块:在 AngularJS 应用程序中引入 Angular Intro.js 模块。 ``` angular.module('myApp', ['angular-intro']); ``` 4. 在 HTML 中使用:在 HTML 中使用指令来添加新手引导和演示。 ``` <div introjs intro-options="options"> <p>这是一个新手引导示例</p> <button ng-click="showStep2()">下一步</button> </div> ``` 上面的代码中,`introjs` 指令告诉 Angular Intro.js 应该在这个元素上启用引导,`intro-options` 属性包含 Intro.js 配置选项,`showStep2()` 是一个 AngularJS 控制器中的函数,用于显示下一个步骤。 5. 在控制器中配置选项和方法:在 AngularJS 控制器中配置 Intro.js 选项和方法。 ``` angular.module('myApp').controller('myCtrl', function($scope) { $scope.options = { steps: [ { element: document.querySelector('#step1'), intro: '这是第一步' }, { element: document.querySelector('#step2'), intro: '这是第二步' } ] }; $scope.showStep2 = function() { introJs().goToStep(2).start(); }; }); ``` 上面的代码中,`steps` 选项包含 Intro.js 步骤对象,每个步骤是一个包含 `element` 和 `intro` 属性的对象。`showStep2()` 函数使用 Intro.js 方法来显示第二步。 以上是 Angular Intro.js 的简单使用方法,可以根据需要进行配置和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值