Taking Yang Hui Triangle as an example, a simple analysis of dynamic two-dimensional array in C language from memory angle

Source: Internet
Author: User

Learn C language, must be around the pointer this big difficulty, and the pointer is the most headache is a variety of pointing relations, first-order pointers are relatively easy to grasp, but once the order of a high, it is easy to understand the point of the relationship, now I will pass the Yang Hui triangle as an example, I will use four ways to analyze the dynamic two-dimensional array If there are deficiencies or errors, please also point out!

Before talking about this, take a one-dimensional array as an example and first re-recognize the array:

int array[5] = {1, 2, 3, 4, 5};

First the array name is the first address constant of the array, that is, the array name is a pointer, there is &array[0] = = array!

Then we can launch *array = = Array[0] = = 1;

The introduction of a concept "refers to a class" (This concept has not appeared in the formal situation, but I was introduced for the convenience of analysis), which represents the type of space the pointer points to! Array is a pointer to a int* type, then its reference class is the int type (easier memory, that is, the type of pointer minus a * is its reference Class!). );

In fact Array[0] is a representation, its essence should be *array;

Our array is a local variable, we have applied sizeof (int) in the system stack, that is, 20 bytes of space, to hold 5 integer number!

Because the value of the array is the address (that is, the first address) of the first element of the arrays, then array+1 means to add an int type space byte to the first address of the array, which is 4 bytes, so that the array+1 value should be the address of the next int type element of the array, i.e. &A[1], so there's array+1 = = &a[1];

Then the array plus a few plus a few, plus the actual number of points is the size of the space!

Then *array can be understood as * (array + 0), similarly, array[1] ==* (array + 1), array[2] = = * (array + 2) ...

The upper formula can also be deformed by the addition exchange law to get array[1] = = * (1 + array), then array[1] through this essence, it can also be changed to form 1[array];

* This write compiler will not error, and even warning will not have, but it is not recommended to write this!

* If you do not understand the system stack I mentioned above, it is strongly recommended to take a look at the following blog, after all involved in memory!

* Later I will not use Array[index] this way, but with * (array + index) This is the most essential way

The relation of formal participation in C language

Here, let's talk about the system stack and the system heap:

The operating system divides the memory into: System data, System function call (core code) area, user code and data region, system stack area, system heap area;

The system stack is automatically assigned by the compiler, which holds the parameter values of functions, the values of local variables, etc.

and the system heap area is the space that the programmer applies the self-application through the Malloc/calloc function, the space of the system heap is much larger than the space of the system stack, but keep in mind that you must release the requested space through the free function after use.

* C language (including C + +) does not have a GC (garbage collection) mechanism like Java, the GC mechanism greatly reduces the workload of the programmer, the programmer in Java through the new application space, only responsible for the application, GC will help the aftermath (in fact, Java JVM), and C + + Requires the programmer to release autonomously, so Java is easier to master than C + +!

--------------------------------------------------------------------------------------------------------------- -------------------

Yang Hui triangle:

I will not be burdensome about how the Yang Hui triangle can be calculated (* ^_^ *)

Method One:

, we give a six-layer Yang Hui triangle, and usually we will give a static two-dimensional array to hold this Yang Hui triangle:

int Yanghui[row][row];

But this will generate a row row Xrow column space, and we actually use the space is smaller than this, in addition to the last layer, each other layer will have wasted space, in order to avoid this situation, we should think of the dynamic array, according to the current number of rows, by malloc/ Calloc dynamically applies for each layer of space. So, we can represent this two-dimensional array in the following way:

int *yanghui[row];

This definition looks very strange, in fact it is completely a one-dimensional array, the size of the one-dimensional array is row, but this one-dimensional array of each element is composed of an int * type, its essence is a one-dimensional pointer array! We can define it in the following form:

typedef int* Type;type Yanghui[row];

It's a good idea to see this, he's a one-dimensional array of type type! And the type is int*, then, the one-dimensional array to hold each element should be a int* type of value, then this value can be an int type of one-dimensional array of the first address! That is, the Yanghui array contains the first address of a row one-dimensional array!

The groundwork is finished and we come down to generate the Yang Hui triangle:

* As the number of Yang Hui triangle is getting bigger and larger, the following code is used long type! First assume that the number of layers to be generated for the Yang hui triangle is num = 5;

void creatyanghuione (int num), void creatyanghuione (int num) {long *yanghui[num];int row;int col;for (row = 0; row < num ; row++) {* (Yanghui + row) = (long *) calloc (sizeof (long), row + 1); for (col = 0; Col <= Row; col++) {* (* (Yanghui + row) + col) = (row = = Col | | col = = 0)? 1: * (* (Yanghui + row-1) + col-1) + * (* (Yanghui + row-1) + col);}}        About the Showyanghuitriangle function I will give at the end, just to print good-looking, do not focus! Showyanghuitriangle (Yanghui, num); for (row = 0; row < num; row++) {Free (* (Yanghui + row);}}


Multiple loops get the following relationship

Method Two:
void creatyanghuitwo (int num), void Destoryyanghui (long **yanghui, int num), void creatyanghuitwo (int num) {long **yanghui = Null;int Row;int Col;yanghui = (long *) calloc (sizeof (long), num); for (row = 0; row < num; row++) {* (Yanghui + row  = (long *) calloc (sizeof (long), row + 1); for (col = 0; Col <= Row; col++) {* (* (Yanghui + row) + col) = (row = = Col | | Col = = 0)? 1: * (* (Yanghui + row-1) + col-1) + * (* (Yanghui + row-1) + col);}} Showyanghuitriangle (Yanghui, num);d estoryyanghui (Yanghui, num);} void Destoryyanghui (long **yanghui, int num) {int row, for (row = 0; row < num; row++) {Free (* (Yanghui + row));} Free (Yanghui);}

Method Three:
Long **creatyanghuithree (int num), long **creatyanghuithree (int num) {Long **yanghui = null;int row;int Col;yanghui = (long * *) calloc (sizeof (long), num), for (row = 0; row < num; row++) {* (Yanghui + row) = (long *) calloc (sizeof (long), row + 1); for (col = 0; Col <= Row; col++) {* (* (Yanghui + row) + col) = (row = = Col | | col = = 0)? 1: * (* (Yanghui + row-1 ) + col-1) + * (* (Yanghui + row-1) + col);}} return Yanghui;}

As with method Two, it is simply the return value is a long * * type, and the value of Yanghui in the Creatynaghhuithree function is returned as the first address of the space requested in the system heap Addressrow as the return value! The space does not disappear with the call of the child function and needs to be released in the main function!

Method Four:
void Creatyanghuifour (long ***yanghui, int num), void Creatyanghuifour (long ***yanghui, int num) {int Row;int Col;*yanghui = (long *) calloc (sizeof (long), num), for (row = 0; row < num; row++) {* ((*yanghui) + row) = (long *) calloc (sizeof ( Long), row + 1), for (col = 0; Col <= Row; col++) {* (* ((*yanghui) + row) + col) = (row = = Col | | col = = 0)? 1: * (* (*y Anghui) + row-1) + col-1) + * (* ((*yanghui) + row-1) + col);}}}

When the Creatyanghuifour function call ends, the stack top pointer falls back, and the local variables of the sub-functions of the system stack request are released, but the value of the Yanghui space of the main function is changed from NULL to Ddressrow by the pointer operation, and this space is applied in the system heap, Does not disappear with the call of the child function, that is, the space has not been released, so it needs to be released in the main function!

Print functions and main functions:
void Showyanghuitriangle (long **yanghui, int num), int getmaxnumberlength (long num), int getmaxnumberlength (long num) { int count = 1;while (num/=10) {++count;} return count;} void Showyanghuitriangle (long **yanghui, int num) {int len = getmaxnumberlength (* (* (Yanghui + num-1) + NUM/2)); int I;int  J;int row;int col;for (row = 0; row < num; row++) {for (i = 0; i < num-row-1; i++) {for (j = 0; J < Len; j + +) {printf ("");}}  for (col = 0; col < row + 1; col++) {printf ("%ld", * (* (Yanghui + row) + col)), if (Getmaxnumberlength (* (* (Yanghui + row) + col) < Len) {for (j = 0; J < len-getmaxnumberlength (* (* (Yanghui + row) + col); J + +) {printf ("");}} for (j = 0; J < Len; J + +) {printf ("");}} printf ("\ n");}} int main () {long **yanghui = Null;int num;printf ("Please enter the number of lines: \ n"), scanf ("%d", &num); Creatyanghuione (num); Creatyanghuitwo (num);//yanghui = Creatyanghuithree (num); Creatyanghuifour (&yanghui, num); Showyanghuitriangle ( Yanghui, num);d estoryyanghui (Yanghui, num); return 0;}

Output

Thank you for reading (* ^_^ *)

Taking Yang Hui Triangle as an example, a simple analysis of dynamic two-dimensional array in C language from memory angle

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.