[Turn] char data [0] usage summary

@2019-07-31

1 struct MyData 
2 {
3     int nLen;
4     char data[0];
5 }; 

I did not start to understand the content of the red part of the next Internet search and found very useful, recorded.

        
In the structure, data is an array name; however, no element of the array; real address of the array immediately following structure MyData, and this address is the address of the structure after the data (if allocated to the structure is greater than the content of this structure the actual size of the body, the rear part of the excess data is the content); this method can be declared smart array implemented in C language extensions.


Taking such actual use:
  struct the MyData * P = (struct the MyData *) the malloc (the sizeof (struct the MyData) + strlen (str)), so that it can be operated by str p-> data.

Example:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 struct MyData 
 6 {
 7     int nLen;
 8     char data[0];
 9 };
10 
11 int main()
12 {
13     int nLen = 10;
14     char str[10] = "123456789";
15 
16     cout << "Size of MyData: " << sizeof(MyData) << endl;
17 
18     MyData *myData = (MyData*)malloc(sizeof(MyData) + 10);
19     memcpy(myData->data,  str, 10);
20 
21     cout << "myData's Data is: " << myData->data << endl;
22 
23     free(myData);
24 
25     return 0;
26 }

Output:

  Of the MyData Size:. 4
  myData the Data apos IS: 123456789        
since no element array, the array is allocated space in the structural body, so sizeof (struct Mydata) = 4.
malloc application is 14 bytes continuous space, it returns a pointer to the 14 bytes, cast into a struct INFO when the front 4 bytes are considered Mydata structure, the rear part of the copy of the "123456789" Content.

 

In reading the program often you see such a definition char data [0], which is a kind of usage, what good is used in what areas?

The main purpose of this paper is to clarify the definition of this role, as well as the scope of application, which requires a conceptual model and a pointer to the memory of the operating system have a good understanding of a situation.

First look at a program:

 1 #include <stdio.h>
 2 
 3 #include <string.h>
 4 
 5 #include <stdlib.h>
 6 
 7  
 8 
 9 typedef struct _Info
10 
11 {
12 
13     int i;
14 
15     char data[0];
16 
17 }Info;
18 
19  
20 
21 int main(int argc, char* argv[])
22 
23 {
24 
25     printf("%d/n",sizeof(Info));
26 
27     return 0;
28 
29 }

Results of the implementation are: 4.

Integer i accounted for four bytes, which indicates that data does not take up space. data is an array name; no element of the array; immediately after the structure Info real address of the array; this statement to be ingenious method to achieve the C language array expansion.

Remember that is different from the above structure:

1 typedef struct _Info
2 
3 {
4 
5     int i;
6 
7     char* data;
8 
9 }Info;

This structure occupies 8 bytes of space, because the pointer to the type of 4 bytes of space.

Look at an example:

 1 #include <stdio.h>
 2 
 3 #include <string.h>
 4 
 5 #include <stdlib.h>
 6 
 7 typedef struct _Info
 8 {
 9     int i;
10 
11     char data[0];
12 
13 }Info;
14 
15 
16 
17 int main(int argc, char* argv[])
18 {
19 
20     char buf[10] = "123456789";
21 
22     void* p = NULL;
23 
24 
25     printf("%d\n",sizeof(Info));
26 
27     Info* info = (Info*)malloc(sizeof(Info) + 10);
28 
29     p = (void*)info->data;
30 
31     printf("addr of info is %p. addr of data is %p ./n", info, p);
32 
33 
34     strcpy((char*)p, buf);
35 
36     printf("%s\n", (char*)p);
37 
38     return 0;
39 }

Results of the implementation of:

  4
  addr of info is 0x188b020. addr of data is 0x188b024 .
  123456789

Found, data immediately after the address structure.

 

[ Source ]

Guess you like

Origin www.cnblogs.com/skullboyer/p/11275070.html