sizeof

Size of generic structure

余生颓废 提交于 2019-11-30 20:27:50
I need to find out a size of a generic structure (I can not do it like sizeof(T) or using Marshal.SizeOf(...) 0> gives me an error) So I wrote: public static class HelperMethods { static HelperMethods() { SizeOfType = createSizeOfFunc(); } public static int SizeOf<T>() { return SizeOfType(typeof(T)); } public static readonly Func<Type, int> SizeOfType = null; private static Func<Type, int> createSizeOfFunc() { var dm = new DynamicMethod("SizeOfType", typeof(int), new Type[] { typeof(Type) }); ILGenerator il = dm.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Sizeof); //needs to be

Linux网络编程二、tcp连接API

怎甘沉沦 提交于 2019-11-30 19:36:06
一、服务端   1、创建套接字:     int socket(int domain, int type, int protocol);       domain:指定协议族,通常选用AF_INET。       type:指定socket类型,TCP通信下使用SOCK_STREAM。       protocol:指定协议,通常为0。       返回值:成功则返回新socket的文件描述符,失败返回-1。       头文件:sys/socket.h sys/types.h   2、绑定套接字     int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);       sockfd:要绑定的套接字。       my_addr:本地地址,使用sockaddr_in结构体创建。       addrlen:my_addr的长度。       返回值:成功返回0,失败返回-1。   3、监听套接字     int listen(int s, int backlog);       s:要监听的套接字       backlog:指定未完成连接队列的最大长度,如果一个连接请求到达时为完成连接队列已满,那么客户端将会接收到错误。       返回值:成功返回0,失败返回-1。   4、接受连接     int

C standard regarding sizeof overflowing size_t

感情迁移 提交于 2019-11-30 19:28:47
Is this undefined behavior? The relevant parts of the standard don't say much. size_t n = SIZE_MAX / sizeof(double) + 1; size_t m = sizeof(double[n]); The C standard does not explicitly state that the size_t type is sufficient for working with the sizes of all objects or types, especially for hypothetical types that are not actually instantiated. In C 2018 7.19 2, the standard says that size_t “is the unsigned integer type of the result of the sizeof operator”. That tells us about the type size_t but not about the values that may arise during computation. In 5.2.4, the standard recognizes that

区间DP复习

允我心安 提交于 2019-11-30 19:21:09
区间DP复习 (难度排序:(A,B),(F,G,E,D,H,I,K),(C),(J,L)) 这是一个基本全在bzoj上的复习专题 没有什么可以说的,都是一些基本的dp思想 A [BZOJ1996] [Hnoi2010] chorus 合唱队 裸题 \(dp[i][j][2]\) 表示区间 \(i,j\) 最后放的是 \(i\) 还是 \(j\) 的方案数 int n; int a[N]; ll dp[N][N][2]; int main(){ rep(i,1,n=rd()) a[i]=rd(); rep(i,1,n) dp[i][i][0]=1; drep(i,n,1) rep(j,i,n) rep(k,0,1) { if(k==0) { if(i>1 && a[i-1]<a[i]) (dp[i-1][j][0]+=dp[i][j][k])%=P; if(j<n && a[j+1]>a[i]) (dp[i][j+1][1]+=dp[i][j][k])%=P; } else { if(i>1 && a[i-1]<a[j]) (dp[i-1][j][0]+=dp[i][j][k])%=P; if(j<n && a[j+1]>a[j]) (dp[i][j+1][1]+=dp[i][j][k])%=P; } } cout<<(dp[1][n][0]+dp[1][n][1])%P<<endl

C++数组与指针

主宰稳场 提交于 2019-11-30 19:04:58
不知道在通过前面的内容学习后,是否有很多小伙伴都会认为数组和指针是等价的,数组名表示数组的首地址呢?不幸的是,这是一种非常危险的想法,并不完全正确,前面我们将数组和指针等价起来是为了方便大家理解(在大多数情况下数组名确实可以当做指针使用),不至于被指针难倒,这里就请大家忘记这种观念,因为它可能将会颠覆你之前的认知。 数组和指针不等价的一个典型案例就是求数组的长度,这个时候只能使用数组名,不能使用数组指针,这里不妨再来演示一下: 运行结果: 数组是一系列数据的集合,没有开始和结束标志,p 仅仅是一个指向 int 类型的指针,编译器不知道它指向的是一个整数还是一堆整数,对 p 使用 sizeof 求得的是指针变量本身的长度。也就是说,编译器并没有把 p 和数组关联起来,p 仅仅是一个指针变量,不管它指向哪里,sizeof 求得的永远是它本身所占用的字节数。 站在编译器的角度讲,变量名、数组名都是一种符号,它们最终都要和数据绑定起来。变量名用来指代一份数据,数组名用来指代一组数据(数据集合),它们都是有类型的,以便推断出所指代的数据的长度。 对,数组也有类型,这是很多小伙伴没有意识到的!我们可以将 int、float、char 等理解为基本类型,将数组理解为由基本类型派生得到的稍微复杂一些的类型。sizeof 就是根据符号的类型来计算长度的。 对于数组 a,它的类型是int [6]

Adding a default constructor to a base class changes sizeof() a derived type [duplicate]

六眼飞鱼酱① 提交于 2019-11-30 18:54:33
This question already has an answer here: When extending a padded struct, why can't extra fields be placed in the tail padding? 4 answers I tend to think I have a pretty good grasp of C++ internals and memory layouts, but this one has me baffled. I have the following test code: #include <stdio.h> struct Foo { //Foo() {} int x; char y; }; struct Bar : public Foo { char z[3]; }; int main() { printf( "Foo: %u Bar: %u\n", (unsigned)sizeof( Foo ), (unsigned)sizeof( Bar ) ); } The output is reasonable: Foo: 8 Bar: 12 However, this is the very odd part, if I uncomment that simple default constructor

7.30

时光总嘲笑我的痴心妄想 提交于 2019-11-30 18:47:09
索引 OI赞歌 刷题表 P1010 幂次方【打表】 南蛮图腾【分治】 连续自然数的和【数学】 末日的传说 【数学】 八皇后【回溯】 加分二叉树【DP,前序遍历】 P1030 求先序排列【前序中序后序遍历】 noip原题过手 积木大赛【过水】 乘积最大【DP】 进制转换【模拟,注意负进制的特点】 P1010 幂次方 /* translation: solution: trigger: note: * date: 2019.07.30 */ #include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(ll i=a;i<=b;++i) #define dwn(i,a,b) for(ll i=a;i>=b;--i) template <typename T> inline void rd(T &x){x=0;char c=getchar();int f=0;while(!isdigit(c)){f|=c=='-';c=getchar();}while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x=f?-x:x;} #define mem(a,b) memset(a,b,sizeof(a)) #define N int n

Is sizeof(T) == sizeof(int)?

谁都会走 提交于 2019-11-30 17:50:06
I've been poring over the draft standard and can't seem to find what I'm looking for. If I have a standard-layout type struct T { unsigned handle; }; Then I know that reinterpret_cast<unsigned*>(&t) == &t.handle for some T t; The goal is to create some vector<T> v and pass &v[0] to a C function that expects a pointer to an array of unsigned integers. So, does the standard define sizeof(T) == sizeof(unsigned) and does that imply that an array of T would have the same layout as an array of unsigned ? While this question addresses a very similar topic, I'm asking about the specific case where

Does Unary + operator do type conversions?

十年热恋 提交于 2019-11-30 17:29:16
Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4 printf("%d %d %d",sizeof(+ch),sizeof(+sh),sizeof(i)); // output: 4 4 4 Does it mean + is doing type conversion here? Because it is behaving same as following printf("%d %d %d",sizeof((int)ch),sizeof((int)sh),sizeof(i)); // output: 4 4 4 This forces me to think + is doing type conversion. But then I try it on double double f; printf("%d %d",sizeof(+f),sizeof((int)f),sizeof(f)); // output:

Size of struct containing double field

 ̄綄美尐妖づ 提交于 2019-11-30 17:24:48
问题 Firstly, I understand byte padding in structs. But I have still a small test contain a double field in struct and I don't know how to explain this : typedef struct { char a; double b; }data; typedef struct{ char a; int b; }single; int main(){ printf("%d\n",sizeof(double)); printf("%d\n",sizeof(single)); printf("%d\n",sizeof(data)); } Through out this test, the answer is : 8 8 and 16 . Why this result make me thinking ? By second test, we can see size of word on my machine is 4 bytes. By first