6.S081:lecture 2 笔记

preparation:

2.9 (Bitwise operators) :

主要是讲了,位操作。
& 按位与(AND)
| 按位或(OR)
^ 按位异或(XOR)
<< 左移
>> 右移
~ 按位求反(一元运算符)

n = n & 0177;
在 C++ 中,`n = n & 0177` 是位运算的表达式。

`&` 是按位与运算符。`0177` 是一个八进制数(以 0 开头表示),其对应的十进制值为 127。在这个语句中,`n` 与 127 进行按位与操作。

具体步骤是,将 `n` 和 `0177`(即 127)转换为二进制后,对每一位进行与操作。这将导致 `n` 的最高位之外的位都保持不变,而最高位可能被清零(因为 `0177` 只有最低 7 位是 1,最高位是 0)。

这种操作通常用于将整数 `n` 限制为 0 到 127 之间的范围,确保结果不超出这个范围。

位操作的灵活运用:将要取的位置移动到最右边,再与需求位置为1,其他位置为0的数做&操作。

/* getbits: get n bits from position p */ 
 unsigned getbits(unsigned x, int p, int n) 
 { 
 	return (x >> (p+1-n)) & ~(~0 << n); 
 }

5.1 (Pointers and addresses) through 5.6 (Pointer arrays)

主要介绍指针相关内容。

  1. 指针与变量地址的关系
  2. 指针作为函数参数时对于原传入参数的影响
  3. 指针与数组之间的关系
  4. 指针,地址的算术运算

有效的指针运算包括相同类型指针之间的赋值运算;指针同整数之间的加法或减法运算;
指向相同数组中元素的两个指针间的减法或比较运算;将指针赋值为 0 或指针与 0 之间的比
较运算。

  1. 指针与字符串
  2. 指针数组

说了一个快排的例子,麻了,快排忘了。

6.4 pointers to structures

指向结构的指针。

LEC 2 (TAs/dm):

1. 有关GDB调试

2. 有关C语言内存管理与指针

Memory in C

Static Memory
● Global variables, accessible throughout the whole program
● Defined with static keyword, as well as variables defined in global scope.
Stack Memory
● Local variables within functions. Destroyed after function exits.
Heap Memory
● You control creation and destruction of these variables: malloc(), free()
● Can lead to memory leaks, use-after-free issues.

Pointers in C

  • A pointer is a 64-bit integer whose value is an address in memory.
  • Every variable has an address, so every variable’s pointer can be accessed,
    including a pointer to a pointer. And a pointer to a pointer to a pointer. And so on.
  • A pointers can handle arithmetic with the operators ++, --, +, -.

Pointers add and subtract in increments of the base data’s length (in bytes).

Use the extern keyword! Extends function’s visibility to all files in the program.

posted @ 2023-11-02 20:49  HDD-SG  阅读(54)  评论(0)    收藏  举报