C language initialization and Memory Allocation

Source: Internet
Author: User

Many strings may report errors when being copied using strcpy or garbled characters when being directly assigned values, because they are not initialized.

Char * STR = (char *) malloc (sizeof (char ));

Memset (STR, 0, sizeof (char );

Sometimes the length of the array is unknown during compilation, so the array can be dynamically allocated.

Size = get_size (); </P> <p> int * P = new int [N]; </P> <p> for (int * q = P; Q! = P + N; ++ q) </P> <p>... </P> <p> Delete [] P; </P> <p>

Initialization of a custom struct

Mystuct mystr;

Memset (& mystr, 0, sizeof (mystr ));

And below

Mystuct * mystr;

Memset (mystr, 0, sizeof (* mystr ));

If it is a two-dimensional array, can I directly pass the declared two-dimensional array name to a pointer during initialization? After compilation, it will be found that there is a problem. The array name of the one-dimensional array is the pointer to the array. the pointer value stores the starting address of a contiguous area of the array in the memory, therefore, the subscript of the array indicates the offset of a storage area in the memory area relative to the starting address. The two-dimensional array is far more complicated than the one-dimensional array in concept, or two-dimensional arrays are constructed in an obscure way on a one-dimensional array.

I saw an article in my c ++ blog and noted down the following notes:

In fact, multi-dimensional arrays of computer systems are actually implemented in the form of one-dimensional arrays. For a two-dimensional array of N x m, set its array name to array. The pointer array points to an array, which stores a series of pointers pointing to the corresponding one-dimensional array, which stores our data.

Array-> [one-dimensional array pointer 1]-> [one-dimensional array, m length]

[One-dimensional array pointer 2]-> [one-dimensional array, M-length]

...... ......

[One-dimensional array pointer N]-> [one-dimensional array, M-length]

Array indicates the address of the I-th pointer variable. array [J] indicates the offset of J * sizeof (array type) relative to the I-th pointer variable ).

The system accesses the content of row I and column J of the N * m dimension array through this mechanism.

 

Therefore, for a two-dimensional array mystuct mystr [N] [m], the array pointer or two-dimensional pointer is required.

There are two methods for initialization:

Mystruct ** des = malloc (N * sizeof (mystruct *));

For (INT I = 0; I <n; I ++)

Des [I] = malloc (M * sizeof (mystruct ));

The following example shows how two dimensional pointers operate on two-dimensional arrays.

 Void double_div () </P> <p >{</P> <p> int score [3] [4] ={{ 60, 70, 80, 90}, {56, 89, 67,88 },{ 34,78, 90,66 }}; </P> <p> int * P1, * P2; </P> <p> int I; </P> <p> for (p1 = * score; P1 <= * (score + 2); P1 = p1 + 4) /* score + I is still the address */</P> <p >{</P> <p> for (P2 = p1; P2 <P1 + 4; p2 ++) </P> <p >{</P> <p> If (* P2 <60) </P> <p >{</P> <p> for (I = 0; I <4; I ++) </P> <p >{</P> <p> printf ("% d", * (P1 + I )); </P> <p >}</P> <p> printf ("/N "); </P> <p >}</P> <p>

The memory copy after Initialization is much simpler. With the memcpy function, the object pointer must be initialized. In addition, there is no restriction on the Data Type through memory copy. This is also the case in the following article that cstring to char * can use the ct2a macro method. If you use strcpy or direct = assignment, the effect cannot be achieved.

Float a = 3.12; </P> <p> float B = 0.0; </P> <p> void * data; </P> <p> DATA = malloc (sizeof (float); // 4 </P> <p> memcpy (data, & A, sizeof (float )); </P> <p> memcpy (& B, Data, sizeof (float); <br/>

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.