[C/C++] 配置2D陣列 (Allocate 2D Array)
最近要整合C/C++高人寫的Source Code,發現我自己好像斷手斷腳XD,因為要搞較為底層的記憶體空間配置⋯⋯先前寫Objective C多麼愜意,用不到的記憶體空間,系統會自動去釋放,但是在C/C++就要自己寫code處理呢!
現在想要配置2D陣列 (Allocate 2D Array),要怎麼做才好辦事呢?
以後都來這兒複製貼上吧XD~
/**
Theme: Allocate 2D Array
IDE: Xcode 9
Language: C
Date: 107/03/30
Author: HappyMan
Blog: https://cg2010studio.com/
*/
#include
#include
float** createArray(int m, int n)
{
float* values = calloc(m * n, sizeof(float));
float** rows = malloc(n * sizeof(float*));
for (int i = 0; i < n; ++i) {
rows[i] = values + i*m;
}
return rows;
}
void destroyArray(float** arr)
{
free(*arr);
free(arr);
}
int main()
{
float** happyArr = createArray(2,3);
happyArr[0][0] = 1;
happyArr[0][1] = 1;
happyArr[1][0] = 2;
happyArr[1][1] = 2;
happyArr[2][0] = 3;
happyArr[2][1] = 3;
destroyArray(happyArr);
return 0;
}
我發現顯示code的外掛會讓下面兩行出問題~
#include <stdio.h>
#include <stdlib.h>
最後記得釋放記憶體唷~
![[C++] 如何引用C標頭檔.png](https://cg2010studio.com/wp-content/uploads/2018/03/c-e5a682e4bd95e5bc95e794a8ce6a899e9a0ade6aa94.png?w=540)
![[Xcode] C++ 讀取所在目錄檔案](https://cg2010studio.com/wp-content/uploads/2018/03/xcode-c-e8ae80e58f96e68980e59ca8e79baee98c84e6aa94e6a188.png?w=540)


![[iOS] 使用HTTP的POST方法提交的表單](https://cg2010studio.com/wp-content/uploads/2018/03/ios-e4bdbfe794a8httpe79a84poste696b9e6b395e68f90e4baa4e79a84e8a1a8e596ae.png?w=540)
![[watchOS] 使用加速度計 (Using Accelerometer).jpg](https://cg2010studio.com/wp-content/uploads/2018/02/watchos-e4bdbfe794a8e58aa0e9809fe5baa6e8a888-using-accelerometer.jpg?w=540)


![[iOS] Bundle name 和 Bundle display name.png](https://cg2010studio.com/wp-content/uploads/2018/01/ios-bundle-name-e5928c-bundle-display-name.png?w=540)
![[iOS] Bundle name 和 Bundle display name.PNG](https://cg2010studio.com/wp-content/uploads/2018/01/ios-bundle-name-e5928c-bundle-display-name1.png?w=540)

HappyMan・迴響