C宏sizeof malloc

来源:互联网 发布:linux运维工程师是什么 编辑:程序博客网 时间:2024/05/17 05:07

一下内容参照百度百科


1.C宏使用

#include <iostream>//劲量使用带()的宏#define MAX(x,y) ((x)<(y)?(y):(x))#define MIN(x,y) (x>y?y:x)//##将两个字符连接、#在前原样输出在不检查里面是不是有内嵌了#define LINK(a,b)  a##b#define BEFORE(a) #a/* ({...})的作用是将内部的几条语句中最后一条的值返回,它也允许在内部声明变量 typeof (X) x_  X是什么类型就定义 x_ 是什么类型 */#define MINMIN(X,Y) ({\typeof (X) x_ = (X);\typeof (Y) y_ = (Y);\(x_ < y_) ? x_ : y_; })int main(int argc, const char * argv[]){    std::cout << MAX(10,11)<< std::endl;    std::cout << MIN(10,11)<< std::endl;    std::cout << LINK(11,22) << std::endl;        std::cout << BEFORE(11) << std::endl;    std::cout << BEFORE(LINK(11,22)) << std::endl;    std::cout << MINMIN(11,22) << std::endl;        return 0;}/* 11 10 1122 11 LINK(11,22) 11 */


2.sizeof

1.五种语法形式

sizeof(对象 | 基本类型变量)

sizeof(类型)

sizeof(常量)  会自动转换成对应的类型

sizeof(函数)  //返回值为  void 的函数不行

int i;
sizeof(i); 
sizeof(int); 
sizeof(2); 2的类型为int,所以等价于 sizeof( int );

sizeof(2+3.14); 3.14的类型为double,2也会被提升成double类型,所以等价于 sizeof( double );

char foo(){    printf("foo() has been called.\n");    return'a';}int main(){    size_tsz = sizeof( foo() );     // foo() 的返回值类型为char,所以sz = sizeof(char),foo()并不会被调用    printf("sizeof( foo() ) = %d\n", sz);}

2. 32位操作系统一些值

char : 1、int : 4、指针地址 : 4


3.指针和数组

char* pc = "abc";int* pi;string* ps;char** ppc = &pc;void(*pf)();      // 函数指针   sizeof(pc);       // 结果为4    sizeof(pi);       // 结果为4    sizeof(ps);       // 结果为4    sizeof(ppc);      // 结果为4    sizeof(pf);       // 结果为4char a1[] = "abc";            // 结果为4,字符末尾还存在一个NULL终止符 sizeof(a2)int a2[3]; sizeof( a1 );      // 结果为3*4=12(依赖于int)

函数数组

void foo3(char a3[3])   {           int c3 = sizeof( a3 );     //c3 ==  4 }   void foo4(char a4[])   {           int c4 = sizeof( a4 );     //c4 == 4   }

4.结构体、联合体、枚举

结构体

struct S1    {charc;  inti;    }; 可能是 5 ,也可能是 8 有位对齐, 为了方便查找

联合体  取占空间最大的

枚举  正常


5.sizeof与strlen

1.sizeof 是运算符、strlen 是函数。

2.数组做sizeof不退化,strlen 变指针了

3.sizeof实际开辟内存大小,strlen运行时检查字符串长度(必须'\0'结尾)

char str[20]="0123456789";int a = strlen(str);                //a=10;int b = sizeof(str);                //而b=20;

例子

char ss[] = "0123456789";   sizeof(ss)      //结果 11 ===》ss是数组,计算到\0位置,因此是10+1   sizeof(*ss)     //结果 1 ===》*ss是第一个字符int ss[100] = "0123456789";sizeof(ss)      //结果 400 ===》ss表示在内存中的大小 100×4strlen(ss)      //错误 ===》strlen的参数只能是char* 且必须是以'\0'结尾的char q[]="abc";char p[]="a\n";sizeof(q),sizeof(p),strlen(q),strlen(p);char const* static_string = "Hello";   // sizeof(static_string) 是 sizeof 一个指针,所以在 32bit system 是 4   char stack_string[] = "Hello";         //sizeof(stack_string) 是 sizeof 一个数组,所以是 6 * sizeof(char)   char* string = newchar[6];   strncpy(string, "Hello", 6);           // sizeof(string) 是 sizeof 一个指针,所以还是 4。    // 和第一个不同的是,这个指针指向了动态存储区而不是静态存储区struct test{ int a, b, c, d, e, f, g, h;};int main(){    test & r = *new test;    cout << sizeof(test) << endl;    // 32    cout << sizeofr << endl;         // 也是 32    system("PAUSE");}//r 引用的是整个的 test 对象而不是指向 test 的指针,所以 sizeof r 的结果和 sizeof test 完全相同。


3.malloc

1.使用

void *malloc(size_t size); 

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

if(p == null) 失败

2.与new区别

int *p = new int;

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


int *p = new int[100];

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

3.释放

free(p); p = null;

4.原理

malloc函数的实质体现在,它有一个将可用的内存块连接为一个长长的列表的所谓空闲链表










0 0
原创粉丝点击