sizeof

Please explain the output of sizeof()

孤街醉人 提交于 2019-12-01 06:40:40
问题 I am new in c programming, and I am trying to understand the sizeof function. Please help me understand how this program works: #include<stdio.h> main( ) { printf ( "\n%d %d %d", sizeof ( '3'), sizeof ( "3" ), sizeof ( 3 ) ) ; } I am getting output as 4 2 4 . However, I am not able to understand the reason I get this output. Kindly explain it. 回答1: sizeof ( '3') , it is the size of character constant which is int so you are getting value as 4 on your machine. sizeof ( "3" ) , it is size of

size of a structure containing bit fields [duplicate]

无人久伴 提交于 2019-12-01 06:34:45
Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8 bytes. CASE I: struct B { unsigned char c; // +8 bits } b; sizeof(b); // Output: 1 (because unsigned char takes 1 byte on my system) CASE II: struct B { unsigned b: 1; } b; sizeof(b); // Output: 4 (because unsigned takes 4 bytes on my system) CASE III: struct B { unsigned char c; // +8 bits unsigned b: 1; // +1 bit } b; sizeof(b); // Output: 8 I don't

2019 ACM/TINA实验室10.12训练赛

跟風遠走 提交于 2019-12-01 06:08:53
A CodeForces 1238A Prime Subtraction 水题,除了差1都行。 #include<stdio.h> #include<set> #include<iostream> #define mem(ss) memset(ss,0,sizeof(ss)) #define rep(d, s, t) for(int d=s;d<=t;d++) #define rev(d, s, t) for(int d=s;d>=t;d--) typedef long long ll; typedef long double ld; typedef double db; const ll mod = 998244353; const int N = 1e4 + 10; #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; ll T,x,y; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld%lld",&x,&y); if(x-y==1){ printf("NO\n"); } else{ printf("YES\n"); } } return 0; } B CodeForces 1238B Kill 'Em

tcp与串口透传

ⅰ亾dé卋堺 提交于 2019-12-01 05:29:31
介绍 tcp作为服务端,监听端口8888,实现串口透传,这里是使用select监听tcp的receive和串口的read,单工通信 程序 #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <linux/serial.h> #include <sys/ioctl.h> #include <sys/select.h> #include <termios.h> void Test_err(int flg) { if(flg<0) { perror(strerror(errno)); exit(1); } } int main(int argc, char** argv) { printf("usage : %s <port,default is 8888>\n",argv[0]); printf("tcp-server...enter\n"); unsigned char* uart_port="/dev

Can there be a C++ type that takes 0 bytes

久未见 提交于 2019-12-01 05:25:26
I'm trying to declare a C++ variable that takes up zero bytes. Its in a union, and I started with the type as int[0]. I don't know if that is actually zero bytes (although sizeof(int[0]) was 0). I need a better way to declare a 0 byte type, and hopefully one that can be typedefed to something like nullType or emptyType. The variable is in a union, so in the end memory is reserved anyway. I tried void on the off chance it would work, but C++ complained. I'm using Ubuntu 10.10, with a current version of the kernel, and an up-to-date GCC. Here's the union: union RandomArgumentTypesFirst { uint

图论2 1010

♀尐吖头ヾ 提交于 2019-12-01 04:56:37
最小生成树 给你一个无向带权连通图,每条边是黑色或白色。让你求一棵恰好有need条白色边的权值和最小的生成树。题目保证有解。 对于所有数据,V,E<=100000,c为[1,1000]中的正整数。 题解 可以知道恰好选到need条白边就是最优的,考虑给所有白边加上一个值,随着值的增大,在生成树中的白边越少,所以可以二分。 取最后一次满足条件的值。 排序的时候,对于相同值的边白边优先。 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int maxn=100005; int n,m,need; int fa[maxn],sum,cnt; struct edge{ int x,y,c,val; bool operator < (const edge a) const { if(val==a.val) return c<a.c; return val<a.val; } }e[maxn],cy[maxn]; template<class T>inline void read(T &x){ x=0;int f=0;char ch=getchar(); while(!isdigit(ch)) {f|=(ch=='-');ch=getchar();} while(isdigit

Why is the size of a class object different based on order of members?

蹲街弑〆低调 提交于 2019-12-01 04:23:03
问题 class CHaraICICCC { int i; char c1; int j; char c2; char c3; char c4; }; class CHaraIICCCC { int i; int j; char c1; char c2; char c3; char c4; }; void fun() { CHaraICICCC eici; CHaraIICCCC eiicc; int icic = sizeof(eici); // -> output of icic is 16. int iicc = sizeof(eiicc); // -> output of icic is 12. } If any one knows, Please let me know why like this. Thanks Hara 回答1: Because of alignment. x86 compilers tend to align int types on 4 bytes boundary (for faster memory access) so CHaraICICCC

VB.NET and sizeof

為{幸葍}努か 提交于 2019-12-01 04:04:35
I'm converting some code from C# to VB.NET. I have the following line in C# var bytes = new byte[password.Length * sizeof(char)]; Looking on MSDN it appears that VB.NET does not seem to have the sizeof operator. I understand there is a Marshal.SizeOf but further MSDN documentation states that the value returned can be different to that of sizeof . Can anybody help? Is there an equivalent statement in VB.NET? Additional Information My aim is to convert a password into an array of bytes which I can then hash and then either store in a database or compare to a previously stored hash. But I don't

Force Specific Struct Size in C

情到浓时终转凉″ 提交于 2019-12-01 03:36:29
For various reasons, I have some structs I want to force to be specific sizes (in this case 64 bytes and 512 bytes). Both however, are below the somewhat below the sizes I want them to be. Is there anyway for me to tell the compiler to set them to these specific sizes and pad with zeros, or would I be best off just declaring an array inside the struct that makes up the excess space so that it aligns on the size I want? You can use a union. struct mystruct_s { ... /* who knows how long */ }; typedef union { struct mystruct_s s; unsigned char padding[512]; } mystruct; This will ensure the union

size of a structure containing bit fields [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-01 03:26:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8 bytes. CASE I: struct B { unsigned char c; // +8 bits } b; sizeof(b); // Output: 1 (because unsigned char takes 1 byte on my system) CASE II: struct B { unsigned b: 1; } b; sizeof(b); //