Enum type and union type

Source: Internet
Author: User

Enum type: Purpose is integer and character correspondence.

Here is just a description of the anonymous enumeration type, enum {__align=8}; This anonymous enumeration is equivalent to a static constant, similar to the following static const int __align=8;

Union type: The meaning of shared memory, interesting is that the general is to put the member data, but also can be put function, here need to explain is that it can be understood as a special structure. (Note that it is not always possible to put a function)

There is such a union in the STL: Union obj{union obj * Free_list_link;char client_data[1];} The shared memory size is 4B, and Char client_data[1]; occupies 1 B, then the big one is 4 B. Here's a design highlight.

Understand Char client_data[1]; is a group, but the array is pointed at the head with a pointer. It can be assumed that there is a pointer client_data pointing to an array with a storage space of 1 B. For a consortium, it is or expands to 4B, and you can see that client_data is pointing to the Union. It can be thought here that Client_data is the address of the consortium. If this value is not a member of the Union, it is not actually shared with the member Free_list_link, which is equivalent to a union containing 2 logical meanings, with its own address and pointers to the next union.

can be equivalent to struct obj{struct obj *free_list_link;}; Here is a pointer to the next, and its own address can only be passed, for example, obj * P=PT; Then the PT value is the P value. And not pt->client_data to get it as before. can also be obtained by &PT. But note that this needs to be converted to a (void *) pointer in order to be used, otherwise it is just something that uses the struct body part. In the previous design, the memory for the array pointers was extensible and was automatically converted.

#include <iostream>using namespacestd;union obj{Union obj*Free_list_link; Charclient_data[1];     }; intMain () {//Suppose these two are the memory to be allocated.     Charmem[ -] = {'C'}; Charmem1[ -] = {0 }; //now the beginning of every piece of memory is a union node structure//----------------------------------        //| Union obj | ...... ........// ----------------------------------  Union obj *P1 = (Union obj *) mem; Chara=p1->client_data[0]; //cout <<* ((p1->client_data) +1) << Endl; //cout<<p1->free_list_link<<endl; //cout<<p1->client_data<<endl; //use a variable to represent the structure//P1->free_list_link set to the starting segment of the next memoryP1->free_list_link = (Union obj *) mem1; //you can see that the mem and client_data two pointer values are consistentcout <<"mem ="<< (void*) Mem <<Endl; //cout<<mem<<endl;cout <<"P1->client_data ="<< (void*) P1->client_data <<Endl; //cout<<p1->client_data<<endl; //Client_data Just to simplify the definition of this paragraph of memory, just a bit easier,//you can use (void *) P1 to represent this memory, but each time you want to convert, it may be inconvenient,//It may not be used in practice.    return 0;}

Enum type and union type

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.