sizeof

C语言字节对齐分析

∥☆過路亽.° 提交于 2019-11-29 02:18:02
1、前言 什么是字节对齐呢?现代计算机中的内存空间都是按字节(byte)划分的,从理论上讲似乎任何类型的变量的访问都可以从任何地址开始,但是实际情况是在访问特定变量的时候经常需要在特定的内存地址进行访问,因此,就需要各种类型数据按照一定的规则在空间上排列,而不是顺序地一个接一个地排放,这就是字节对齐。 2、字节对齐的好处 各个硬件平台对存储空间的处理上有很大的不同,一些平台对某些特定类型的数据只能从某些特定的地址开始存取,比如有些架构的CPU在访问一个没有进行对齐的变量的时候会发生错误,那么在这种架构上编程必须保证字节对齐。其它平台可能没有这种情况,但是最常见的是如果不按照适合其平台要求对数据存放进行对齐,会在存取效率上带来损失,例如某些平台每次读都是从偶地址开始,如果一个int类型(32位系统)变量存放在偶地址开始的地方,那么一个读周期就可以读出32bit,而如果存放在奇地址开始的地方,就需要2个读周期,并对两次读出的结果进行高低字节的拼凑才能得到该32bit数据。 3、对齐的准则 有四个重要的基本概念,如下: (1)数据类型自身的对齐值:char类型数据自身对齐值为1字节,short类型数据为2字节,int/float类型数据为4字节,double类型数据为8字节; (2)结构体或类的自身对齐值:其成员中自身对齐值最大的哪个值; (3)指定对齐值:#pragma pack

sizeof() std::vector (C++)

自作多情 提交于 2019-11-29 01:57:39
There is a topic already on this topic but I have doubts still. To calculate the size of a vector, which one is correct: sizeof(VEC) + sizeof(int) * VEC.capacity() or VEC.capacity() * (sizeof(VEC) + sizeof(int)) What do you mean by size of the vector? The size of the vector object is just sizeof(vec); If you are interested in how much memory the vector has allocated on the heap, you can use vec.capacity()*sizeof(T) So, if you add these, you'll get how much memory you've "lost" because of the vector. vec.capacity()*sizeof(T) + sizeof(vec) Please note that exactly how much memory is allocated is

How can I get sizeof a vector::value_type?

你离开我真会死。 提交于 2019-11-29 01:13:10
I want to get sizeof of the type that is contained in a vector. Here is what I tried: #include <iostream> #include <vector> int main() { std::vector<uint> vecs; std::cout << sizeof(vecs.value_type) << std::endl; return 0; } From my understanding this should be correct. However, when compiling with GCC 4.8.1 this is what I get: test-sizeof.cpp: In function ‘int main()’: test-sizeof.cpp:7:27: error: invalid use of ‘std::vector<unsigned int>::value_type’ std::cout << sizeof(vecs.value_type) << std::endl; ^ What am I doing wrong? How can I get the size of the contained type? TemplateRex 3.4.3

windows socket 完善数据简单结构化

一曲冷凌霜 提交于 2019-11-29 01:00:28
client //交互命令 enum CMD { CMD_LOGIN, CMD_LOGIN_RESULT, CMD_LOGOUT, CMD_LOGOUT_RESULT, CMD_ERROR }; //消息头 struct DataHeader { int cmd;//消息指令 int dataLength;//消息长度 }; //登录消息 struct Login : public DataHeader { Login() { cmd = CMD_LOGIN; dataLength = sizeof(Login); } char szName[64];//用户名 char szPass[64];//密码 }; //登录返回消息 struct LoginResult : public DataHeader { LoginResult() { cmd = CMD_LOGIN_RESULT; dataLength = sizeof(LoginResult); iResult = 1; } int iResult; }; //登出消息 struct Logout : public DataHeader { Logout() { cmd = CMD_LOGOUT; dataLength = sizeof(Logout); } char szName[64]; }; //登出返回消息

Python列表和元组

孤者浪人 提交于 2019-11-29 00:59:50
最近都在用Python写一些脚本,用到一些基础的数据结构,今天来聊一聊Python中最常见的两种数据结构:列表(list)和元组(tuple),这两种数据结构到底有哪些区别呢? 一、共同点 1、列表和元组,都是一个可以放置任意数据类型的有序集合。 比如: l = [1, 2, 'hello', 'world'] # 列表中同时含有int和string类型的元素 l [1, 2, 'hello', 'world'] tup = ('jason', 22) # 元组中同时含有int和string类型的元素 tup ('jason', 22) 2、列表和元组都支持负数索引 ,-1表示最后一个元素,-2表示倒数第二个 元素,以此类推。 l = [1, 2, 3, 4] l[-1] 4 tup = (1, 2, 3, 4) tup[-1] 4 3、列表和元组都支持切片操作 。比如: list = [1, 2, 3, 4] l[1:3] # 返回列表中索引从1到2的子列表 [2, 3] tup = (1, 2, 3, 4) tup[1:3] # 返回元组中索引从1到2的子元组 (2, 3) 4、列表和元组都可以随意嵌套 。比如: l = [[1, 2, 3], [4, 5]] # 列表的每一个元素也是一个列表 tup = ((1, 2, 3), (4, 5, 6)) #

【C/C++】复合类型(自定义类型)

杀马特。学长 韩版系。学妹 提交于 2019-11-29 00:50:57
gcc编译器 gcc(GNU Compiler Collection,GNU 编译器套件),是由 GNU 开发的编程语言编译器。gcc原本作为GNU操作系统的官方编译器,现已被大多数类Unix操作系统(如Linux、BSD、Mac OS X等)采纳为标准的编译器,gcc同样适用于微软的Windows。 gcc最初用于编译C语言,随着项目的发展gcc已经成为了能够编译C、C++、Java、Ada、fortran、Object C、Object C++、Go语言的编译器大家族。 编译命令格式: gcc [-option1] ... <filename> g++ [-option1] ... <filename> 1 2 命令、选项和源文件之间使用空格分隔 一行命令中可以有零个、一个或多个选项 文件名可以包含文件的绝对路径,也可以使用相对路径 如果命令中不包含输出可执行文件的文件名,可执行文件的文件名会自动生成一个默认名,Linux平台为a.out,Windows平台为a.exe gcc、g++编译常用选项说明: 选项 含义 -o file 指定生成的输出文件名为file -E 只进行预处理 -S(大写) 只进行预处理和编译 -c(小写) 只进行预处理、编译和汇编 C语言是不跨平台的,用Java用习惯的我突然回到C,有点不适应,用SpringBoot完成的Java项目,打成jar包

C语言面试笔试

冷暖自知 提交于 2019-11-29 00:49:37
1、运算符和表达式 1.1自增自减运算符++与– n++:表示先返回n,再让n+1=>n; ++n:表示先让n+1=>n,再返回n; n–:表示先返回n,再让n-1=>n; –n: 表示先让n-1=>n,再返回n; 例如n=2,表达式n++是先返回n,即2,再n自增1变为3,表达式++n是先n自增1变为3,再返回n,即3. 注意: 1 ++、–运算可以提高程序的执行效率。这是因为++、–只需要一条机器指令就可以完成,而n=n+1要对应3条机器指令; 2 自增自减运算符的对象只能是简单变量, 不能是常数,或带有运算符的表达式 ,即5++、–(a+b)是错误的; 贪心法 符号指的是程序的一个基本组成单元,起作用相当于一个句子中的单词,在C编译器解释表达式符号时,它 在移动到下一个符号之前在单个符号中包含尽可能多的字符,称之为贪心法 。 比如a++++b 这句话被解释为(a++)++b(所以表达式错误); a+++b++被解释为(a++)+(b++)表达式正确 ; #include <stdio.h> int main() { int x; scanf("%d",&x); if (x++>5) printf("%d\n",x); else printf("%d\n",--x);//注意这行代码 } 上面是输入4,得到4; #include <stdio.h> int main() {

C: Why isn't size_t a C keyword?

笑着哭i 提交于 2019-11-29 00:03:21
问题 sizeof is a C keyword . It returns the size in a type named size_t . However, size_t is not a keyword, but is defined primarily in stddef.h and probably other C standard header files too. Consider a scenario where you want to create a C program which does not include any C standard headers or libraries. (Like for example, if you are creating an OS kernel.) Now, in such code, sizeof can be used (it is a C keyword, so it is a part of the language ), but the type that it returns ( size_t ) is

Can I get the size of a struct field w/o creating an instance of the struct?

只愿长相守 提交于 2019-11-28 23:49:08
问题 It's trivial to get the size of a struct's field in C++ if you have an instance of the struct. E.g. (uncompiled): typedef struct Foo { int bar; bool baz; } Foo; // ... Foo s; StoreInSomething(s.bar, sizeof(s.bar)); // easy as pie Now I can still do something like this, but with the interface I'm implementing (I get a BOOL that indicates what the state of a specific bit in a bitfield should be), I'd be creating the struct solely to get the size of the data member. Is there a way to indicate to

[OpenCV]直线拟合

╄→гoц情女王★ 提交于 2019-11-28 23:08:27
OpenCV实现了直线的拟合。 CV_IMPL void cvFitLine( const CvArr* array, int dist, double param, double reps, double aeps, float *line ) { cv::AutoBuffer<schar> buffer; schar* points = 0; union { CvContour contour; CvSeq seq; } header; CvSeqBlock block; CvSeq* ptseq = (CvSeq*)array; int type; if( !line ) CV_Error( CV_StsNullPtr, "NULL pointer to line parameters" ); if( CV_IS_SEQ(ptseq) ) { type = CV_SEQ_ELTYPE(ptseq); if( ptseq->total == 0 ) CV_Error( CV_StsBadSize, "The sequence has no points" ); if( (type!=CV_32FC2 && type!=CV_32FC3 && type!=CV_32SC2 && type!=CV_32SC3) || CV_ELEM_SIZE(type) != ptseq-