sizeof

Why does sizeof(my_arr)[0] compile and equal sizeof(my_arr[0])?

若如初见. 提交于 2020-06-09 07:55:07
问题 Why does this code compile? _Static uint32_t my_arr[2]; _Static_assert(sizeof(my_arr) == 8, ""); _Static_assert(sizeof(my_arr[0]) == 4, ""); _Static_assert(sizeof(my_arr)[0] == 4, ""); The first 2 asserts are obviously correct, but I would have expected the last line to fail, as my understanding is that sizeof() should evaluate to an integer literal, which can't be treated as an array. In other words, it would fail in the same way that the following line fails: _Static_assert(4[0] == 4, "");

Unexpected behaviour when using sizeof operator [duplicate]

五迷三道 提交于 2020-05-29 07:43:57
问题 This question already has answers here : Why (and when) do I need to use parentheses after sizeof? (3 answers) Closed last month . #include <stdio.h> #include <stdlib.h> typedef struct StupidAssignment{ long length; char* destination_ip; char* destination_port; long timestamp; long uid; char* message; }packet; void main(){ int number_of_packets=10;int i; packet* all_packets[number_of_packets]; for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof packet); } The above snippet does not

Unexpected behaviour when using sizeof operator [duplicate]

隐身守侯 提交于 2020-05-29 07:43:21
问题 This question already has answers here : Why (and when) do I need to use parentheses after sizeof? (3 answers) Closed last month . #include <stdio.h> #include <stdlib.h> typedef struct StupidAssignment{ long length; char* destination_ip; char* destination_port; long timestamp; long uid; char* message; }packet; void main(){ int number_of_packets=10;int i; packet* all_packets[number_of_packets]; for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof packet); } The above snippet does not

Unexpected behaviour when using sizeof operator [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-05-29 07:43:08
问题 This question already has answers here : Why (and when) do I need to use parentheses after sizeof? (3 answers) Closed last month . #include <stdio.h> #include <stdlib.h> typedef struct StupidAssignment{ long length; char* destination_ip; char* destination_port; long timestamp; long uid; char* message; }packet; void main(){ int number_of_packets=10;int i; packet* all_packets[number_of_packets]; for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof packet); } The above snippet does not

Get the size of std::any

允我心安 提交于 2020-05-27 10:47:20
问题 Is there any way to get the size (in bytes) of the data stored by std::any ? The only workaround I came up with is querying the type of its value by std::any::type and comparing the result to a list of known types like my_any.type() == typeid(T) , then the size is sizeof(T) . Unfortunately, this solution only works when the types are known beforehand. Do you know any solution? 回答1: You cannot get the size of the object held by std::any (other than the workaround you've mentioned). std::any is

Why sizeof of a struct is unsafe

独自空忆成欢 提交于 2020-05-11 04:25:16
问题 The MSDN clearly states For all other types, including structs, the sizeof operator can only be used in unsafe code blocks. The C# Language Specification is even more precise : The order in which members are packed into a struct is unspecified. For alignment purposes, there may be unnamed padding at the beginning of a struct, within a struct, and at the end of the struct. The contents of the bits used as padding are indeterminate. When applied to an operand that has struct type, the result is

What is the minimum size of a boolean?

有些话、适合烂在心里 提交于 2020-04-30 06:20:13
问题 I was studying about arithmetic types in C++ Prime by Stanley B. Lippman. When the author talks about integral types and the size of each one of them, like for example char is 8 bits, I noticed that it is not so clear the minimum size of a boolean. Considering type conversions, and the following code: bool b = 42; // b is true int i = b; // i had value 1 Can I say that booleans have the same minimum size of an integer (usually 16 bits), since if it is false it would be 0, that is an int, and

车站售票系统

妖精的绣舞 提交于 2020-04-07 19:38:36
#include <windows.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define EmFile "C:\\zm.txt" //密码帐号文件名 #define N 3 #define TEMP 2 #define TRUE 1 #define FALSE 0 int shoudsave=0 ; int count1=0,count2=0,mark=0,mark1=0 ; struct train { char num[10];//列车号 char city[10];//目的城市 char takeoffTime[10];//发车时间 char receiveTime[10];//到达时间 int price;//票价 int bookNum ;//票数 int leftbookNum; }; //订票人的信息 struct man { char num[10];//ID char name[10];//姓名 int bookNum ;//需求的票数 char password[7];//密码 int tag; }; //定义火车信息链表的结点结构 typedef struct node { struct train data ; struct node