sizeof

Why sizeof(Derived4) is 8 byte? I think it should be 5 bytes

一笑奈何 提交于 2019-12-10 20:14:26
问题 This is the output of the given program: sizeof(Empty) 1 sizeof(Derived1) 1 sizeof(Derived2) 4 sizeof(Derived3) 1 sizeof(Derived4) 8 sizeof(Dummy) 1 This is the program: #include <iostream> using namespace std; class Empty {}; class Derived1 : public Empty {}; class Derived2 : virtual public Empty {}; class Derived3 : public Empty { char c; }; class Derived4 : virtual public Empty { char c; }; class Dummy { char c; }; int main() { cout << "sizeof(Empty) " << sizeof(Empty) << endl; cout <<

C++11 之 将sizeof用于类成员

为君一笑 提交于 2019-12-10 20:04:49
sizeof运算符 (主要复习 sizeof,带一下C++11的特性) sizeof 运算符返回一条表达式或一个类型名字所占的字节数。 sizeof运算符满足右结合律, 其所得的值是一个size_t 类型(unsigned int)的常量表达式。 运算符的运算对象有两种形式: sizeof ( type ) sizeof expr 在第二种形式中,sizeof返回的是表达式结果类型的大小。与从不同的一点是, sizeof并不实际计算其运算对象的值 : Sales_data data , * p ; sizeof ( Sales_data ) ; //存储Sales_data类型的对象所占的空间大小 sizeof data ; //data 的类型的大小,即sizeof(Sales_data) sizeof p ; //指针所占的空间大小 sizeof * p ; //p 所指类型的空间大小,即sizeof(Sales_data) sizeof data . revenue ; //Sales_data的revenue 成员对应类型的大小 sizeof Sales_data :: revenue ; //另一种获取 revenue大小的方式 在sizeof的运算对象中解个无效指针仍然是一种安全的行为,因为指针实际上并没有被真正使用。 如上述代码最后一句, C+

Unexpected results for “sizeof” of strings

半世苍凉 提交于 2019-12-10 18:55:44
问题 Why would sizeof in the following cases print different values: printf("%d",sizeof("ab")); //print 3 char* t="ab"; printf("%d",sizeof(t)); //print 4 In the first case I have 2 characters... Shouldn't sizeof print 2? Because they are 2 bytes? 回答1: Strings in C are null terminated. "ab" in memory looks like 'a' 'b' '\0' While t is a pointer, so size is 4. 回答2: t is a pointer to an array containing "ab" . Its size is the size of a pointer. "ab" is an array containing "ab" . Its size is the size

Struct size containing vector<T> different sizes between DLL and EXE

久未见 提交于 2019-12-10 18:32:31
问题 I have this situation where an EXE program imports a DLL for a single function call. It works by passing in a custom structure and returning a different custom structure. Up till now it's worked fine until I wanted one of the structs data members to be a vector < MyStruct > When I do a sizeof(vector< MyStruct >) in my program I get a size of 20 but when I do it from inside the DLL I get a size of 24. This size inconsistency is causing a ESP pointer error. Can anyone tell me why a Vector <

Guarantee on size ordering on char, wchar_t, char16_t, char32_t

一笑奈何 提交于 2019-12-10 18:05:38
问题 Does the C++ standard provide any guarantee on the ordering of the size in bytes of char , wchar_t , char16_t , char32_t ? (any extract from the standard is welcome) For example do I have the guarantee that: sizeof(char) <= sizeof(wchar_t) <= sizeof(char16_t) <= sizeof(char32_t) 回答1: It's 1 == sizeof(char) <= sizeof(wchar_t) and 1 == sizeof(char) <= sizeof(char16_t) <= sizeof(char32_t) . 5.3.3/1 Sizeof [expr.sizeof] ... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. ... [

我的梦境 提交于 2019-12-10 17:36:05
图: 邻接矩阵 MGraph *G int j; for(j=0;j<n;j++) if(G->a[i][j]!=0) printf("%d",j); //邻接表 LGragh *G ENode *p=G->a[i]; while(p){ printf("%d",p->adjVex); p=p->nextArc; } //图的深度优先遍历(邻接表) void DFS(LGraph *G,int i,int visited[]) { printf("%d",i)//访问节点 visited[i]=1;//设置结点i已访问 Enode *p=G->a[i];//获取顶点i的邻接链表的头结点 while(p){ //依次考察i的邻接链表中的每一个结点 if(!visited[p->adjVex]) //如果该结点表示的顶点未被访问,则对该结点执行DFS DFS(G,p->adjVex,visited) } void DFS_Graph(LGragh *G) { int i; int *visited=(int*)malloc(G->n*sizeof(int);//创建顶点是否已访问标记数组 for(i=0;i<G->n;i++)//初始化visited数组,设置均未被访问 visited[i]=0; for(i=0;i<G->n;i++)//防止存在图不连通的情况

setsockopt 设置socket

北城余情 提交于 2019-12-10 16:06:19
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()的时候,返回的是实际发送出去的字节(同步

罗德里格斯公式

风流意气都作罢 提交于 2019-12-10 15:49:58
在三维空间中,给定一固定旋转轴,任意初始向量绕旋转轴旋转任意角度,可表示为 ,其中,v 表示旋转前向量, 表示旋转后向量,R 表示旋转矩阵,该矩阵参数与固定旋转轴坐标,旋转角度有关。下面使用图示推导旋转矩阵 R: 如上图所示,单位向量 为固定旋转轴,原向量 v 旋转 后为 ; 分解原向量 ,向量 v 与向量 k(或者 ) 的夹角为 ,该值已知; 向量 构成直角三角形,则有: , ; 向量叉乘 ,其方向垂直于 构成的平面,w 的模长为 ,则有 ,且两向量正交; 以上向量 构成三维空间已知正交向量基, 可表示: ,在 向量构成平面上, 旋转 后为: , ,由于 ,进一步化简为: ; 定义向量 ,其方向与 平行,其模长为 ,则 ; 引入向量 的叉积矩阵 ,叉积运算可转换为矩阵运算 ; 改写 线性组合 ,引入叉积矩阵 K得: , 则旋转矩阵 。 以下给出代码实现: 1 void GetRotateMatrix(Matrix<float>& Q, Vector<float>& axis, float theta) 2 { 3 // 使用罗德里格斯公式(Rodriguez formula) 4 5 // 构造单位向量 6 float len = sqrt(axis.data[0] * axis.data[0] + 7 axis.data[1] * axis.data[1] + axis

assignment inside sizeof function in c [duplicate]

一世执手 提交于 2019-12-10 15:46:03
问题 This question already has answers here : Why does sizeof(x++) not increment x? (9 answers) Closed 5 years ago . foo(a = b+c); //new value of a(after the call) = b+c //but sizeof(a = b+c); //new value of a = old value of a Why isn't the the result of the assignment statement reflected in the stack of the function( which contains the above code) in the latter case? 回答1: sizeof is an operator not a function. Operand of sizeof is not evaluated except when it is a variable length array. C11: 6.5.3

setsockopt用法详

左心房为你撑大大i 提交于 2019-12-10 15:02:06
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()