C-language struct hack notes _c language

Source: Internet
Author: User
Tags int size

Recently in the compiler CodeGenerator experiment, part of the need to translate Java programs into C programs, such as:

Copy Code code as follows:

int [] array;
Array = new INT[10];
System.out.println (Array.Length); 10

The natural idea for this code to translate into C is:
Copy Code code as follows:

int * array; int array[] not support in C
Array = (int*) malloc (sizof (int) *10);
printf ("%d\n", Sizof (array)/sizeof (int)); 1

Unfortunately this is wrong because the malloc operation allocates space on the heap and is not necessarily contiguous, sizof (array) Gets the unit that the pointer itself occupies, and sizeof (int) is equal to the length of the array cannot be evaluated by SIZOF. It's not the same as the following:
Copy Code code as follows:

int array[10];
printf ("%d\n", Sizof (array)/sizeof (int)); 10

Here array is the sizeof of the entire contiguous storage space, so the length of the entire region is evaluated by the operator. But when the array name is passed as a function parameter, the array is degraded to a pointer and back to the problem.

What should we do?

After searching the StackOverflow, I found that ANSI C had no direct way to get the allocation length by pointing to the memory pointer. However, Windows provides methods for calculating the size of memory that the pointer points to [MALLOC.H]:

_msize:returns the size (in bytes) as a unsigned integer.

Copy Code code as follows:

size_t _msize (
void *memblock
);


However, because of the operating system policy, the actual allocated size may be larger than the specified number.

Under Linux, the pointer offsets an integer size unit to record the actual allocation size, and we'll look at the contents of that unit:

Copy Code code as follows:

TEST.c
int main () {
int * p;
int i;
int size;
for (i=1;i<11;i++)
printf ("%d", I);
printf ("\ n");
for (i=0;i<10;i++) {
p = (int*) malloc (sizeof (int) *i);
Size = * (int*) ((char*) p-sizeof (int));
printf ("size:%d", size);
Free (p);
}
printf ("\ n");
}


$GCC test.c
$./a.out
1 2 3 4 5 6 7 8 9 10
17 17 17 17 25 25 33 33 41 41


It seems that the Linux allocation policy does not make the memory size and the number of elements one by one corresponding, this method is not available. It turned out that there were also functions like _msize in Linux [malloc.h]:

Copy Code code as follows:

int * array;
int size;
Array = (int*) malloc (Sizof (50);
Size = malloc_usable_size (array);
printf ("%d\n", size);//50

But malloc.h does not belong to standard C, and we continue to look for common law. After a lot of lookup, finally found a code trick, called Struct-hack. As mentioned earlier, int a[] is illegal in C, but it is possible to take it as the last member of the struct:

Copy Code code as follows:

typedef struct array{
int size;
int free;
int buf[];
}array,*tiger_array;


This is the feature that is added later in the C language to implement the flexible array, so each time you allocate space to the array, you need to synchronize the record size. And the size of the time, directly removed to:

Copy Code code as follows:

Tiger_array Ta;
Ta = (int*) malloc (sizeof (array) +100);
ta->size = 100;
Ta->free = 0;

Note that the size of the allocation should be sizeof (struct) plus the size of the array required.

Here's the question.

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.