物探论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1163|回复: 0

[Envi] C++库研究笔记——几个注意(size_t)

[复制链接]
发表于 2013-8-12 11:32:26 | 显示全部楼层 |阅读模式
6.47 Function Names as Strings:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
GCC provides three magic variables that hold the name of the current function, as a string. The first of these is __func__, which is part of the C99 standard:
The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
     static const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function.
__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend you use __func__, but provide a fallback definition with the preprocessor:
     #if __STDC_VERSION__ < 199901L
     # if __GNUC__ >= 2
     #  define __func__ __FUNCTION__
     # else
     #  define __func__ "<unknown>"
     # endif
总之,用size_t 保证可移植性,保证其可以表示出最大需要表示的长度。
size_t 在stddef.h 中无命名空间
在<cstddef>有std::
size_t 与ptrdiff_t 要联合使用

When i is signed and size is unsigned, then i is converted to unsigned before the comparison is performed. This is part of what are called the usual arithmetic conversions.http://stackoverflow.com/questio ... for-loop-and-size-t
unsigned int vs. size_t:
When to use std::size_t?
Difference between size_t and std::size_t
About size_t and ptrdiff_t
(ptrdiff_t 在64位下有更高的效率)

[cpp] view plaincopy
#include <iostream>  
#include <stddef.h>  

template <typename T, int N>  
int size(T (&)[N])  
{  
    return N;  
}  

int main()  
{  
    int a[20];  
    std::cout<<size(a)<<std::endl;  


    std::cout<<"sizeof(short):"<<sizeof(short)<<std::endl;  
    std::cout<<"sizeof(float):"<<sizeof(float)<<std::endl;  
    std::cout<<"sizeof(double):"<<sizeof(double)<<std::endl;  
    std::cout<<"sizeof(long):"<<sizeof(long)<<std::endl;  
    std::cout<<"sizeof(size_t):"<<sizeof(size_t)<<std::endl;  


    std::cout<<"sizeof(ptrdiff_t):"<<sizeof(ptrdiff_t)<<std::endl;  // error when not using #include <stddef.h>  
    std::cout<<"sizeof(ptrdiff_t):"<<sizeof(std::ptrdiff_t)<<std::endl;  
    return 0;  
}  

结果:

20
sizeof(short):2
sizeof(float):4
sizeof(double):8
sizeof(long):8
sizeof(size_t):8
sizeof(ptrdiff_t):8
sizeof(ptrdiff_t):8


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|物探论坛 ( 鄂ICP备12002012号 微信号:iwutan )

GMT+8, 2024-4-19 23:08 , Processed in 0.062448 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表