13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j].

这道题让我们写个C语言函数my2DAlloc用来给一个二维数组分配内存,并且让我们尽可能的少调用malloc函数。一个二维数组实际是数组的数组,我们用指针来表示数组,用双指针来表示二维数组。我们首先建立一个一维数组,对于每个位置,再建立一个一维数组,这样我们就得到了一个二维数组,参见如下代码:

int** my2DAlloc(int rows, int cols) {
int **rowptr = (int**)malloc(rows * sizeof(int*));
for (int i = ; i < rows; ++i) {
rowptr[i] = (int*)malloc(cols * sizeof(int));
}
return rowptr;
}

关于释放内存,我们不能仅仅释放rowptr,我们要确保每个cell中的内存也被释放了,参见如下代码:

void my2DDealloc(int **rowptr, int rows) {
for (int i = ; i < rows; ++i) {
free(rowptr[i]);
}
free(rowptr);
}

其实我们还可以在连续的内存块上来分配内存,例如对于一个5行6列的二维数组,我们可以在开头的五个内存块里存上每一行的起始地址,后面的五行数据是连续排列的,一行接着一行,参见代码如下:

class Solution {
public:
int** my2DAlloc(int rows, int cols) {
int header = rows * sizeof(int*);
int data = rows * cols * sizeof(int*);
int **rowptr = (int**)malloc(header + data);
if (rowptr == NULL) return NULL;
int *buf = (int*)(rowptr + rows);
for (int i = ; i < rows; ++i) {
rowptr[i] = buf + i * cols;
}
return rowptr;
}
};

这样申请连续的一段内存空间的好处是只需要调用一次malloc就行,而且在释放的时候也不需要特别的写函数来free,好处还是蛮多的。

[CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组的更多相关文章

  1. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  2. [LeetCode] Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. 数组(Array),二维数组,三维数组

    数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...

  5. [leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum

    303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法 算子数组的sum的时候也是和存差不多的逻辑,就是某一部 ...

  6. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. Java使用Array类创建多维数组

    1.创建一维数组 import java.lang.reflect.Array; public class ArrayTest { public static void main(String[] a ...

  9. c指针与数组,传参问题,指针数组与数组指针的区别,二维数组动态内存分配

    一 数组的结构:顺序存储,看谭浩强中的图,牢记 1.数组名指代一种数据结构:数组 现在可以解释为什么第1个程序第6行的输出为10的问题,根据结论1,数组名str的内涵为一种数据结构,即一个长度为10的 ...

随机推荐

  1. Cordova or Xamarin 用.net开发IOS和Android程序

    Visual Studio 2015 和 Apache Cordova 在开始前,问一下自己下面这些问题: 熟练掌握web技术的开发者比例是多少?(占所有开发者的比例) 熟练掌握移动开发技术(并且使用 ...

  2. JavaScript Patterns 3.5 JSON

    JSON: JavaScript Object Notation {"name": "value", "some": [1, 2, 3]}  ...

  3. 使用easy ui过程中资料(网址)总结

    (1)JQuery Easy Ui 可装载组合框 - ComboBox (2)JQuery Easy Ui DataGrid (3)Easy ui combobox 多级联动 (四级联动) (4)jQ ...

  4. HTTP 协议中的 Content-Encoding 和 Transfer-Encoding(内容编码和传输编码)

    转自:http://network.51cto.com/art/201509/491335.htm Transfer-Encoding,是一个 HTTP 头部字段,字面意思是「传输编码」.实际上,HT ...

  5. Garbage Collectors – Serial vs. Parallel vs. CMS vs. G1 (and what’s new in Java 8)

    转自:http://blog.takipi.com/garbage-collectors-serial-vs-parallel-vs-cms-vs-the-g1-and-whats-new-in-ja ...

  6. 公用表表达式(CTE)引发的改变执行顺序同WHERE条件顺序引发的bug

    以下模拟一下CTE出错 /*测试环境 Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyr ...

  7. 安装node.js+express for win7的Web开发环境配置

    1.安装 node.js. 进入官网的下载地址:http://www.nodejs.org/download/ . 选择Windows Installer或者选择Windows Installer ( ...

  8. cocos2d-x之物理引擎之碰撞监测

    #include "HelloWorldScene.h" USING_NS_CC; #define RED_BIT_MASK    0b0100 #define GREEN_BIT ...

  9. 汇编中call printf参数压栈时错误理解

    EAX, ECX,EDX,EBX均可以32bit,16bit,8bit访问,如下所示: <-------------------EAX------------------------>|& ...

  10. Mongodb--gridfs与分片实验

    1.放置一个大文件到gridfs,查看fs.chunks和fs.files的情况. Step1.开启一台mongod服务. ./mongod --dbpath dbs/master     登录mon ...