mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-03-31 17:23:00 +08:00
support bazel complie this project and format code.
This commit is contained in:
15
basic_content/volatile/BUILD
Normal file
15
basic_content/volatile/BUILD
Normal file
@@ -0,0 +1,15 @@
|
||||
# please run `bazel run basic_content/volatile:noopt_vola`
|
||||
# please run `bazel run basic_content/volatile:volatile`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "noopt_vola",
|
||||
srcs = ["noopt_vola.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "volatile",
|
||||
srcs = ["volatile.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -80,7 +80,7 @@ void threadFunc2()
|
||||
可以。一个例子是只读的状态寄存器。它是volatile因为它可能被意想不到地改变。它是const因为程序不应该试图去修改它。
|
||||
|
||||
(2)一个指针可以是volatile吗?为什么?
|
||||
可以。尽管这并不常见。一个例子是当一个中断服务子程序修该一个指向一个buffer的指针时。
|
||||
可以。尽管这并不常见。一个例子是当一个中断服务子程序修改一个指向一个buffer的指针时。
|
||||
|
||||
(3)下面的函数有什么错误?
|
||||
```c++
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
/* Compile code without optimization option */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
const int local = 10;
|
||||
int *ptr = (int*) &local;
|
||||
#include <stdio.h>
|
||||
int main(void) {
|
||||
const int local = 10;
|
||||
int *ptr = (int *)&local;
|
||||
|
||||
printf("Initial value of local : %d \n", local);
|
||||
printf("Initial value of local : %d \n", local);
|
||||
|
||||
*ptr = 100;
|
||||
*ptr = 100;
|
||||
|
||||
printf("Modified value of local: %d \n", local);
|
||||
printf("Modified value of local: %d \n", local);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,16 +1,15 @@
|
||||
/* Compile code with optimization option */
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const volatile int local = 10;
|
||||
int *ptr = (int*) &local;
|
||||
int main(void) {
|
||||
const volatile int local = 10;
|
||||
int *ptr = (int *)&local;
|
||||
|
||||
printf("Initial value of local : %d \n", local);
|
||||
printf("Initial value of local : %d \n", local);
|
||||
|
||||
*ptr = 100;
|
||||
*ptr = 100;
|
||||
|
||||
printf("Modified value of local: %d \n", local);
|
||||
printf("Modified value of local: %d \n", local);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user