sizeof

In C++, why does a derived class that just contains a union with an instance of its base class take more memory than the size of the union?

人盡茶涼 提交于 2019-12-23 09:19:54
问题 More specifically, a class, inheriting from an empty class, containing just a union whose members include an instance of the base data-less class, takes up more memory than just the union. Why does this happen and is there any way to avoid spending the extra memory? The following code illustrates my question: #include <iostream> class empty_class { }; struct big : public empty_class { union { int data[3]; empty_class a; }; }; struct small { union { int data[3]; empty_class a; }; }; int main()

What is sizeof(something) == 0?

不羁岁月 提交于 2019-12-23 09:05:36
问题 I have a template that takes a struct with different values, for example: struct Something { char str[10]; int value; ... ... }; And inside the function I use the sizeof operator: jump in memory sizeof(Something); Sometimes I would like to not jump anything at all; I want sizeof to return zero. If I put in an empty struct it will return 1; what can I put in the template to make sizeof return zero? 回答1: sizeof will never be zero. (Reason: sizeof (T) is the distance between elements in an array

linux网络编程之bind函数

与世无争的帅哥 提交于 2019-12-23 03:45:49
NAME bind - bind a name to a socket SYNOPSIS #include <sys/socket.h> int bind(int socket, const struct sockaddr *address, socklen_t address_len); DESCRIPTION The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets created with the socket() function are initially unnamed; they are identified only by their address family. The bind() function takes the following arguments: socket Specifies the file descriptor of the socket to be bound. address Points to a sockaddr structure containing the

Linux网络通信

孤街醉人 提交于 2019-12-23 03:45:23
使用TCP协议的socket 1.网络字节序 由于在主机存储为小端序,网络传输为大端序,并且在网络中需要读取IP号和端口号,所以发送端要将小端序转为大端序,接收端将大端序转为小端序 #include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); 表示host,n表示network,l表示32位长整数,s表示16位短整数。 2. IP地址转换函数 #include <arpa/inet.h> int inet_pton(int af, const char *src, void *dst); const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); 3.构造一个sockaddr struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl

socket编程中服务器端常用函数 以及简单实现

爷,独闯天下 提交于 2019-12-23 03:45:12
1 常用函数 1.1 connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen);   客户端需要调用connect()连接服务器,connect和bind的参数形式一致,区别在于bind的参数是自己的地址,而connect的参数是对方的地址。connect()成功返回0,出错返回-1,程序会阻塞。 1.2 bind():很少用   由于客户端不需要固定的端口号,因此不必调用bind(),客户端的端口号由内核自动分配。注意,客户端不是不允许调用bind(),只是没有必要调用bind()固定一个端口号,服务器也不是必须调用bind(),但如果服务器不调用bind(),内核会自动给服务器分配监听端口,每次启动服务器时端口号都不一样,客户端要连接服务器就会遇到麻烦。 2 客户端的简单实现(C语言) 功能:连接到服务器后,如果服务器发送数据,则返回服务器发送的数据 unsigned char TcpRecvBuf[1520] /* 缓存区,完整的以太帧最大也就1520或1518字节,如果去掉协议部分则可以更小 */ static void Task_TCP_Client (void *pdata) { struct sockaddr_in server, client; int

socket基本使用

我只是一个虾纸丫 提交于 2019-12-23 03:44:22
UDP发送和接收 MainRecv.cpp #include <iostream> #include <WinSock2.h> #include <sstream> #pragma comment(lib,"ws2_32.lib") #define RECV_IP "127.0.0.1" #define RECV_PORT 8899 #define LEN_RECV_BUF 2048 int main() { std::ostringstream ossTemp; WSAData wsaData; std::cout<<"Start..."<<std::endl; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET sktRecv=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrRecv; addrRecv.sin_family=AF_INET; addrRecv.sin_addr.s_addr=inet_addr(RECV_IP); //htonl(INADDR_ANY); addrRecv.sin_port=htons(RECV_PORT); int retVal; retVal=bind(sktRecv,(sockaddr*)&addrRecv,sizeof(addrRecv))

sizeof(*this) and decltype(*this) in derived classes

孤人 提交于 2019-12-22 10:45:39
问题 Suppose there are classes: struct A { int a; virtual size_t GetMemoryUsage() const { return sizeof(*this); } }; struct B : public A { int b; }; And there may be deeper inheritance. What I want is to have a method which will return the number of bytes an object occupies in memory, GetMemoryUsage() in this case. Usually it can be achieved by using sizeof(*this) . The problem is (at least AFAIU) that I have to override the method in each derived class and actually copy-paste its body. I don't

有限状态机(FSM)的设计与实现(二)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 09:28:03
分层状态机的设计: 对于状态较多的状态机,通常的设计会维护一个庞大的二维矩阵,所有状态耦合在一起,这往往导致维护困难,由于可能存在许多公共的特性,也会导致许多状态具有相同的处理函数。针对这些问题我们可以通过设计分层状态机来解决,主要的思想就是根据不同的功能模块设计出多个状态机,各个状态机分布在不同的层次上。上层状态机调用下层状态机时,上层状态机入栈,下层状态机变为当前处理状态机。通常我们使用堆栈来保存当前状态机的上层状态机信息。 下图描述一个分层状态机设计实现: 如上图所示,假设L1为上层状态机,L1状态机在L1_STATE2中可以通过L1L2_EVENT1事件触发进入L2状态机,L2状态机在L2_STATE2中通过L1L2_EVENT2事件触发返回L1状态机,L1和L2各自维护自己的状态表。 数据结构: struct FSM_S { int curState; //当前状态机状态 int curFsmTableSize; //当前状态机查询表大小 STATE_TABLE_T* curFsmTable; //当前状态机查询表 FSM_STACK_T stack[MAX_FSM_STACK_DEP];//状态机堆栈 int curStackTop; //栈顶 FSM_REGIST_T registFsm[MAX_FSM_NUM]; //注册状态机 int registFsmNum;

Size of packed struct with union of bit fields less than 8 bits in C

筅森魡賤 提交于 2019-12-22 08:42:18
问题 Is it possible in C to get the size of the following structure to be 2? #include <stdio.h> struct union_struct { char foo; char bar : 2; union { char foobar1 : 6; char foobar2 : 6; }; }; int main(void) { printf("size of union struct: %d\n", sizeof(struct union_struct)); return 0; } output, compiled with gcc: size of union struct: 3 回答1: If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently: #ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED

链表的常见操作

雨燕双飞 提交于 2019-12-22 05:32:56
  链表是数据结构的重要内容,在计算机程序中应用广泛,同时也是各公司笔试题目的重点。   以下简单实现了链表的一些操作,包括创建、增加节点、删除节点、单链表逆置、合并有序链表等。 一、链表创建   链表主要有三种形式,包括单链表、双链表和循环链表。   单链表每个节点只包含一个后驱指针,双链表节点同时包含一个前驱指针和一个后驱指针,循环链表的尾节点的后驱指向头节点。   代码如下: /*单链表节点结构*/typedef struct NodeType{ char elem; NodeType *next;}Node;/*双链表节点结构*/typedef struct DNodeType{ char elem; DNodeType *next; DNodeType *prev;}DNode; 创建单链表 /*创建链表*/Node * CreateList(Node *head){ if(NULL == head)//分配头节点空间 head=(Node*)malloc(sizeof(Node)), head->next=NULL; Node *current=head , *temp; char ch; while(1) { cout<<"\n input elem:"; cin>>ch; if('#' == ch) /*#结束输入*/ break; temp=(Node *)