sizeof

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

北慕城南 提交于 2019-11-30 01:45:18
问题 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

Why is int typically 32 bit on 64 bit compilers?

守給你的承諾、 提交于 2019-11-30 01:26:53
Why is int typically 32 bit on 64 bit compilers? When I was starting programming, I've been taught int is typically the same width as the underlying architecture. And I agree that this also makes sense, I find it logical for a unspecified width integer to be as wide as the underlying platform (unless we are talking 8 or 16 bit machines, where such a small range for int will be barely applicable). Later on I learned int is typically 32 bit on most 64 bit platforms. So I wonder what is the reason for this. For storing data I would prefer an explicitly specified width of the data type, so this

How can this structure have sizeof == 0?

百般思念 提交于 2019-11-30 01:07:17
There is an old post asking for a construct for which sizeof would return 0 . There are some high score answers from high reputation users saying that by the standard no type or variable can have sizeof 0. And I agree 100% with that. However there is this new answer which presents this solution: struct ZeroMemory { int *a[0]; }; I was just about to down-vote and comment on it, but time spent here taught me to check even the things that I am 100% sure on. So... to my surprise both gcc and clang show the same results: sizeof(ZeroMemory) == 0 . Even more, sizeof a variable is 0 : ZeroMemory z{};

Why do I get different results when I apply sizeof operator?

醉酒当歌 提交于 2019-11-30 00:31:53
问题 I have this program #include <stdio.h> int main() { char arr[100]; printf("%d", (int)sizeof(0,arr)); } This prints 4 when compiled as a C file and prints 100 as a C++ file. Why? I am using gcc. 回答1: In C the result of the right hand operand of the comma operator has a type and value . In C a comma operator does not yield an lvalue. So there is an lvalue to rvalue conversion resulting in decay of array type to pointer type. So in C what you get is the result of sizeof(char*) . In C++ the

C++数值类型的取值范围

天大地大妈咪最大 提交于 2019-11-30 00:24:05
//C++数值类型的取值范围 #include "stdio.h" #include "iostream" using namespace std; int main() { char a1=-128;//-128~+127 short b1=-32768;// -32768~+32767 int c1=-2147483648;//-2147483648~+2147483647 long d1=-2147483648;//-2147483648~+2147483647 long long x1=-9223372036854775808;//-9223372036854775808~9223372036854775807 //以下取-1转化后为最大值 unsigned char a2=-1;//0~255 unsigned short b2=-1;// 0~65535 unsigned int c2=-1;//0~4294967295 unsigned long d2=-1;//0~4294967295 unsigned long long x2=-1;//0~18446744073709551615 float e=100/3.0;//10e±38,6位有效数字-7 double f=100.0/3;//10e±308,12位有效数字-16 long double g=100.0

bzoj 2208 //2208: [Jsoi2010]连通数

大憨熊 提交于 2019-11-29 23:44:36
bzoj 2208 //2208: [Jsoi2010]连通数 //在线测评地址 https://www.lydsy.com/JudgeOnline/problem.php?id=2208 更多题解,详见 https://blog.csdn.net/mrcrack/article/details/90228694 BZOJ刷题记录 //2208: [Jsoi2010]连通数 //在线测评地址https://www.lydsy.com/JudgeOnline/problem.php?id=2208 //第一个思路,Floyd算法,求最短路径,算法的时间复杂度O(n^3),看了数据范围 N不超过2000,只好作罢 //统计 连通数 时,采用O(n^2)算法即可 //问题是,如何确认2点连通,这个算法的时间复杂度,如何降下来。 //多点的连通问题。 //强连通分量及缩点tarjan算法解析https://blog.csdn.net/acmmmm/article/details/16361033可看此文,手绘图。 //初探tarjan算法(求强连通分量) https://www.luogu.org/blog/styx-ferryman/chu-tan-tarjan-suan-fa-qiu-qiang-lian-tong-fen-liang-post 此文值得一看 /

P1398 [NOI2013]书法家

折月煮酒 提交于 2019-11-29 23:18:38
传送门 就是个普及组 $dp$ 合集,把 $NOI$ 从左到右拆成 $9$ 个部分,每个部分都可以分别 $dp$ 除了 $N$ 的中间部分比较恶心以外其他都还好,自己推一下然后就知道转移,就 $N$ 的中间优化转移比较不好写 随便吧,反正 $9$ 个 $dp$ 都挺简单的, 量变导致质变 ,我在想那一年的选手是不是都被恶心到了...反正我是被恶心死了 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long ll; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const int N=157,M=507; int n,m,sum[N][M],ans; int f[2][M][N][N],mx[M][N][N],mxx,mxxx[N][N]; inline

What's the difference between sizeof and alignof?

六眼飞鱼酱① 提交于 2019-11-29 22:50:50
What's the difference between sizeof and alignof? #include <iostream> #define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl int main(int, char**) { SIZEOF_ALIGNOF(unsigned char); SIZEOF_ALIGNOF(char); SIZEOF_ALIGNOF(unsigned short int); SIZEOF_ALIGNOF(short int); SIZEOF_ALIGNOF(unsigned int); SIZEOF_ALIGNOF(int); SIZEOF_ALIGNOF(float); SIZEOF_ALIGNOF(unsigned long int); SIZEOF_ALIGNOF(long int); SIZEOF_ALIGNOF(unsigned long long int); SIZEOF_ALIGNOF(long long int); SIZEOF_ALIGNOF(double); } will output 1/1 1/1 2/2 2/2 4/4 4/4 4/4 4/4 4/4 8/8 8/8 8/8 I think I don't

Size of int and sizeof int pointer on a 64 bit machine

浪尽此生 提交于 2019-11-29 21:07:16
I was just wondering how can I know if my laptop is 64 or 32 bit machine. (it is a 64). So, I thought about printing the following: int main() { printf("%d",sizeof(int)); } and the result was 4, which seemed weird (since it is a 64 bit machine) But, when I printed this: int main() { printf("%d",sizeof(int*)); } the result was 8, which made more sense. The question is: Since I'm using a 64 bit machine, shouldn't a primitive type such as int should use 8 bytes (64 bit) and by that sizeof int should be 8? Why isn't it so? And why is the int* size is 8? A bit confused here, so thanks in advance.

秋招C++开发学习之路day3

a 夏天 提交于 2019-11-29 20:49:26
day3(list、string、str.copy、vector、reserve、resize、static、数组和指针区别) 0. list是双向链表实现的,所以也是一段不连续的内存存储。 string类的底层是一个字符串指针。对字符串的操作和字符数组的操作是不一样的。C++字符串转换成C字符数组,常用的data()、c_str()、copy();最好不用C指针来存放转换,如:const char * cstr=str . c_str(); 改变str会改变cstr的值。所以考虑复制的形式,把转换后的从c_str中复制出来然后赋值给C字符串。 strncpy(str1,str2,int n)把str2的n个字符复制到str1; str.copy(p,n,m)表示至少复制n个数据到p所指向的空间,从m个字符开始。 string::npos 表示机器的最大正整数,64 位18446744073709551615 ,32 位4294967295 强制转换成int之后就是 -1 ,m没有值则默认为0,也就是第一个字符开始。 int snprintf(a,n,格式,…),例如:int i = snprintf(a , 9 ,” %012d ”, 12345) ; print(“i=%d,a=%s”,i,a),结果是 12 00000001;%012d就是按int类型12为输出