sizeof

C++ Primer Plus(四)——复合类型

余生颓废 提交于 2019-12-02 20:15:29
只能在定义数组时才能初始化,不能将一个数组赋值给另一个数组,但可以使用下标分别赋值给数组元素,但可以将一个string对象赋值给另一个string对象 如果只对数组的一部分初始化,其他元素自动设置为0 C++11可在初始化的大括号里不包含任何东西,这将把所有元素设置为0 C++11在数组列表初始化时,禁止缩窄转换 C-风格字符串以\0结尾,不是\0结尾的字符数组不是字符串 任何两个由空白(空格,制表符,换行符)分隔的字符串常量都将自己拼成一个 sizeof运算符指出整个数组的长度,而strlen()指出存储在数组中的字符串的长度 cin使用 空白(空格,制表符,换行符)确定字符串的结束位置,面向单词 cin的get( )函数和getline( )函数,面向行: getline( )函数丢弃换行符,在读取指定数目-1的字符或遇到换行符时停止读取 get( )函数的一种变体和getline函数参数相同,但保留换行符,在再次读取字符时要先使用无参数的get()函数读取换行符后才能正确读取字符,再使用clear( )函数来恢复输入 可以使用C-风格字符串来初始化string对象,使用cin来将键盘输入存储到string对象中,使用cout来显示string对象,使用数组表示法来访问存储在string对象中的字符 C++将"(和)"作为界定符, 使用前缀R 来标识原始字符串(不转义)

eventfd(2) 结合 select(2) 分析

好久不见. 提交于 2019-12-02 18:46:18
本文代码选自内核 4.17 eventfd(2) - 创建一个文件描述符用于事件通知。 使用 源码分析 参考 #include <sys/eventfd.h> int eventfd(unsigned int initval, int flags); int eventfd2(unsigned int initval, int flags); 参数 - \initval 为初始值(关联内部结构的 count) - \flags 内核 2.6.26 之前的版本这个参数无效且必须指定为 0 flags 有意义的参数为 - EFD_CLOEXEC, 等效于 O_CLOEXEC - EFD_NONBLOCK, 等效于 O_NONBLOCK - EFD_SEMAPHORE, 信号量选项,影响 read(2) 的取值 返回 - 成功返回一个新的文件描述符,失败返回 -1 并设置 errno eventfd 作为一个非常简单的抽象文件,每个文件描述符都对应一个在内核空间维护的 __u64 count , 一个无符号64位整形的计数器,而eventfd对应的文件操作都与这个计数器相关。 提供的文件操作 read(2), 读取 count 减少的值,若flags设置 EFD_SEMAPHORE 则 count -= 1 , 否则 count -= count ; 函数成功返回 8 write(2),

Size of class with virtual function

拟墨画扇 提交于 2019-12-02 18:19:11
问题 I was revising the C++ concepts, but I am stuck with a very simple code #include <iostream> using namespace std; class foo { public: //int i; void virtual foobar() { cout << "foobar\n"; } }; int main() { foo f; cout << sizeof(f) << endl; //cout << sizeof(f.i) << endl; return 1; } The output of the above code is 8 But when I removed comments from the code Output is 16 and 4 I did not understand when the class have no member variable present then VPTR size is 8 but after adding a variable size

Best practice for getting datatype size(sizeof) in Java

随声附和 提交于 2019-12-02 18:09:50
I want store a list of doubles and ints to a ByteBuffer, which asks for a size to allocate. I'd like to write something like C's syntax int size=numDouble*sizeof(double)+numInt*sizeof(int); But there is no sizeof in Java. What is the best practice to calculate the size in byte? Should I hardcode it? Ed Staub (If you're using Java 8 or beyond, be sure to look at @Frank Kusters' answer !) All of the primitive wrappers have a SIZE constant, which is in bits, not bytes. So in the example given, it would be: int size=(numDouble*Double.SIZE+numInt*Integer.SIZE) / Byte.SIZE; Or, if you wanted to

Sizeof struct in Go

帅比萌擦擦* 提交于 2019-12-02 18:06:44
I'm having a look at Go, which looks quite promising. I am trying to figure out how to get the size of a go struct, for example something like type Coord3d struct { X, Y, Z int64 } Of course I know that it's 24 bytes, but I'd like to know it programmatically.. Do you have any ideas how to do this ? import unsafe "unsafe" /* Structure describing an inotify event. */ type INotifyInfo struct { Wd int32 // Watch descriptor Mask uint32 // Watch mask Cookie uint32 // Cookie to synchronize two events Len uint32 // Length (including NULs) of name } func doSomething() { var info INotifyInfo const

Why sizeof() of a string variable always return the same number even when content changes?

人走茶凉 提交于 2019-12-02 17:01:12
问题 This is a rather simple problem but is pretty confusing. string R = "hhhh" ; cout<< sizeof( R )<<endl; OUTPUT: 4 Variation: string R = "hhuuuuuuhh" ; cout<< sizeof( R )< OUTPUT2: 4 What is going wrong ? Should I use char array instead ? 回答1: Think of sizeof being compile-time evaluable. It evaluates to the size of the type, not the size of the contents. You can even write sizeof(std::string) which will be exactly the same as sizeof(foo) for any std::string instance foo . To compute the number

点分治

被刻印的时光 ゝ 提交于 2019-12-02 16:56:36
1.求<=k点对数,容斥法 /* 求树中距离不超过k的点对数 暴力枚举两点,lca求的复杂度是O(n^2logn),这样很多次询问都是冗余的 那么选择重心作为根,问题分成两部分,求经过重心的距离<=k的点对+不经过重心的距离<=k的点对 先来求第一部分,计算所有点的深度,排序,O(nlogn)可以计算出距离<=k的过重心点对 但是这样还不是正确答案,因为还要容斥掉来自同一棵子树的非法点对,那么对这部分再算一次即可 再求第二部分,这部分其实等价于原问题的子问题,所以我们再去重心的每个子树里找重心,和上面一样求 如果一个点已经被当过重心了,那么给它打个vis标记,之后不再访问 这样最多递归O(logn) 次,所以总复杂度是O(n*logn*logn) */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 10005 using namespace std; struct Edge{int to,nxt,w;}e[N<<1]; int head[N],tot,n,k,ans; void add(int u,int v,int w){ e[tot].to=v;e[tot].w=w;e[tot].nxt=head[u];head[u]=tot++; } int vis[N]

( 图论专题 )【 最短路 】

橙三吉。 提交于 2019-12-02 15:13:12
( 图论专题 )【 最短路 】 dijkstra的堆优化(最简版本) 先了解不用堆优化的dijkstra:https://blog.csdn.net/weixin_43828245/article/details/90722389 推荐视频讲解(代码是Python写的,重点听思路):https://www.bilibili.com/video/av25829980 了解c++优先队列:https://blog.csdn.net/weixin_43828245/article/details/90742490 #include <iostream> #include <queue> #include <cstring> #define inf 0x3f3f3f using namespace std; typedef struct node { int v,date; } ty; bool operator < ( const ty &a, const ty &b ) // 自定义优先队列规则,先pop出小的来 { return a.date>b.date; // 这里和sort相反,a>b是先pop出小的 } int a[2002][2002]; int via[2002]; // 判断是否已经pop出来了 int dis[2002]; // 存放距离 int i,j,n,m;

Why does “sizeof(a ? true : false)” give an output of four bytes?

岁酱吖の 提交于 2019-12-02 14:46:16
I have a small piece of code about the sizeof operator with the ternary operator: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); // Ok printf("%zu\n", sizeof(a)); // Ok printf("%zu\n", sizeof(a ? true : false)); // Why 4? return 0; } Output ( GCC ): 1 1 4 // Why 4? But here, printf("%zu\n", sizeof(a ? true : false)); // Why 4? the ternary operator returns boolean type and sizeof bool type is 1 byte in C. Then why does sizeof(a ? true : false) give an output of four bytes? It's because you have #include <stdbool.h> . That header defines macros

C++ sizeof C-style string / char array - optimization

爷,独闯天下 提交于 2019-12-02 13:58:32
I'm a student at university. I work mostly with Java, C++ is very new to me, so I probably make many silly mistakes and I have upcoming exams to cope with. Don't be too harsh with me. Note: I can NOT use C++ std::string because I need to work with C-strings due to university tasks! Referring to my studies and the question I asked about pointers and const arguments (which you find here ) I tried messing around with memory management but it seems it has no effect, or I just misunderstood some aspects about sizeof or actual sizes of certain elements. This is my class Person: Person.cpp using