sizeof

【Linux】C字节对齐

大兔子大兔子 提交于 2019-12-05 22:18:42
什么是对齐,以及为什么要对齐 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数据按照一定的规则在空间上排列,而不是顺序的一个接一个的排放,这就是对齐。 对齐的作用和原因 各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特定地址开始存取。其他平台可能没有这种情况,但是最常见的是如果不按照适合其平台要求对数据存放进行对齐,会在存取效率上带来损失。比如有些平台每次读都是从偶地址开始,如果一个int型(假设为 32 位系统)存放在偶地址开始的地方,那么一个读周期就可以读出,而如果存放在奇地址开始的地方,就可能会需要2个读周期,并对两次读出的结果的高低字节进行拼凑才能得到该int数据。显然在读取效率上下降很多。这也是空间和时间的博弈。 对齐的实现 通常,我们写程序的时候,不需要考虑对齐问题。编译器会替我们选择适合目标平台的对齐策略。当然,我们也可以通过给编译器传递预编译指令而改变对指定数据的对齐方法。 但是,正因为我们一般不需要关心这个问题,所以当编辑器对数据存放做了对齐,而我们不了解的话,常常会对一些问题感到迷惑。最常见的就是struct数据结构的sizeof结果,出乎意料。为此,我们需要对对齐算法所了解。 对齐的算法

sizeof(*this) and decltype(*this) in derived classes

匆匆过客 提交于 2019-12-05 21:39:10
Suppose there are classes: struct A { int a; virtual size_t GetMemoryUsage() const { return sizeof(*this); } }; struct B : public A { int b; }; And there may be deeper inheritance. What I want is to have a method which will return the number of bytes an object occupies in memory, GetMemoryUsage() in this case. Usually it can be achieved by using sizeof(*this) . The problem is (at least AFAIU) that I have to override the method in each derived class and actually copy-paste its body. I don't like duplicated code :) Am I correct? How can I make sizeof(*this) and decltype(*this) return what I want

sizeof char* array in C/C++

半腔热情 提交于 2019-12-05 21:30:22
问题 There are plenty of similar inquires, but in my case I don't understand what isn't working: int mysize = 0; mysize = sizeof(samplestring) / sizeof(*samplestring); std::cout << mysize << '\n' << samplestring; This outputs: 4 Press 'q' to quit. How is it possible? 4 definitely isn't the size of this string. I even tried the following, with the same result: mysize = sizeof(samplestring) / sizeof(samplestring[0]); EDIT: Ok, this is the declaration: char *samplestring = "Start."; I'm on C++, but I

空类的大小

拟墨画扇 提交于 2019-12-05 20:28:34
空类就是没有任何数据成员的类,这种类占用的内存大小在不同的语言里面有不同的实现 c struct A {}; printf("sizeof(A): %lu\n", sizeof(struct A)); // sizeof(A): 0 这个结果输出是0,也就是说 c 语言中的空类大小为 0 struct A a1; struct A a2; printf("address(a1): %p\n", &a1); printf("address(a2): %p\n", &a2); printf("&a1 == &a2: %d\n", &a1 == &a2); // address(a1): 0x7ffdead15ff0 // address(a2): 0x7ffdead15ff0 // &a1 == &a2: 0 在 gcc 中,两个空类拥有相同的地址,但是比较的结果却是不同的……这个我也不知道咋解释…… c++ class A {}; std::cout << "sizeof(A): " << sizeof(A) << std::endl; // sizeof(A): 1 c++ 的空类大小为1,因为 c++ 中规定不同的对象必须拥有不同的地址,如果为0会导致两个空类的地址一样 class B { A a1; A a2; }; std::cout << "sizeof(B): " <<

C语言 sizeof()用法介绍

爱⌒轻易说出口 提交于 2019-12-05 19:08:20
本文 转自https://www.cnblogs.com/huolong-blog/p/7587711.html 1. 定义 sizeof是一个操作符(operator)。 其作用是返回一个对象或类型所占的内存字节数。 2. 语法 sizeof有三种语法形式: 1) sizeof (object); //sizeof (对象) 2) sizeof object; //sizeof 对象 3) sizeof (type_name); //sizeof (类型) 对象可以是各种类型的变量,以及表达式(一般sizeof不会对表达式进行计算)。 sizeof对对象求内存大小,最终都是转换为对对象的数据类型进行求值。 sizeof (表达式); //值为表达式的最终结果的数据类型的大小 举例: int i; sizeof(int); //值为4 sizeof(i); //值为4,等价于sizeof(int) sizeof i; //值为4 sizeof(2); //值为4,等价于sizeof(int),因为2的类型为int sizeof(2 + 3.14); //值为8,等价于sizeof(double),因为此表达式的结果的类型为double char ary[sizeof(int) * 10]; //OK,编译无误 1. 基本数据类型的sizeof 这里的基本数据类型是指short

哈夫曼编解码解压缩文件—C++实现

丶灬走出姿态 提交于 2019-12-05 18:06:56
前言 哈夫曼编码是一种贪心算法和二叉树结合的字符编码方式,具有广泛的应用背景,最直观的是文件压缩。本文主要讲述如何用哈夫曼编解码实现文件的压缩和解压,并给出代码实现。 哈夫曼编码的概念 哈夫曼树又称作最优树,是一种带权路径长度最短的树,而通过哈夫曼树构造出的编码方式称作哈夫曼编码。 也就是说哈夫曼编码是一个通过哈夫曼树进行的一种编码,一般情况下,以字符 “0” 与 “1” 表示。编码的实现过程很简单,只要实现哈夫曼树,通过遍历哈夫曼树,这里我们从根节点开始向下遍历,如果下个节点是左孩子,则在字符串后面追加 “0”,如果为其右孩子,则在字符串后追加 “1”。结束条件为当前节点为叶子节点,得到的字符串就是叶子节点对应的字符的编码。 哈夫曼树实现 根据贪心算法的思想实现,把字符出现频率较多的字符用稍微短一点的编码,而出现频率较少的字符用稍微长一点的编码。哈夫曼树就是按照这种思想实现,下面将举例分析创建哈夫曼树的具体过程。下面表格的每一行分别对应字符及出现频率,根据这些信息就能创建一棵哈夫曼树。 字符 出现频率 编码 总二进制位数 a 500 1 500 b 250 01 500 c 120 001 360 d 60 0001 240 e 30 00001 150 f 20 00000 100 如下图,将每个字符看作一个节点,将带有频率的字符全部放到优先队列中

C语言中的内存对齐

独自空忆成欢 提交于 2019-12-05 17:27:17
什么是内存对齐 内存对齐”应该是编译器的“管辖范围”。编译器为程序中的每个数据单元安排在适当的位置上。 出现原因( 老生常谈的两句话 ) 平台原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。 性能原因:数据结构(尤其是栈)应该尽可能地在自然边界上对齐。原因在于,为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。 对齐规则 第一个成员放在偏移量(offset)为0的地方。 以后的每个成员的偏移量: min(#pragma pack()指定的数值,当前成员的大小) 的倍数中。 每个成员对齐后,本身也要对齐,整体的偏移量:min(#pragma pack()指定的数值,结构体最大数据成员的大小)。 关于 #pragma pack 通过 #pragma pack(n) 来设定变量以n字节对齐方式 n字节对齐就是说变量存放的起始地址的偏移量有两种情况 如果n大于等于该变量所占用的字节数,那么偏移量必须满足默认的对齐方式 。 果n小于该变量的类型所占用的字节数,那么偏移量为n的倍数,不用满足默认的对齐方式 。 32位系统默认为4,64位系统默认为8。 举个例子🌰 #include<stdio.h> #pragma pack(4) struct Node{ char a;

Size of packed struct with union of bit fields less than 8 bits in C

橙三吉。 提交于 2019-12-05 16:21:52
Is it possible in C to get the size of the following structure to be 2? #include <stdio.h> struct union_struct { char foo; char bar : 2; union { char foobar1 : 6; char foobar2 : 6; }; }; int main(void) { printf("size of union struct: %d\n", sizeof(struct union_struct)); return 0; } output, compiled with gcc: size of union struct: 3 If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently: #ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED #define ANON #else #define ANON3(X) anonymous__## X ##__ #define ANON2(X) ANON3(X) #define ANON ANON2(__LINE__)

变长数组的使用

本小妞迷上赌 提交于 2019-12-05 15:31:23
1 #include <stdio.h> 2 #include <string.h> 3 #include "iostream" 4 5 typedef struct { 6 unsigned int dwMsgLen; 7 char ucMsg[0];  //柔性数组(零长数组) C99以上支持 8 }T_SendMsg; 9 10 11 int main(void) 12 { 13 printf("sizeof(T_SendMsg) = %d\n", sizeof(T_SendMsg)); 14 15 char ucBuf[300] = {0}; 16 unsigned int dwPwName = 1002; 17 unsigned int dwPwNameLen = 0; 18 19 dwPwNameLen += sprintf(ucBuf,"pw%4d",dwPwName); 20 21 T_SendMsg tSendMsg; 22 memcpy(tSendMsg.ucMsg, ucBuf, dwPwNameLen); 23 24 printf("%s\n", tSendMsg.ucMsg); 25 printf("sizeof(T_SendMsg) = %d\n", sizeof(tSendMsg)); 26 return 0; 27 } g++ flexarr.cpp

C语言实现FTP服务器

微笑、不失礼 提交于 2019-12-05 15:20:26
公共部分代码 /* common.h */ #ifndef COMMON_H #define COMMON_H #include <arpa/inet.h> #include <ctype.h> #include <dirent.h> #include <errno.h> #include <fcntl.h> #include <netdb.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #define DEBUG 1 #define MAXSIZE 512 #define CLIENT_PORT_ID 30020 struct command { char arg[255]; char code[5]; }; int socket_create(int port); int socket_accept(int sock_listen); int socket_connect(int port, char *host); int recv_data(int sockfd, char*