
C/C++ language
文章平均质量分 65
livedrm
try try try to touch the seventh layer
展开
-
Linux下C/C++程序内存布局 各种类型数据存储区域及生长方向
<br /><br />在32位系统下,对于一个进程来说,理论上它具有4G的内存空间,那么写一小段代码来看看各个部分在逻辑上是如何分布的。<br />#include <stdio.h><br />#include <stdlib.h><br />int g_i = 3;<br />static int s_i = 4;<br />const int c_i = 5;<br />char* g_p = "abc";<br />void fun()<br />{}<br />static void s_fun原创 2010-06-25 15:08:00 · 1735 阅读 · 1 评论 -
Linux下malloc/free详解
在程序开发中,堆和栈是最常使用的两个内存区,在Linux下栈分为用户栈和内核栈,内核栈具有固定大小,而用户栈可以通过ulimit来设定,最大8M。堆具有很大的灵活性,程序员可以根据需要获取任意大小的内存(只只是相对于栈来说,对于32位机,它最大能分配2G多的虚拟地址空间)。m原创 2011-08-30 18:38:31 · 6745 阅读 · 0 评论 -
Linux下malloc/free内存碎片问题
通过对malloc,free源码的分析,发现在堆上分配内存,很容易造成内存碎片。内存碎片在这里可以从两个方面进行分析,物理地址的内存碎片和线性地址(虚拟地址)的内存碎片。首先是物理地址的内存碎片,malloc_free_list大小依次为8,16,32....。那么我们请求的原创 2011-08-31 18:13:04 · 10868 阅读 · 0 评论 -
little endian & big endian (record)
Programmers should strive to make their programs portable across different machines and compilers.For program objects that span multiple bytes, we must establish two conventions: what will be the address of the object, and how will we order the bytes in me原创 2011-01-06 12:52:00 · 590 阅读 · 0 评论 -
内联函数并不总是内联
Inline function是在C++中引入的一种机制,它可以拓展函数代码,避免调用函数的额外开销。在Linux环境下,gcc编译选项必须加上优化选项才能使inline有效。如inline int fun(int i, int j){ printf("%d/n", i+j); return i+j;}void main(){fun(3,4);}不带优化编译选项:g++ -S inline.cc产生的汇编代码片断如下:andl $-16, %esp ;栈指针对齐sub原创 2011-01-05 18:55:00 · 693 阅读 · 0 评论 -
指针,引用区别
指针和引用看上去完全不同,但它们似乎具有相同的功能。先看一个小例子,了解下究竟编译器对他们做了什么处理。int main(){ int i = 3; int *p = &i; return 0;}我们生成它的汇编代码,主要部分为:main:.LFB2: pushl %ebp.LCFI0: movl %esp, %ebp.LCFI1: subl $8, %esp.LCFI2: andl $-16, %esp movl原创 2010-06-11 18:06:00 · 475 阅读 · 0 评论 -
const 小结
<br /><br />通用定义:In computer programming, a constant is a special kind of variable whose value cannot typically be altered by the program during its execution.<br /> <br />const主要用于限制程序员的写行为,限制不是禁止,仍然可以通过各种方式突破这种限制。<br /> <br />1.修饰普通变量<br />const int i; /原创 2010-06-09 17:38:00 · 439 阅读 · 0 评论 -
异或
1.异或运算具有交换律、结合律。2.一个数和自己做异或的结果是0。xorl %eax, %eax比movl $0, %eax快3.与0做异或,保持原值不变。运用特性1,2,3,常在面试中遇到不用中间变量交换两个变量值。int A=5, B=3;A B5 3A=A^B 5^3 3B=A^B 5^3 5^(3^3)=5^0=5A=A^B 5^3^5=3 5(这样运算只能帮助更好的理解异或的特性,但是针对效率而言,它不及采用中间变量的办法)。还有一个应用如:原创 2010-06-08 17:19:00 · 1525 阅读 · 1 评论 -
ELF format -- How programs look from the inside
ELF format -- How programs look from the insideELF 是用于Linux系统下一种文件格式,包括目标文件,二进制文件,共享库和core dump。它非常简单,而且具有好的输出格式。ELF针对不同的架构具有相同的布局,但是排列次序(endianness)和字长可能不同;重定位类型,符号类型和可能具有平台相关的值,当然也包括平台相关的代码。翻译 2010-12-21 18:02:00 · 1157 阅读 · 0 评论