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. 建立JDBC的环境配置和相关下载(Mac)

    首先已经安装好XMAPP和Workbench. 1.打开MySQL,然后打开Workbench: 然后我们需要下载MySQL的JDBC驱动. 1.进入MySQL官网:http://dev.mysql. ...

  2. mysql登录和连接 权限

    在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...

  3. 利用 cos 组件实现jsp中上传附件

    需求:在web功能中附件上传功能为最基本的功能之一,所以用cos组件做了一个附件上传的demo.附件上传功能的实现可以利用其它的java组件实现,相关资料网上比较多. 说明步骤:下载组件并安装 --& ...

  4. 利用File类过滤器列出目录下的指定目录或文件

    需求:列出d盘下的全部txt文件 实现方法:利用File类的过滤器功能 package com.test.common.util; import java.io.File; import java.i ...

  5. JavaScript Patterns 2.11 Writing Comments

    Document all functions, their arguments and return values, and also any interesting or unusual algor ...

  6. 标准sql语句,学习

    标准SQL语句总结标准SQL语句总结,标准SQL语言基本上适用于下面所列出的数据库软件 -------------------------------------------------------- ...

  7. myeclipse关闭html,jsp等页面的可视化编辑器

    myeclipse打开html,jsp等页面时,有的是默认用可视化编辑器打开的,这样打开会显得很慢,只要关闭可视化编辑器就会快很多了,方法如下: 1,选择菜单: windows -> prefe ...

  8. 复制文件的bat脚本

    数据库备份到不同的机器上的一段脚本 使用系统的copy 命令来复制单个文件 如果要复制某个文件夹,则使用xcopy 命令 set date=%Date:~0,4%%Date:~5,2%%Date:~8 ...

  9. mysql日志类型

    在MySQL中共有4中日志:错误日志.二进制日志.查询日志和慢查询日志 一.错误日志 错误日志名 host_name.err,并默认在参数DATADIR指定的目录中写入日志文件.可使用 --log-e ...

  10. 从mysql数据表中随机取出一条记录

    核心查找数据表代码: ; //此处的1就是取出数据的条数 但这样取数据网上有人说效率非常差的,那么要如何改进呢 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. S ...