您的位置:首页 > 编程语言 > C语言/C++

base之basictypes.h基本数据类型 (1)

2016-07-08 17:31 267 查看
定义了基本的数据类型,比如int8, int16, int32以及基本数据类型的范围

关键在于数据类型占多少字节,比如char占1个字节,int占4个字节。

至于int, long,这两个我就不清楚,原本以为64位系统下,int占4个字节,long占8个字节。

实际上64位下,int和long都是占4字节

类型64位系统下所占字节数
int4
long4
long int4
long long8
long long int8
也就是说long long和long long int等同,因为占的字节数一样,表示的范围也一样,表示如下表

类型同类型64位系统下所占字节数
shortshort int2
intint4
longlong int4
long longlong long int8

其实stdint.h是这么实现的

/* Exact integral types.  */

/* Signed.  */

/* There is some amount of overlap with <sys/types.h> as known by inet code */
#ifndef __int8_t_defined
# define __int8_t_defined
typedef signed char     int8_t;
typedef short int       int16_t;
typedef int         int32_t;
# if __WORDSIZE == 64
typedef long int        int64_t;
# else
__extension__
typedef long long int       int64_t;
# endif
#endif


最后贴上我的代码

#ifndef _BASE_BASICTYPES_H_
#define _BASE_BASICTYPES_H_

//
// signed
//
typedef signed char     schar;

typedef signed char     int8;

typedef signed short    int16;

typedef signed int      int32;

typedef signed long long    int64;

//
// unsigned
//
typedef unsigned char   uchar;

typedef unsigned char   uint8;

typedef unsigned short  uint16;

typedef unsigned int    uint32;

typedef unsigned long long  uint64;

#endif // _BASE_BASICTYPES_H_


测试代码

#include "gtest/gtest.h"
#include "base/basictypes.h"

TEST(basictypes_unittest, size)
{
EXPECT_EQ(sizeof(schar), 1);
EXPECT_EQ(sizeof(int8),  1);
EXPECT_EQ(sizeof(int16), 2);
EXPECT_EQ(sizeof(int32), 4);
EXPECT_EQ(sizeof(int64), 8);

EXPECT_EQ(sizeof(uchar), 1);
EXPECT_EQ(sizeof(uint8), 1);
EXPECT_EQ(sizeof(uint16), 2);
EXPECT_EQ(sizeof(uint32), 4);
EXPECT_EQ(sizeof(uint64), 8);
}


简单实现一遍chromium的base,增长见识

源码 chromium-4.0.210.0_p26329

职业社交网站 领英
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  chromium c++