sizeof

sizeof运算符

我是研究僧i 提交于 2019-12-10 01:45:26
sizeof运算符的作用是获取指定数据类型的字节数。在C#中只能用于值类型,不能用于引用类型中, 对于结构(struct),sizeof运算符可用于不安全的代码中。 1、在VS2008编程环境中,unsafe代码的编译必须使用/unsafe参数参能编译。在项目属性中,将“生成”页签 下的"允许不安全代码"选中,然后编译运行。 class SizeofExample { public static void GetSizeofExample() { unsafe//表示不安全代码 { //获取结构占用的字节长度,这句代码必须放在unsafe声明的范围内 Console.WriteLine("MyPoint结构占用的字节数为:" + sizeof(MyPoint)); } //以下显示各种基本数据类型的sizeof运算结果 Console.WriteLine("sbyte数据类型的字节数为:" + sizeof(sbyte)); Console.WriteLine("byte数据类型的字节数为:" + sizeof(byte)); Console.WriteLine("short数据类型的字节数为:" + sizeof(short)); Console.WriteLine("ushort数据类型的字节数为:" + sizeof(ushort)); Console.WriteLine(

What is the return type of sizeof operator?

左心房为你撑大大i 提交于 2019-12-10 00:35:05
问题 What is the return type of sizeof operator? cppreference.com & msdn says sizeof returns size_t. Does it really return a size_t? I'm using VS2010 Professional, and targeting for x64. int main() { int size = sizeof(int); // No warning int length = strlen("Expo"); //warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data return 0; } I have this question because first line is not issuing any warning, whereas the second does. Even if I change it to char size, I don

Internal mechanism of sizeof in C?

走远了吗. 提交于 2019-12-10 00:34:51
问题 I use sizeof to get size of a struct in C, but the result I got is unexpected. struct sdshdr { int len; int free; char buf[]; }; int main(){ printf("struct len:%d\n",(sizeof(struct sdshdr))); return 0; } //struct len:8, with or without buf my question is why does buf not occupy any space and why is the size of the int type still 4 on a 64-bit CPU? here is the output from gcc -v : Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4

初识c语言

与世无争的帅哥 提交于 2019-12-09 22:18:44
一、数据类型 如下即为我列举的各种数据类型 运行一段代码,求出每种数据类型的字节长度 #include <stdio.h> int main() { printf("sizeof(char)=%d\n", sizeof(char)); //字符数据类型 printf("sizeof(short)=%d\n", sizeof(short)); //短整型 printf("sizeof(int)=%d\n", sizeof(int)); //整形 printf("sizeof(long)=%d\n", sizeof(long)); //长整型 printf("sizeof(long long)%d\n", sizeof(long long)); //更长的整形 printf("sizeof(float)=%d\n", sizeof(float)); //单精度浮点数 printf("sizeof(double)=%d\n", sizeof(double)); //双精度浮点数 printf("sizeof(long double)=%d\n", sizeof(long double)); return 0; }    运行结果如图,每种类型的字节长度一目了然。 二、变量常量 1.变量的作用域 1). 局部变量的作用域是变量所在的局部范围。 2). 全局变量的作用域是整个工程。 2

C# sizeof(enum) alternative? (to workaround resharper false error)?

扶醉桌前 提交于 2019-12-09 17:46:40
问题 In C# I've got some "safe" API code related to UAC elevation. It involves getting the size of an enum (as follows) int myEnumSize = sizeof (MyEnum); The code itself is valid, compiles, works correctly etc. But Resharper falsely flags it as a an error ("Cannot use unsafe construct in safe context") within the solution. (Starting with version 2.0 of C#, applying sizeof to built-in types no longer requires that unsafe mode be used.) I love Resharper, and I love the solution analysis, but with

What is a common C/C++ macro to determine the size of a structure member?

半腔热情 提交于 2019-12-09 13:30:20
问题 In C/C++, how do I determine the size of the member variable to a structure without needing to define a dummy variable of that structure type? Here's an example of how to do it wrong, but shows the intent: typedef struct myStruct { int x[10]; int y; } myStruct_t; const size_t sizeof_MyStruct_x = sizeof(myStruct_t.x); // error For reference, this should be how to find the size of 'x' if you first define a dummy variable: myStruct_t dummyStructVar; const size_t sizeof_MyStruct_x = sizeof

Objective-C Runtime: What to put for size & alignment for class_addIvar?

久未见 提交于 2019-12-09 13:20:47
问题 The Objective-C Runtime provides the class_addIvar C function: BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) What do I put for size and alignment ? I'm adding an instance variable of type UITextPosition * , but no UITextPosition object is in scope. For size , can I just do sizeof(self) , where self is a subclass of UITextField ? I.e., can I assume that a UITextPosition object is the same size as a UITextField object? How do I get alignment

atoi,itoa,strcpy, strcmp,strcpy, strcpy_s, memc...

不问归期 提交于 2019-12-09 10:30:51
strcpy()、strlen()、memcpy()、memmove()、memset()的实现 strcpy(), 字符串拷贝. char * strcpy( char * strDest, const char * strSrc) { assert((strDest != NULL) && (strSrc != NULL)); char * address = strDest; while ( ( * strDest ++ = * strSrc ++ ) != ' \0 ' ) NULL ; return address ; } strlen, 第一种方法: int strlen( const char *str) { assert(str != NULL); int len = 0; while ((*str++) != '\0' ) len++; return len; } 第二种方法: int strlen( const char *str) { assert(str != NULL); const char *p = str; while ((*p++) != '\0' ); return p - str - 1; } 第三种方法: int strlen( const char * str) { if (str[0] == '\0' ) return 0; else

setsockopt 设置socket 详细用法

核能气质少年 提交于 2019-12-09 10:26:17
1. closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket: BOOL bReuseaddr=TRUE; setsockopt (s,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(BOOL)); 2. 如果要已经处于连接状态的soket在调用closesocket后强制关闭,不经历 TIME_WAIT的过程: BOOL bDontLinger = FALSE; setsockopt (s,SOL_SOCKET,SO_DONTLINGER,(const char*)&bDontLinger,sizeof(BOOL)); 3. 在send(),recv()过程中有时由于网络状况等原因,发收不能预期进行,而设置收发时限: int nNetTimeout=1000;//1秒 //发送时限 setsockopt (socket,SOL_S0CKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int)); //接收时限 setsockopt (socket,SOL_S0CKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int)); 4. 在send()的时候,返回的是实际发送出去的字节(同步

Sizeof operator with variable-length array type

时光怂恿深爱的人放手 提交于 2019-12-09 08:33:25
问题 According to cppreference: If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time. It means: if the type of expression is a VLA type, then expression is evaluated. For example: #include <stdio.h> int main() { int i = 0; int a[i]; printf("%zu\n",sizeof(a[i++])); printf("%d\n",i); // Here, print 0 instead of 1 return 0; } So, according to the reference, here i becomes 1 . But, with my GCC compiler, i