Allocating and releasing two-dimensional arrays in C

Source: Internet
Author: User

  
 
  1. /*
  2. 动态分配二维数组,在数据量很多的时候也不会导致程序崩溃
  3. 注意要free掉
  4. sizeOfElement: 数组中每个元素的大小
  5. row: 行数,从1开始计数
  6. col: 列数,从1开始计数
  7. */
  8. void * * Malloctwodimensionarray ( unsigned sizeofelement row unsigned col
  9. void ** DP = ( sizeof ( void *) * row );
  10. for (int i = 0; i< row; i++)
  11. dp[i] = malloc(sizeOfElement*col);
  12. return dp;
  13. }
  14. void freeTwoDimensionArray(void *arr, unsigned row, unsigned col){
  15. int ** intArr = (int **)arr;
  16. for (unsigned i = 0; i < row; ++i){
  17. free(intArr[i]);
  18. }
  19. free(arr);
  20. }



From for notes (Wiz)

Allocating and releasing two-dimensional arrays in 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.