sizeof and strlen整理

sizeof

  • 定义
    • 计算对象类型所占用的字节数(byte)
      • 记住是字节数,而不是个数
  • 语法
    • sizeof(对象)
      • int i; sizeof(i);
    • sizeof(类型)
      • sizeof(int);
    • sizeof 对象
      • int i; sizeof i;
    • sizeof(2);sizeof(2+3.14);
      • sizeof()可以对一个表达式求值,但是它是根据表达式的类型进行求值。
      • 相当于sizeof(int);sizeof(double);
    • sizeof(function())
      • sizeof()可以对一个函数调用求值,其结果是函数返回类型的大小,记住函数是不会被调用的。
      • int function(); sizeof(function());//4
      • 返回值类型是没有确定的函数,不能调用sizeof;
      • void function(); sizeof(function());//error
      • void function(); sizeof(function); //error
  • sizeof的常量性
    • sizeof的计算发生在编译时刻
  • 基本数据类型的sizeof
    • 32位和64位机器的最大区别
      • 32位机器:指针代表寻址空间, 32位的寻址空间是2^32, 即32个bit,也就是4个字节
      • 64位机器,很显然就是8个字节
      • 所以sizeof(指针),32位得4bytes,64位得8bytes,但是其他的基本数据类型是一致的,如sizeof(int)都是4个字节。
  • 数组的sizeof
    • 静态分配的数组
    • 动态分配的数组
      • 很显然,动态分配的数组在编译时是无法知道数组的大小
        • n待输入;int* a=new int[n];sizeof(a); //4
        • 所以对于动态分配的数组sizeof将转换为对指针的sizeof
  • 指针--该指针指向字符数组
    • char *a = "12345"; sizeof(a); //4
    • 上述和char b[] = "12345"还是有区别的,sizeof(b) //6
  • 结构体的sizeof
    • sizeof求得的结构体(及其对象)的大小并不等于各个数据成员对象的大小之和!
      • 结构体的大小等于结构体内最大成员大小整数倍
      • 结构体内的成员的首地址相对于结构体首地址的偏移量是其类型大小的整数倍
      • 为了满足规则1和2编译器会在结构体成员之后进行字节填充

struct A{

         int num1;

         int num2;

         double num3;

};

sizeof(A)//16

struct B{

         int num1;

         double num3;

         int num2;

};

sizeof(B) //24
 
strlen
  • 定义
    • 返回字符串的长度。
      • 从字符串的第一个字符开始遍历,直到遇到结束符NULL。返回的长度不包括NULL。
    • 运行时计算长度,这个sizeof刚好相反
      • 静态字符数组
        • char a[] = "123";strlen(a);//3,遇到NULL停止,不包括NULL
        • char a[] = "";strlen(a);//0,不包括NULL
        • char a[20] = "123";strlen(a);//3
        • char* a= "123";strlen(a);//3
      • 动态字符数组
        • char *a = new char[20];strlen(a);//27
        • 上述并不知道NULL的结尾,所以得到的长度每次运行都不一样。

#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) void vTaskGetRunTimeStats( char *pcWriteBuffer ) { TaskStatus_t *pxTaskStatusArray; volatile UBaseType_t uxArraySize, x; uint32_t ulTotalTime, ulStatsAsPercentage; #if( configUSE_TRACE_FACILITY != 1 ) { #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats(). } #endif /* * PLEASE NOTE: * * This function is provided for convenience only, and is used by many * of the demo applications. Do not consider it to be part of the * scheduler. * * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part * of the uxTaskGetSystemState() output into a human readable table that * displays the amount of time each task has spent in the Running state * in both absolute and percentage terms. * * vTaskGetRunTimeStats() has a dependency on the sprintf() C library * function that might bloat the code size, use a lot of stack, and * provide different results on different platforms. An alternative, * tiny, third party, and limited functionality implementation of * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in * a file called printf-stdarg.c (note printf-stdarg.c does not provide * a full snprintf() implementation!). * * It is recommended that production systems call uxTaskGetSystemState() * directly to get access to raw stats data, rather than indirectly * through a call to vTaskGetRunTimeStats(). */ /* Make sure the write buffer does not contain a string. */ *pcWriteBuffer = 0x00; /* Take a snapshot of the number of tasks in case it changes while this function is executing. */ uxArraySize = uxCurrentNumberOfTasks; /* Allocate an array index for each task. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will equate to NULL. */ pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); if( pxTaskStatusArray != NULL ) { /* Generate the (binary) data. */ uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime ); /* For percentage calculations. */ ulTotalTime /= 100UL; /* Avoid divide by zero errors. */ if( ulTotalTime > 0 ) { /* Create a human readable table from the binary data. */ for( x = 0; x < uxArraySize; x++ ) { /* What percentage of the total run time has the task used? This will always be rounded down to the nearest integer. ulTotalRunTimeDiv100 has already been divided by 100. */ ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime; /* Write the task name to the string, padding with spaces so it can be printed in tabular form more easily. */ pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName ); if( ulStatsAsPercentage > 0UL ) { #ifdef portLU_PRINTF_SPECIFIER_REQUIRED { sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage ); } #else { /* sizeof( int ) == sizeof( long ) so a smaller printf() library can be used. */ sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); } #endif } else { /* If the percentage is zero here then the task has consumed less than 1% of the total run time. */ #ifdef portLU_PRINTF_SPECIFIER_REQUIRED { sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter ); } #else { /* sizeof( int ) == sizeof( long ) so a smaller printf() library can be used. */ sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); } #endif } pcWriteBuffer += strlen( pcWriteBuffer ); } } else { mtCOVERAGE_TEST_MARKER(); } /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION is 0 then vPortFree() will be #defined to nothing. */ vPortFree( pxTaskStatusArray ); } else { mtCOVERAGE_TEST_MARKER(); } } 解释一下
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值