sizeof

what's the mechanism of sizeof() in C/C++?

有些话、适合烂在心里 提交于 2019-11-28 08:42:19
It seems sizeof is not a real function? for example, if you write like this: int i=0; printf("%d\n", sizeof(++i)); printf("%d\n", i); You may get output like: 4 0 And when you dig into the assemble code, you'll find sth like this: movl $4, %esi leaq LC0(%rip), %rdi xorl %eax, %eax call _printf So, the compiler put directly the constant "4" as parameters of printf add call it. Then what does sizeof do? You know, there's a reason why there are standard documents (3.8MB PDF) ; C99, section 6.5.3.4, §2: The sizeof operator yields the size (in bytes) of its operand, which may be an expression or

关于Tinyhttpd最全注释解析

我是研究僧i 提交于 2019-11-28 08:37:39
#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <ctype.h> #include <strings.h> #include <string.h> #include <sys/stat.h> #include <pthread.h> #include <sys/wait.h> #include <stdlib.h> #include <stdint.h> #include "/usr/include/mysql/mysql.h" #define ISspace(x) isspace((int)(x)) #define SERVER_STRING "Server: zkphttpd/0.1.0\r\n" #define STDIN 0 #define STDOUT 1 #define STDERR 2 void search_mysql(int, char*); void accept_request(void *); void bad_request(int); void cat(int, FILE *); void cannot_execute

[BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化)

梦想的初衷 提交于 2019-11-28 08:30:37
[BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化) 题面 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街。商店街有n个商店,并且它们之间的道路构成了一颗树的形状。 第i个商店只卖第i种物品,小苗对于这种物品的喜爱度是wi,物品的价格为ci,物品的库存是di。但是商店街有一项奇怪的规定:如果在商店u,v买了东西,并且有一个商店w在u到v的路径上,那么必须要在商店w买东西。小葱身上有m元钱,他想要尽量让小苗开心,所以他希望最大化小苗对买到物品的喜爱度之和。这种小问题对于小葱来说当然不在话下,但是他的身边没有电脑,于是他打电话给同为OI选手的你,你能帮帮他吗? 分析 由题意,如果在u,v买了东西,那u到v的路径上的所有点一定都得买。因此最后购买东西的点一定是一个联通块。显然这个联通块一定包含某个子树的重心,所以可以点分治。 当我们点分治到某个重心rt的时候,我们考虑对这个重心分治出来子树计算背包。显然子树中的一个节点被选,它的父亲也一定被选。那么我们就把问题转化成了一般的树形背包问题,注意结点x必须选。 那么就可以列出模板的dp方程。设 \(dp[i][j]\) 表示当前处理到dfs序为i的节点x,当前背包内物品价格不大于j,能得到的最大喜爱度。设x子树的下一个子树dfs序的开始位置为nex[x].x为dfs序i对应的节点 则 \(dp[i]

Why does sizeof(x++) not increment x?

安稳与你 提交于 2019-11-28 07:55:25
问题 Here is the code compiled in dev c++ windows: #include <stdio.h> int main() { int x = 5; printf("%d and ", sizeof(x++)); // note 1 printf("%d\n", x); // note 2 return 0; } I expect x to be 6 after executing note 1 . However, the output is: 4 and 5 Can anyone explain why x does not increment after note 1 ? 回答1: From the C99 Standard (the emphasis is mine) 6.5.3.4/2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The

how to determine sizeof class with virtual functions?

落爺英雄遲暮 提交于 2019-11-28 07:50:05
this is kind of homework question. For the following code, #include <iostream> using namespace std; class A { public: virtual void f(){} }; class B { public: virtual void f2(){} }; class C: public A, public B { public: virtual void f3(){} }; class D: public C { public: virtual void f4(){} }; int main() { cout<<sizeof(D)<<endl; } The output is: 8 Could anyone please explain how it is 8 bytes? If the vtable implementation is compiler dependent, what should I answer for this kind of question in interviews? What about virtual base classes? EDIT: i am working on a 32-bit platform. This is of course

C语言常见排序之冒泡排序

社会主义新天地 提交于 2019-11-28 07:23:32
  #include<stdio.h> void BubbleSort(int *Array,int Length) { int i= 0,j = 0,temp = 0; for(i = 0; i < Length - 1; i++) { for(j = 0; j < Length-i-1; j++) {   if(Array[j] > Array[j+1])   {    temp = Array[j];    Array[j] = Array[j+1];    Array[j+1] = temp;   } } } } int main(void) { int i = 0; int ArrayBuf[5] = {2,1,0,4,3}; BubbleSort(&ArrayBuf[0],sizeof(ArrayBuf)/sizeof(int)); for(i = 0; i <( sizeof(ArrayBuf)/sizeof(int));i++) { printf("%d ",ArrayBuf[i]); } } 来源: https://www.cnblogs.com/HMM0530/p/11398683.html

Swift use sizeof with Int32 Array

会有一股神秘感。 提交于 2019-11-28 07:20:13
问题 i want to get the Length of an Array with "sizeof". I tried everything. This is the error message: "[Int32] is not convertible to T.Type" The Array has to be Int32. var testArray: [Int32] = [2000,400,5000,400] var arrayLength = sizeof(testArray) 回答1: You can get the number of elements in an array simply with let count = testArray.count and the total number of bytes of its elements with var arrayLength = testArray.count * sizeof(Int32) // Swift 3: var arrayLength = testArray.count *

基于Linux购票服务器

不羁的心 提交于 2019-11-28 07:12:25
/date 航班信息文件 /register 用户注册信息文件 /user 用户购票信息文件 server #include "head.h" char chars[10]; char sendbuf[1024]; struct list_node{ char name[10];//用户名 char passwd[10];//密码 char number[10];//航班号 char staddress[10];//起点站 char arraddress[10];//终点站 char date[10];//班期 char type[10];//机型 char stime[10];//起飞时间 char price[10];//票价 char aggregate[10];//总金额 struct list_head list;//内核链表指针域 }; struct list_node_ip{ int connfd; char ip[50]; pthread_t tid; struct list_head list; }; struct list_node_ip *head = NULL; //初始化内核链表头 struct list_node_ip *init_list_head_ip(struct list_node_ip *head) { head = (struct list

Get size of pointer in C

安稳与你 提交于 2019-11-28 06:27:10
How do I get the size of a pointer in C using sizeof ? I want to malloc some memory to store a pointer (not the value being pointed to). Given an arbitrary type (I've chosen char here, but that is for sake of concrete example): char *p; You can use either of these expressions: sizeof(p) sizeof(char *) Leading to a malloc() call such as: char **ppc = malloc(sizeof(char *)); char **ppc = malloc(sizeof(p)); char **ppc = malloc(sizeof(*ppc)); The last version has some benefits in that if the type of ppc changes, the expression still allocates the correct space. This should do the trick: sizeof

Why does sizeof return different values for same string in C? [duplicate]

試著忘記壹切 提交于 2019-11-28 06:04:29
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sizeof doesn't return the true size of variable in C C -> sizeof string is always 8 Sizeof prints out 6 for: printf("%d\n", sizeof("abcde")); But it prints out 4 for: char* str = "abcde"; printf("%d\n", sizeof(str)); Can someone explain why? 回答1: The string literal "abcde" is a character array . It is 6 bytes long, including the null terminator. A variable of type char* is a pointer to a character. Its size is