[CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
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 分配一个二维数组的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 数组(Array),二维数组,三维数组
数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...
- [leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum
303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法 算子数组的sum的时候也是和存差不多的逻辑,就是某一部 ...
- [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 ...
- [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 ...
- Java使用Array类创建多维数组
1.创建一维数组 import java.lang.reflect.Array; public class ArrayTest { public static void main(String[] a ...
- c指针与数组,传参问题,指针数组与数组指针的区别,二维数组动态内存分配
一 数组的结构:顺序存储,看谭浩强中的图,牢记 1.数组名指代一种数据结构:数组 现在可以解释为什么第1个程序第6行的输出为10的问题,根据结论1,数组名str的内涵为一种数据结构,即一个长度为10的 ...
随机推荐
- 《极客学院 --NSAttributedString 使用详解-4-UITextKit 简介》学习笔记(待处理)
如果要在富文本中添加图片的话,用UITextKit才能实现. 什么是UITextKit:它就是处理富文本的框架. 什么时候使用UITextKit:比如要实现图文混搭的节目. 在gitHub中 http ...
- sublime text 3 如何安装 package control
sublime text3 是个很好的编辑工具,前端程序员觉得她很好,我是在一次视频中看到她能帮助自动完成很多快捷的操作. 为什么安装? 如果想要给sublime text 中安装别的插件(这里称呼为 ...
- 既生瑜何生亮?ASP.NET MVC VS ASP.NET Web API
Asp.net MVC 与 Asp.net Web API 区别 在我们开发一些web应用时,我们一样可以在MVC Framework 中使用JsonResult 来返回JSON数据,同样也可以处理一 ...
- c++虚函数,纯虚函数,抽象类,覆盖,重载,隐藏
C++虚函数表解析(转) ——写的真不错,忍不住转了 http://blog.csdn.net/hairetz/article/details/4137000 浅谈C++多态性 http://bl ...
- Effective Java 64 Strive for failure atomicity
Principle Failure atomic - A failed method invocation should leave the object in the state that it w ...
- 在IE8等不支持placeholder属性的浏览器中模拟placeholder效果
placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果.下面直接上代码: <!doc ...
- Java小方法
/** * 计算百分比. * @param dividend 被除数 * @param divisor 除数 * @return 结果 */ private String getPercent(lon ...
- 微信公众平台应用开发:方法、技巧与案例--柳峰,Java语言版本
他本人的博客:http://blog.csdn.net/lyq8479 作者简介: 刘运强,网名“柳峰”,资深微信公众平台应用开发工程师,国内微信公众平台应用开发的先驱之一,项目经验丰富.他还是一位资 ...
- R语言Cairo包的使用
Cairo使用起来非常简单,和基础包grDevices中的函数对应. CairoPNG---grDevices:png(). CairoTIFF---grDevices:tiff(). CairoPD ...
- 关于hadoop 配置文件的一些实验
机器配置如下,两台机器,nn2,nn2,搭建基于QJM的高可用集群,zk集群. 如果我在yarn-site.xml中配置的nodemanager的可用资源过少,其他配置如果不一致,那么就会造成提交的j ...