C + + Primer learning notes _15_ from C to C + + (1)--bool type, const qualifier, const and # define, structure body-in-store alignment

Source: Internet
Author: User


Welcome to read the reference, if there are errors or questions, please leave a message to correct, thank you

One, bool type (C language not) 1, The logical type is also called the Boolean type, its value is true(logical True) and false(logical false), the number of storage bytes in the different compilation system may differ, VC + + is 1 bytes in total. 2, declaration method:bool result; result=true; 3, can be used as integers (true is generally 1,false is 0) 4. When converting other types of values to Boolean values, non-0 values are converted to true, and 0 values are converted to false5. Example

#include <iostream>
using namespace std;
int main (void)
{
 bool result;
 // result = true;
 result = 100;
 cout << result << endl;
 return 0;
}
Operation result: 1

Second, the const qualifier 1. Use const to give the literal constant a name (identifier). This identifier is called the identifier constant; because the declaration and use of the identifier constant is much like a variable, it is also called a constant variable.
2. The general form of definition: (1) const data type constant name = constant value; (2) data type const constant name = constant value;
3. Points for attention: (1) Constant variables must be initialized when they are defined; (2) After constant variables are initialized, no further assignment is allowed;
4. Examples
#include <iostream>
using namespace std;
int main (void)
{
 // const int a; Error, constant must be initialized
 const int a = 100;
 // a = 200; Error, constant cannot be reassigned
 int b = 22;
 const int * p; // const is on the left side of *, indicating that * p is a constant, and the content pointed to by the pointer cannot be changed via * p
 p = & b;
 // * p = 200; Error, the constant cannot be reassigned
 // int * const p2; Error, p2 is a constant, the constant must be initialized
 int * const p2 = & b; // const is to the right of *, indicating that p2 is a constant
 // int c = 100;
 // p2 = & c; Error, the constant cannot be reassigned
 * p2 = 200;
 cout << b << endl;
 return 0;
}
operation result:
200

Three, const (C ++) and #define (C language) 1. The difference between const-defined constants and symbolic constants defined by #define (1) const-defined constants have types, and # define-defined ones have no types. Type safety checks are performed, and the latter is just a simple replacement. (2) Constants defined by const are allocated at compile time, while constants defined by #define are replaced at precompilation, and memory is not allocated. (3) The scope is different. The scope of the constant variable defined by const is the scope of the variable. The constant scope defined by #define is its definition point to the end of the program, of course, it can also be cancelled with #undef somewhere
2. You can also use enum to define constants. C ++ recommends replacing #define with const and enum as much as possible to define constants. In addition, you can also use enum to define constants. In C ++, try to use const and enum to replace #define to define constants and inline to replace macro definitions with parameters.
#include <iostream>
using namespace std;

#define STR (a) #a
#define CAT (a, b) a ## b

int main (void)
{
    int xy = 100;
    cout << STR (ABCD) << endl; // #ABCD => "ABCD" // means double quotes to a, a # means that position is replaced by a macro parameter as a string
    cout << CAT (x, y) << endl; // x ## y => xy // indicates that x connects to y, two ## means to concatenate the strings at both ends of ##

    return 0;
}

3. Constants defined by #define are prone to side effects.
// An example of Effective C ++ 3rd.
#define CALL_WITH_MAX (a, b) f ((a)> (b)? (a): (b))
int a = 5;
int b = 0;
CALL_WITH_MAX (++ a, b); // a is accumulated twice
CALL_WITH_MAX (++ a, b + 10); // a is accumulated once
Here, before calling f, the number of increments of a actually depends on "who is it compared to"



4. You can also use enum to define constants. Try to use const and enum instead of #define to define constants.



Fourth, the structure memory alignment
First revisit the calculation principle of class / object size:

        32-bit 64-bit char 1 1 int 4 Most 4, a few 8 long 4 8 float 4 4 double 8 8 Pointer 4 8 (same as long) Note: No matter what type of pointer is, it is the same as long, double * is also 4.
1. What is memory alignment (1) The compiler arranges each "data unit" in a suitable position. (2) C and C ++ languages are very flexible, it allows you to interfere with "memory alignment"
2. Why alignment performance reasons: fast access to data at aligned addresses.
3. How to align-Alignment rules (1) The first data member is placed at offset 0 (the address stored by the first member is the address of the structure) (2) Other members are aligned to min (sizeof (member ), An integer multiple of the value specified by #pragma pack). (You can see the right-click properties) Win32 optional 1, 2, 4, 8, 16 Linux 32 optional 1, 2, 4 is greater than the maximum value is the default, the default value in win is 8, Linux default value 4 (3) The entire structure should also be aligned, and the total size of the structure should be aligned to an integer multiple of the maximum number of alignments in each member.
4. Examples (1) The default value in win is 8
#include <iostream>
using namespace std;
#include <stdio.h>

struct Test
{
 char a;
 double b;
};

int main (void)
{
 cout << sizeof (Test) << endl;
 return 0;
}
Operation result: 16
(2)
#include <iostream>
using namespace std;
#include <stdio.h>

#pragma pack (2)
struct Test
{
 char a;
 double b;
 char c;
};
#pragma pack ()

// The offset between the first member and the structure variable is 0
// Other members should be aligned to the address of an integer multiple of a number (aligned number)
// The alignment number takes the smaller value of an alignment integer preset by the compiler and the size of the member
// The total size of the structure is an integer multiple of the maximum alignment number

int main (void)
{
 Test test;
 // & test == & test.a;
 char * p = (char *) & test;
 // cout << p << endl;
 printf ("p =% p \ n", p);
 p = & test.a;
 printf ("p =% p \ n", p);

 cout << sizeof (Test) << endl;
 return 0;
}
Operation result: p = 0019568 p = 0019568
12
(3) Under Linux, the default is 4 Modify the code part:
#pragma pack (4) Operation result: 16
(4)
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

struct A
{
    long a1;
    short a2;
    int a3;
    int * a4;
};

int main ()
{
    cout << sizeof (A) << endl;
}

(5)
#include <stdint.h>
#include <stdio.h>

union X
{
    int32_t a;
    struct
    {
        int16_t b;
        int16_t c;
    };
};

int main ()
{
    X x;
    x.a = 0x20150810;
    printf ("% x,% x \ n", x.b, x.c);
    return 0;
}



reference:

C ++ primer fourth edition

C ++ primer fifth edition



Copyright notice: This article is an original article by bloggers and may not be reproduced without the permission of the blogger.

C ++ Primer study notes _15_ from C to C ++ (1)-bool type, const qualifier, const and #define, structure memory alignment 

Related Article

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.