Dynamic Array with any deformation of dimension and length

Source: Internet
Author: User

1. Overview of dynamic arrays with any deformation of dimensions and lengths
Recently, some colleagues have to design a multidimensional array that can be freely deformed. After a few days of hard work, they have to solve the problem and turn to me for help. So I wrote an array ADT that can freely change the dimension and length. Later, I thought that someone on the Internet should also need this kind of things. Why don't I put it on my blog and let friends who need this kind of things come together for reference. As a result, the first three functions are extended to 20 functions. In view of the rush of time, there must be a lot of imperfections in the source code. If you are interested in improving the source code, you can't go further.

 

I call this ADT dynamic dimensionality and length array, a dynamic dimension and length array, or ddla for short. Its main features are as follows:

 

1. VLA Variable Length array that surpasses c99. Although the length of each dimension can be a variable, there are still two restrictions: one is that the dimension is unchangeable; the other is that the length cannot be changed once defined, that is, it is not dynamic. Ddla is dynamic regardless of dimension and length. Not only can the length of each dimension be a variable, but also a dimension can be a variable.

 

2. Even after the definition of ddla, you can change the dimension and length at any time as needed, so that it is truly completely dynamic and can be called transformers in the array. In addition, the original data can be retained after deformation. The deformation method is simple and quick.

 

3. The function of using the C/C ++ array as a form parameter to implicitly convert to a pointer. The C/C ++ feature improves the efficiency, but also causes the problem that the input array size cannot be directly obtained through the sizeof operation within the called function. While keeping this efficiency, you can still directly calculate the size of the input array within the called function.

 

 

To design a dynamic array of the ddla type, it is inevitable to create an intermediate Address Buffer. How can we construct this buffer? The common algorithm is to construct and allocate resources at any time. For example, a dynamic two-dimensional array can be constructed as follows:

 

View plaincopy to clipboardprint?
Int I, m = 10, n = 20;
Int ** P = (INT **) malloc (M * sizeof (int *));
For (I = 0; I <m; ++ I)
P [I] = (int *) malloc (N * sizeof (INT ));
Int I, m = 10, n = 20;
Int ** P = (INT **) malloc (M * sizeof (int *));
For (I = 0; I <m; ++ I)
P [I] = (int *) malloc (N * sizeof (INT ));
 

 

This method has an obvious drawback, that is, the element storage zone and the intermediate address buffer zone are not consecutive! This causes the following problems:

 

1. The heap management functions run frequently, causing low efficiency. The larger the array, the more obvious the efficiency impact;

2. If the memory allocation fails in a step in the middle, the allocated memory needs to be released again, which is complicated;

3. After the array is used up, memory blocks need to be released one by one, which is inefficient;

4. If you need to dynamically change the array, the algorithm is complex.

 

But in fact, the size of the intermediate Address Buffer and the element storage area can be calculated in advance. Under this premise, the two storage blocks can be allocated in advance, greatly improving the efficiency, more importantly, this method greatly simplifies the array deformation algorithm and simplifies the deformation process. You only need to change the size of the element storage area and then re-generate the intermediate address. Ddla is designed based on this principle and uses the intermediate Address Buffer and the element storage area to be continuous.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/supermegaboy/archive/2009/08/27/4491523.aspx

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.