sizeof

__attribute__机制介绍

邮差的信 提交于 2019-12-04 04:54:05
1. __attribute__ GNU C 的一大特色(却不被初学者所知)就是 __attribute__ 机制。 __attribute__ 可以设置函数属性 (Function Attribute) 、变量属性 (Variable Attribute) 和类型属性 (Type Attribute) __attribute__ 前后都有两个下划线,并且后面会紧跟一对原括弧,括弧里面是相应的 __attribute__ 参数 __attribute__ 语法格式为: __attribute__ ( ( attribute-list ) ) 函数属性( Function Attribute ),函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。 __attribute__ 机制也很容易同非 GNU 应用程序做到兼容。 GNU CC 需要使用 –Wall ,这是控制警告信息的一个很好的方式。下面介绍几个常见的属性参数。 2. format 该属性可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配 。它可以给被声明的函数加上类似 printf 或者 scanf 的特征,该功能十分有用,尤其是处理一些很难发现的 bug 。 format 的语法格式为: format ( archetype, string-index,

C++获取操作系统版本号和默认语言

本小妞迷上赌 提交于 2019-12-04 04:52:34
//以下是获得系统类型和版本的代码 OSVERSIONINFO osvi; ZeroMemory(&osvi,sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoaSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); //以下获得系统的默认使用语言 LCID lcid = GetSystemDefaultLCID();//LCID 实际是unsigned long 类型,所以可以通过LCID编码来比较是什么语言, //具体请参照语言编码表 语言编码表参见以下链接 http://www.science.co.il/Language/Locale-codes.asp 来源: CSDN 作者: 肖邦之离歌 链接: https://blog.csdn.net/u011569364/article/details/17118827

Sizeof(char[]) in C

家住魔仙堡 提交于 2019-12-04 04:05:37
问题 Consider this code: char name[]="123"; char name1[]="1234"; And this result The size of name (char[]):4 The size of name1 (char[]):5 Why the size of char[] is always plus one? 回答1: As Michael pointed out in the comments the strings are terminated by a zero. So in memory the first string will look like this "123\0" where \0 is a single char and has the ASCII value 0. Then the above string has size 4. If you had not this terminating character, how would one know, where the string (or char[] for

C语言字节对齐问题详解

我怕爱的太早我们不能终老 提交于 2019-12-04 03:48:45
本文转自: https://www.cnblogs.com/clover-toeic/p/3853132.html 引言 考虑下面的结构体定义: 1 typedef struct{ 2 char c1; 3 short s; 4 char c2; 5 int i; 6 }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始地址是0,则s的地址就是1,c2的地址是3,i的地址是4。 现在,我们编写一个简单的程序: 1 int main(void){ 2 T_FOO a; 3 printf("c1 -> %d, s -> %d, c2 -> %d, i -> %d\n", 4 (unsigned int)(void*)&a.c1 - (unsigned int)(void*)&a, 5 (unsigned int)(void*)&a.s - (unsigned int)(void*)&a, 6 (unsigned int)(void*)&a.c2 - (unsigned int)(void*)&a, 7 (unsigned int)(void*)&a.i - (unsigned int)(void*)&a); 8 return 0; 9 } 运行后输出: 1 c1 -> 0, s -> 2, c2 -> 4, i -> 8 为什么会这样?这就是字节对齐导致的问题。

sizeof a struct member [duplicate]

偶尔善良 提交于 2019-12-04 01:45:40
This question already has answers here : Closed 6 years ago . sizeof single struct member in C (9 answers) How can I get the size of a member in a struct in C? struct A { char arr[64]; }; i need something like that: sizeof(A::arr) thanks wkl sizeof(((struct A*)0)->arr); Briefly, cast a null pointer to a type of struct A* , but since the operand of sizeof is not evaluated, this is legal and allows you to get size of struct members without creating an instance of the struct. Basically, we are pretending that an instance of it exists at address 0 and can be used for offset and sizeof

c++ 面试题 面试题复盘

随声附和 提交于 2019-12-04 01:36:07
目录 1. #include<filename.h>与#include"filename.h"的区别 3.malloc/free 与 new delete 1. #include<filename.h>与#include"filename.h"的区别 对于#include <filename.h> ,编译器从标准库路径开始搜索filename.h, 对于#include “filename.h” ,编译器从用户的工作路径开始搜索filename.h # 2 C++中的类与c中的struct的区别 c++ 中的struct与 c中的struct区别 a) C语言中的 结构体不能为空 ,否则会报错 b) C语言中的结构体只涉及到数据结构,而不涉及到算法,也就是说在C中 数据结构和算法 是分离的。换句话说就是C语言中的结构体 只能定义成员变量 ,但是 不能定义成员函数 。然而在C++中既可以定义成员变量又可以定义成员函数, C++中的结构体和类体现了数据结构和算法的结合 c) C语言的结构体中不能定义成员函数,但是却可以定义 函数指针 ,不过 函数指针本质上不是函数而是指针 ,所以总的来说C语言中的结构体只是一个复 杂数据类型 , 只能定义成员变量,不能定义成员函数,不能用于面向对象编程 。 d) 在C语言中结构体变量定义的时候,若为struct 结构体名 变量名定义的时候

Output data type of sizeof() operator

时光怂恿深爱的人放手 提交于 2019-12-04 00:23:12
问题 I am using Ubuntu 16.04.5 and GCC version 5.4.0. I was playing with sizeof() operator, wrote the code below: #include <stdio.h> int main(int argc, char *argv[]){ long int mylint = 31331313131.1313; printf("size of long int is %d\n", sizeof(mylint)); printf("size of long int is %d\n", sizeof(long int)); return 0; } I tried to compile using gcc -o ... ... command and was expecting: size of long int is 8 size of long int is 8 But I got the following error: fl_double_lint.c: In function ‘main’:

Is it safe to assert(sizeof(A) == sizeof(B)) when A and B are “the same”?

倖福魔咒の 提交于 2019-12-04 00:20:46
Suppose I have two classes that I would expect to have exact same memory layout: struct A { int x; int y; }; /* possibly more code */ struct B { int a; int b; }; Is there anything in the standard that guarantees that I can safely static_assert(sizeof(A) == sizeof(B)) ? As a weaker variant consider struct C { int a; }; static_assert( sizeof(A) >= sizeof(C) ); // can this ever fail? static_assert( sizeof(A) > sizeof(C) ); // can this ever fail? The question was triggered by this one . Naively I would not expect any of the asserts to fail, but is this guaranteed? Nothing in the Standard would

关于结构体大小一篇很详细的文章

有些话、适合烂在心里 提交于 2019-12-03 23:08:30
## 前言 ## 在计算机中数据存储和传输以位(bit)为单位,每8个位bit组成1个字节(Byte)。32位计算机的字长为32位,即4个字节;对应的,64位计算机的字长为64位,即8个字节。计算机系统对基本类型数据在内存中存放的位置有限制,要求这些数据的起始地址的值是某个数k的倍数,这就是所谓的内存对齐,而这个k则被称为该数据类型的对齐模数(alignment modulus)。 ## 结构的存储分配 ## 编译器按照结构体成员列表的顺序为每个成员分配内存,当存储成员时需要满足正确地边界对齐要求时,成员之间可能出现用于填充地额外内存空间。32位系统每次分配字节数最多为4个字节,64位系统分配字节数最多为8个字节。 以下图表是在不同系统中基本类型数据内存大小和默认对齐模数: 注:此外指针所占内存的长度由系统决定,在32位系统下为32位(即4个字节),64位系统下则为64位(即8个字节). ## 没有#pragma pack宏的对齐 ## **对齐规则** : (1)结构体的起始存储位置必须是能够被该结构体中最大的数据类型所整除。 (2)每个数据成员存储的起始位置是自身大小的整数倍(比如int在32位机为4字节,则int型成员要从4的整数倍地址开始存储)。 (3)结构体总大小(也就是sizeof的结果),必须是该结构体成员中最大的对齐模数的整数倍。若不满足

c++晚捆绑的实现机制

Deadly 提交于 2019-12-03 22:07:46
早绑定 (early binding)是指在实例化对象之前定义它的属性和方法,这样编译器或解释程序就能够提前转换机器代码。 晚绑定 (late binding)指的是编译器或解释程序在运行前,不知道对象的类型。使用晚绑定,无需检查对象的类型,只需检查对象是否支持属性和方法即可。 早绑定的优点是: (1) 编译效率 高 (2) 有代码提示 (3) 编译时类型检查 晚绑定的优点是: (1) 不用申明类型 (2) 对象类型可以随时更改 virtual关键字可以告诉编译器实行的是晚捆绑(虚函数)。 为了实现晚捆绑,典型的编译器对每个包含虚函数的类将创建一个表(VTABLE),在VTABLE中放着特定类的虚函数地址。在每个带有虚函数的类中,编译器会放置一个指针VPTR,指向这个对象的VTABLE。当通过基类指针做虚函数调用时,编译器静态的插入能取得这个VPTR并在VTAVLE表中查找函数地址的代码,这样就会引起晚捆绑的发生。 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class NoVirtual { public: void fun() {} int ret() {} private: int a; }; class OneVirtual { public: virtual void