Dynamically allocate two-dimensional arrays in C ++

Source: Internet
Author: User

In C ++, dynamic one-dimensional arrays are implemented through the dynamic allocation of space by malloc. Dynamic Two-dimensional arrays can also be implemented through the dynamic allocation of space by malloc.

In fact, the C ++ language does not have a two-dimensional array, at least it does not directly support two-dimensional arrays. Instead, it is replaced by an array ", A two-dimensional array can be viewed as an array composed of pointers to arrays. For a two-dimensional array p [I] [j], the compiler uses the formula * (p + I) + j) to obtain the value of the array element:
1. p + I calculates the row pointer.
2. * (P + I) is a pointer pointing to the first element address of the row.
3. * (P + I) + j to get the address of the specific element.
4. * (p + I) + j) to obtain the element value.

Based on the above principle, we can allocate a pointer array, and then allocate space for each element of the pointer array to dynamically allocate a two-dimensional array. The following is an implementation of dynamically allocating two-dimensional arrays written by myself. It is applicable to any type of two-dimensional arrays and can be directly used. Type Definition and error code:

 
 
  1. Typedef unsigned char MK_Byte;
  2. # Define SUCCESS 0/* No error */
  3. # Define MFAILED 1/* General failure */
  4. # Define MNOMEMORY 2/* Out of memory */
  5. Statement
  6. // Ensure Initialization
  7. # Define DeclareTwoDArray (ATYPE, iname) ATYPE **Iname=NULL 
  8. // Define your own malloc and free to ensure proper memory operations
  9. # Define MKMALLOC (nsize) malloc (nsize)
  10. # Define MKFREE (name )\
  11. If (NULL! = Name )\
  12. Free (name );\
  13. Name=NULL
  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.