sizeof

【内存字节】深入理解sizeof占位内存

核能气质少年 提交于 2019-12-06 20:28:17
###运行代码 让计算机告诉你数据类型站内存情况 //32位系统,地址长度是32位(bit),也就是4Byte 64位系统,地址长度是64位(bit),也就是8Byte //注意 1byte = 8 bit;sizeof byte #import <Foundation/Foundation.h> int main(int argc, char *argv[]) { @autoreleasepool { char a[] = "go swift"; //自动为末尾加上'/0',注意空格也要占字节 char b[14] = "go swift"; char *c = a; char *d = "01234"; int16_t t16; int32_t t32; int64_t t64; NSLog(@"%ld", sizeof(a)); NSLog(@"%ld", sizeof(b)); NSLog(@"%ld", sizeof(c)); NSLog(@"%ld", sizeof(d)); //d是指向字符串常量的字符指针 NSLog(@"%ld", sizeof(*d)); //*d是第一个字符 (所占大小由数据类型决定) NSLog(@"int type: %ld,%ld,%ld,%ld", sizeof(t16),sizeof(t32),sizeof(t64),sizeof(

Do the C++ standards guarantee that unused private fields will influence sizeof?

旧时模样 提交于 2019-12-06 20:16:58
问题 Consider the following struct: class Foo { int a; }; Testing in g++, I get that sizeof(Foo) == 4 but is that guaranteed by the standard? Would a compiler be allowed to notice that a is an unused private field and remove it from the in-memory representation of the class (leading to a smaller sizeof)? I don't expect any compilers to actually do that kind of optimization but this question popped up in a language lawyering discussion so now I'm curious. 回答1: The C++ standard doesn't define a lot

What is the size of a Nullable<Int32>?

橙三吉。 提交于 2019-12-06 19:28:58
问题 So, a couple of questions, actually: An int ( Int32 ) is specified to be (obviously) 32 bits. What about an int? ( Nullable<int> )? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that. I would have answered my own question using sizeof(int?) ; but as int? is a managed type this is not allowed. I understand that the size of a type may be platform-dependent, and that in the case of objects which

Array size metafunction - is it in boost somewhere?

若如初见. 提交于 2019-12-06 18:00:52
问题 I found the following template on a blog: template <typename T, size_t N> struct array_info<T[N]> { typedef T type; enum { size = N }; }; It is an elegant alternative to sizeof(a) / sizeof(a[0]) . A commonly-used construct for getting the size of an array should surely be somewhere in a library. I'm not aware of one. Can anyone tell me this functionality is in the standard libraries somewhere and/or in Boost? Preferably in an easy-to-use and lightweight form. 回答1: I eventually found the

C语言各种变量的初始化

佐手、 提交于 2019-12-06 17:49:07
数值类变量初始化 整型、浮点型的变量可以在定义的同时进行初始化,一般都初始化为 0 。 int inum = 0 ; float fnum = 0.00f ; double dnum = 0.00 ; 字符型变量初始化 字符型变量也可在定义的同时进行初始化,一般初始化为 '\0' 。 char ch = '\0' ; 字符串初始化 字符串初始化的方法比较多,我这里简单介绍三种,因为字符串本质上是由一个个字符组成的字符数组,所以其初始化的最终目的, 就是将字符数组里面的一个个字符都初始化为 '\0' 。 方法一 :使用空的字符串 "" 。 char str [ 10 ] = "" ; 方法二 :使用 memset 。 char str [ 10 ] ; memset ( str , 0 , sizeof ( str ) ) ; 方法三 :写一个循环。 char str [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { str [ i ] = '\0' ; } 这里比较推荐的是第二种初始化方法。也即使用 memset 进行初始化。 很多人对 memset 这个函数一知半解,只知道它可以初始化很多数据类型的变量,却不知道其原理是什么样的,这里做一下简要的说明: memset 是按照字节进行填充的。 先看下面的一段代码: int num;

Do I have the guarantee that sizeof(type) == sizeof(unsigned type)?

偶尔善良 提交于 2019-12-06 16:44:51
问题 The sizeof char, int, long double... can vary from one compiler to another. But do I have the guarantee according to the C++11 or C11 standard that the size of any signed and unsigned fundamental integral type is the same ? 回答1: The C++11 Standard says about integer types: (§3.9.1/3) For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

半城伤御伤魂 提交于 2019-12-06 16:06:32
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members? Kevin This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS ). Mis-aligned access might be a soft error. Either corrected in hardware, for a modest performance-degradation. Or corrected by emulation in software, for a severe performance-degradation. In addition, atomicity and other concurrency-guarantees might be broken, leading to

how and why sizeof(a)/sizeof(a[0]) in c is used to calculate the number of elements in an array

…衆ロ難τιáo~ 提交于 2019-12-06 15:15:54
I am a beginner to programming and i don't know the exact meaning of sizeof(a) and sizeof(a[0]) to calculate the no of elements in an array. Why and where is this function used ? And what is the purpose of dividing them. According to the C Standard (6.5.3.4 The sizeof and alignof operators) 2 The sizeof operator yields the size (in bytes) of its operand , which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise,

Is it safe to assume sizeof(double) >= sizeof(void*)?

狂风中的少年 提交于 2019-12-06 14:50:49
Is it safe to assume that sizeof(double) will always be greater than or equal to sizeof(void*) ? To put this in some context, is the following portable? int x = 100; double tmp; union { double dbl; void* ptr; } conv; conv.ptr = (void*)&x; tmp = conv.dbl; conv.dbl = tmp; printf("%d\n", *((int*)conv.ptr)); It does work on the few machines that I've tested it on, but I can see this going horribly wrong if sizeof(void*) > sizeof(double) . On current systems yes. double is 64 bits on all current and future systems because they're aligned with IEEE arithmetic's double-precision. It's unlikely but

tuxedo服务器代码

牧云@^-^@ 提交于 2019-12-06 14:43:17
/* #ident "@(#) samples/atmi/simpapp/simpserv.c $Revision: 1.7 $" */ #include <stdio.h> #include <ctype.h> #include <atmi.h> /* TUXEDO Header File */ #include <userlog.h> /* TUXEDO Header File */ #include </home/zhangenhao/OraHome_1/tuxedo12.1.3.0.0/simpapp/clsinterface.h> #include <fml.h> #include "strPort.h" #include <string.h> #include <stdlib.h> #include "cJSON.h" #define ROW 4422 #define STRLEN 120 #define MOSTDOMAIN 200 #define DATALEN 2048 int lastIndexOf(char *dst, char *src); int indexOf(char *dst, char *src); void ltrim(char *str); void rtrim(char *str); void trim(char *str); void