Advanced C language: 10, struct and union analysis

A struct in C language can be seen as a collection of variables:

    Question: How much memory does an empty struct take?

#include<stdio.h>

struct TS
{

};

intmain()
{
	struct TS t1;
	struct TS t2;
	
	printf("sizeof(TS) = %d\n", sizeof(struct TS));
	printf("sizeof(t1) = %d, &t1 = %p\n", sizeof(t1), &t1);
	printf("sizeof(t1) = %d, &t2 = %p\n", sizeof(t2), &t2);
	
	return 0;
}

Compile and run using Gcc under linux:

sizeof(TS) = 0
sizeof(t1) = 0, &t1 = 0xbf963630
sizeof(t1) = 0, &t2 = 0xbf963630

But under the Bcc compiler, an error will be reported directly. //The compiler believes that the existence of the structure is to store different data types.

Structures and flexible arrays:

    A flexible array is an array whose size is to be determined;

    In C language, flexible arrays can be generated from structures;

    The last element of a struct in C can be an element of unknown size.

struct SoftArray
{
	int len;
	int array[];       -->>  sizeof(struct SoftArray) = ???
}

array[] in SoftArray is just an identifier to be used and does not occupy storage space.

printf("sizeof(struct SoftArray) = %d\n", sizeof(struct SoftArray));
sizeof(struct SoftArray) = 4

Usage of flexible array:  

struct SoftArray sa* = NULL;

sa = (struc SoftArray*)malloc(sizeof(struct SoftArray) + sizeof(int)*5);

sa->len = 5; //Array with length information, that is, you can use a flexible array to get the length information of the array.
sa->array; //Dynamic application space

Observe the following code to experience the use of flexible arrays:

#include <stdio.h>
#include <malloc.h>

struct SoftArray
{
	int len;
	int array[];
};

struct SoftArray* creat_soft_array(int size)
{
	struct SoftArray* ret = NULL;
	
	if(size > 0)
	{
		ret = (struct SoftArray*)malloc(sizeof(struct SoftArray) + sizeof(int) * size);
		
		ret->len = size;
	}
	
	return ret;
}

void del_soft_array(struct SoftArray* sa)
{
	free (in);
}

void func(struct SoftArray* sa)
{
	int i=0;
	
	if( NULL != sa)
	{
		for(i=0; i<sa->len; i++)
		{
			sa->array[i] = i+1;
		}
	}

}

intmain()
{
	int i=0;

	struct SoftArray* sa = creat_soft_array(10);
	
	func (sa);
	
	for(i=0; i<sa->len; i++)
	{
		printf("sa->array[] =  %d\n", sa->array[i]);
	}
	
	del_soft_array(sa);
	
	return 0;
}
Union in C language:
Union only allocates space for the largest member, and all members share this space.
union B
{
	int a;
	int b;
	int c;
};
The use of union will be affected by the size of the system:
little endian: low addresses are stored in low bits
big endian : low addresses are stored in high bits
union C
{
	int i;
	char c; //fetch data from low address
};

union C c;

c.i=1;

printf("%d\n", cc); //?? little endian is 1, big endian is 0;

Using this feature, it is possible to detect the big and small endianness of the system:


#include <stdio.h>

int system_mode()
{
    union A
    {
        int i;
        char c;
    };
    
    union A cn;
    
    cn.i = 1;
    
    return cn.c;

}

intmain()
{
    printf("%d\n", system_mode());
    return 0;
}

summary:

    Each data member in a struct has its own storage space

    struct can generate flexible arrays by the last array identifier

    All arrays in a union share the same storage space

    The use of union will be affected by the size of the system


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325567953&siteId=291194637