Use of a 0-length Array

Source: Internet
Author: User

An array of 0 length is not allowed in iso c and C ++ specification, but c99 of GCC supports this usage.
For GCC documentation on the 0-length array, refer to "arrays of Length zero"

Which of the following code snippets is more concise and flexible? You can see at a glance:

# Include <stdlib. h> # include <string. h> typedef struct tagarray {int length; char contents []; // This member must be the last member of the struct .} Array_s; typedef struct tagpointer {int length; char * Contents;} pointer_s; int main () {int array_length = 10; array_s * parray = (array_s *) malloc (sizeof (array_s) + array_length); pointer_s * ppointer = NULL; If (null = parray) {return 0;} parray-> length = array_length; memset (parray-> contents, 'A ', array_length); free (parray); ppointer = (pointer_s *) malloc (sizeof (pointer_s); If (ppointer = NULL) {return 0;} memset (ppointer, 0, sizeof (pointer_s); ppointer-> length = array_length; ppointer-> contents = (char *) malloc (array_length); If (ppointer-> contents = NULL) {free (ppointer); Return 0;} memset (ppointer-> contents, 'A', array_length); free (ppointer-> contents); free (ppointer ); return 0 ;}
Definition of the first struct: to allocate a continuous memory to the data in a structure, there are two advantages: (1) convenient memory release. If our code is provided to others, you perform secondary memory allocation and return the entire struct to the user. The user can call free to release the struct, but the user does not know that the Members in this structure also need free, so you cannot expect the user to discover this. Therefore, if we allocate the memory of the struct and the memory of its members at one time, and return a struct pointer to the user, you can release all the memory once you do the free operation. (2) access speed is favorable. Continuous memory improves access speed and reduces memory fragments.

The second type of struct is used: the memory needs to be allocated twice and the memory needs to be released twice. It is obviously not the first type of code that checks whether the applied memory is successful or not.

 

Check how the memory is continuous. Run the gdb x command to check that: (the char contents [] In array_s does not occupy the memory of the struct,
Therefore, array_s has only one int Member, four bytes, and we need to allocate 10 bytes for contents []. Therefore, the total length is 14 bytes ):

1 (GDB) P parray 2 $1 = (array_s *) 0x804b008 3 (GDB) p * parray 4 $2 = {length = 10, contents = 0x804b00c "aaaaaaaaaa"} 5 (GDB) P parray-> contents 6 $3 = 0x804b00c "aaaaaaaaaa" 7 (GDB) x/14B parray 8 0x804b008: 10 0 0 0 97 97 97 9 0x804b010: 97 97 97 97 97
From the above memory layout, we can see that the first four bytes are int length, and the last 10 bytes are char contents [].

If a pointer is used, it will look like this: 10 (GDB) P ppointer 11 $4 = (pointer_s *) 0x804b02012 (GDB) p * ppointer 13 $5 = {length = 10, contents = 0x804b030 "aaaaaaaaaa"} 14 (GDB) P ppointer-> contents 15 $6 = 0x804b030 "aaaaaaaaaa" 16 (GDB) x/16B ppointer 17 0x804b020: 10 0 0 0 48-80 4 818 0x804b028: 0 0 0 17 0 0 019 (GDB) x/10B ppointer-> contents 20 0x804b030: 97 97 97 97 97 97 9721 0x804b038: 97 9722 (GDB) x/16x ppointer 23 0x804b020: 0x0a 0x00 0x00 0x00 0x30 0xb0 0x04 0x0824 0x804b028: 0x00 0x00 0x00 0x00 0x11 0x00 0x00 0x00

The first four bytes of the 17th row are int length, and the last four bytes are the addresses of contents.
The 23rd rows are displayed in hexadecimal format. The address is 0x30 0xb0 0x04 0x08, that is, 0x0804b030.
Rows 20th and 21st are the content pointed to by char * contents.

Therefore, we can see the difference: the array is the content, and the pointer stores the content address.

 

 

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.