Initialization of a struct

Source: Internet
Author: User

1, the initialization of the struct can use an array-like way, as follows:
struct Student
{
int _age;
string _name;
};
Student stu = {+, "Andy"};
2. There are two places to be aware of:
A, the order should be consistent, as follows:
Student stu = {"Andy", 26}; Compile error
B, the following can not be initialized
Student stu = {26}; Initialize _age only
3, consider the following situation, student there is a field, indicating the size of the student object, and in the debug and release, the size is not the same, how to do?
Use sizeof as follows:
struct Student
{
Int_size;
int _age;
string _name;
};
Student stu = {sizeof (stu), +, "Andy"};
4, note: The same data type, in Debug and release mode, the memory may be different. Like what:
Int:debug and release are all 4 bytes.
String:debug occupies 32 bytes, release occupies 28 bytes
5, in the debug mode, the object itself may occupy some more bytes, while in debug mode, the object will also occupy a number of bytes (such as int before and after the 4 bytes), for saving debugging information.

Initialization of a struct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.