c/c++编程总结一

C/C++ (MIT opencourse)

Course goal: to help proficient programmers understand how and when to use C and C++.

day1

C/C++被认为是老的,过时的,但是其效率是最高的

安装valgrind—mac

1
2
3
4
5
6
7
8
9
//valgrind内存泄漏检测工具,编译安装valgrind报错
//解决方案如下
brew edit valgrind
修改head下的url为git://sourceware.org/git/valgrind.git
brew update
brew install --HEAD valgrind


valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./run_hello

使用gcc、gdb、valgrind

1
2
3
4
5
6
7
8
vim hello.c

gcc -o run_hello hello.c

./run_hello

//使用valgrind检查run_hello是否内存泄漏,最终会发现有400bytes的泄漏
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./run_hello

hello.c

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
int *x = (int *)malloc(100 * sizeof(int));
x[0] = 0;

return 0;
}