struct

Packing nested structs in C++

北战南征 提交于 2021-01-01 07:30:41
问题 Using Visual Studio 2017, the following gives... struct AAA // 15 bytes { double d; short s; char a1; char a2; char a3; char s4; char s5; }; struct BBB { AAA d; char a4; }; int main() { std::cout << sizeof(AAA) << "\n"; // gives 16 std::cout << sizeof(BBB) << "\n"; // gives 24 getchar(); return 0; } The Question is... how do I get sizeof(BBB) to be 16. 回答1: Use #pragma pack(push, 1) or #pragma pack(1) to enforce compiler not to line up structure members on 2 byte or 4 byte boundaries which

C/C++ Pointer to a POD struct also points to the 1st struct member

邮差的信 提交于 2020-12-21 03:59:35
问题 Can I assume that a C/C++ struct pointer will always point to the first member? Example 1: typedef struct { unsigned char array_a[2]; unsigned char array_b[5]; }test; //.. test var; //.. In the above example will &var always point to array_a? Also in the above example is it possible to cast the pointer to an unsigned char pointer and access each byte separately? Example 2: function((unsigned char *)&var,sizeof(test)); //... //... void function(unsigned char *array, int len){ int i; for( i=0;

C/C++ Pointer to a POD struct also points to the 1st struct member

自作多情 提交于 2020-12-21 03:57:55
问题 Can I assume that a C/C++ struct pointer will always point to the first member? Example 1: typedef struct { unsigned char array_a[2]; unsigned char array_b[5]; }test; //.. test var; //.. In the above example will &var always point to array_a? Also in the above example is it possible to cast the pointer to an unsigned char pointer and access each byte separately? Example 2: function((unsigned char *)&var,sizeof(test)); //... //... void function(unsigned char *array, int len){ int i; for( i=0;

C/C++ Pointer to a POD struct also points to the 1st struct member

放肆的年华 提交于 2020-12-21 03:56:00
问题 Can I assume that a C/C++ struct pointer will always point to the first member? Example 1: typedef struct { unsigned char array_a[2]; unsigned char array_b[5]; }test; //.. test var; //.. In the above example will &var always point to array_a? Also in the above example is it possible to cast the pointer to an unsigned char pointer and access each byte separately? Example 2: function((unsigned char *)&var,sizeof(test)); //... //... void function(unsigned char *array, int len){ int i; for( i=0;

C/C++ Pointer to a POD struct also points to the 1st struct member

蹲街弑〆低调 提交于 2020-12-21 03:54:44
问题 Can I assume that a C/C++ struct pointer will always point to the first member? Example 1: typedef struct { unsigned char array_a[2]; unsigned char array_b[5]; }test; //.. test var; //.. In the above example will &var always point to array_a? Also in the above example is it possible to cast the pointer to an unsigned char pointer and access each byte separately? Example 2: function((unsigned char *)&var,sizeof(test)); //... //... void function(unsigned char *array, int len){ int i; for( i=0;

same date when printing from st_mtime, st_ctime,st_atime

非 Y 不嫁゛ 提交于 2020-12-12 09:53:09
问题 So, I'm supposed to print the date of access of directories, modification and creation, but they all seem to be the same date. Here's my code: struct* tm date; struct stat fileStat; if(options[0] == 1 && options[1] == 0 && options[2] == 0 && options[3] == 0){ date = localtime(&(fileStat.st_mtime)); printf("M%02i/%d/%i-%02d:%02d", date->tm_mon,date->tm_mday, (date->tm_year + 1900)%100, date->tm_hour,date->tm_min); date = NULL; } else if(options[0] == 1 && options[1] == 1 && options[2] == 0 &&

What is a function for structs like Java's instanceof?

送分小仙女□ 提交于 2020-12-08 20:11:24
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

冷暖自知 提交于 2020-12-08 20:07:47
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

寵の児 提交于 2020-12-08 20:05:04
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

Why does `sxhash` return a constant for all structs?

情到浓时终转凉″ 提交于 2020-12-05 03:42:32
问题 When using the Common Lisp sxhash function on structs I'm getting the same value for all structs (in SBCL only structs of the same type). For instance, the following code prints two lists of integers all of which have the same value. (progn (defstruct foo data) (print (mapcar #'sxhash (loop for i below 10 collect (make-foo :data i)))) (defstruct bar data) (print (mapcar #'sxhash (loop for i below 10 collect (make-bar :data i))))) ;;; Allegro (319 319 319 319 319 319 319 319 319 319) (319 319