Flexible array in C Language

Source: Internet
Author: User

Browse the previous article about cool shell today. So I checked the information and found out the "zero-length array", that is, the flexible array. The record is as follows.


A zero-length array is generally found in a struct, and a zero-length array is directly declared. For example,

char str[0];
It cannot be compiled. On the Internet, the zero-length array is usually placed at the position of the last member of the struct, but I tried to put it in the middle.

Put it firstLast positionLocation:

The struct declaration is as follows:

struct str{int len;int b;char s[0];};
Note that the compilation may fail here. It doesn't matter. You can change it to char s. Here char s [0] refers to a variable-length array, which is also equivalent to a pointer, but it is essentially different from a pointer. If it is declared as a pointer, the memory space pointed to by the pointer may be discontinuous with the space of member B, and the declared char s [0] is continuous.

Here, you may not know the size of the struct str. What is it? After testing, it is found that it is 8, that is, sizeof (int) + sizeof (int). That is to say, the space pointed by s does not belong to the str structure. This space is closely followed by the Space represented by the str structure.

The above points out that the space pointed to by char s [0] is closely followed by the Space represented by the struct. Therefore, there are the following methods for applying for memory for a struct str * pointer variable:

s = (struct str *)malloc(sizeof(struct str) + 10);
It indicates that 10 bytes of space is applied for s.
The following is an example:

#include 
 
  #include 
  
   #include 
   
    struct str{int len;int b;char s[0];};int main(int argc, char *argv[]){struct str *s = NULL;printf("sizeof(struct str) = %d\n", sizeof(struct str));s = (str *)malloc(sizeof(struct str) + 10);s->len = 100;s->b = 50;strcpy(&s->s[0], "abc");puts(s->s);printf("sizeof(struct str) = %d\n", sizeof(struct str));printf("addr of len = %p, value of len = %d\n", &s->len, s->len);printf("addr of b = %p, value of b = %d\n", &s->b, s->b);printf("addr of s = %p, value of s = %s\n", &s->s, s->s);return 0;}
   
  
 


The execution result is:

sizeof(struct str) = 8abcsizeof(struct str) = 8addr of len = 0x9831008, value of len = 100addr of b = 0x983100c, value of b = 50addr of s = 0x9831010, value of s = abc

We can see that s is indeed behind Member B! Although on the surface, s is a member of the str structure, it is actually not; the advantage of doing so is that the required memory space can be applied at one time (so the memory is continuous, memory fragmentation is also reduced), because if it is declared as a pointer, you must apply for the memory twice (struct once, member s once ).

Let's see if the member char s [0] is placed in the struct.Center PositionLocation:

#include 
 
  #include 
  
   #include 
   
    struct str{int len;char s[0];int b;};int main(int argc, char *argv[]){struct str *s = NULL;printf("sizeof(struct str) = %d\n", sizeof(struct str));s = (str *)malloc(sizeof(struct str) + 10);s->len = 100;s->b = 50;strcpy(&s->s[0], "abc");puts(s->s);printf("sizeof(struct str) = %d\n", sizeof(struct str));printf("addr of len = %p, value of len = %d\n", &s->len, s->len);printf("addr of s = %p, value of s = %s\n", &s->s, s->s);printf("addr of b = %p, value of b = %d\n", &s->b, s->b);return 0;}
   
  
 

The execution result is:

sizeof(struct str) = 8abcsizeof(struct str) = 8addr of len = 0x8da6008, value of len = 100addr of s = 0x8da600c, value of s = abcaddr of b = 0x8da600c, value of b = 6513249

We can clearly see that the address of s and B is the same. assign values to s and overwrite the value of B, which is a bit like a consortium!

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.