[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的 ...
随机推荐
- html 关于内部是float元素的外部div高度为0的解决方法!
最近编写一个页面的时候遇见一个问题,外部div是block的,而内部元素是float的,大家应该都知道float的元素是没有实际高度的,就算你设置了float元素的高度他也不会撑开外部block元素的 ...
- 主流Web服务器一览
概念Web服务器是可以向发出请求的浏览器提供文档的程序. 1.服务器是一种被动程序:只有当Internet上运行在其他计算机中的浏览器发出请求时,服务器才会响应. 2.最常用的Web服务器是Apach ...
- 颠覆式前端UI开发框架:React
转自:http://www.infoq.com/cn/articles/subversion-front-end-ui-development-framework-react/ 基于HTML的前端界面 ...
- 在Windows8下安装SQL Server 2005无法启动服务
因为尝鲜安装了Windows8,的确很不错,唯一的遗憾就是不支持Sql Server 2005的安装.找了很多办法,基本上都有缺陷.现在终于找到一种完全正常没有缺陷的办法了,和大家分享一下. 0.一定 ...
- Linux常用压缩和解压命令
.tar 解包 tar xvf filename.tar.tar 打包 tar cvf filename.tar dirname.gz 解压1 gunzip filename.gz.gz 解压2 gz ...
- Linux LDAP Server--->Clients配置
Linux Ldap Configuration LDAP Server Base Software & SysTem Info SysTem Info 系统版本:centos 6.4 LDA ...
- ES5严格模式(Strict mode)
严格模式(Strict mode)是由ECMA-262规范定义的新兴JavaScript标准,第五版发布于2009年12月.旨在改善错误检查功能并且标识可能不会延续到未来JavaScript版本的脚本 ...
- Centos 部署Keepalive高可用软件
Keepalive安装部署 一.环境介绍 1)Centos6.4 2) keepalived-1.2.12 3) 主备机的ip Master:172.31.100.5 Slave: 172.31. ...
- mysql force index() 强制索引的使用
mysql force index() 强制索引的使用 之前跑了一个SQL,由于其中一个表的数据量比较大,而在条件中有破坏索引或使用了很多其他索引,就会使得sql跑的非常慢... 那我们怎么解决呢? ...
- sqlserver 2005 分布式架构 对等事务复制 .
http://www.cnblogs.com/qanholas/archive/2012/03/22/2412444.html 一.为什么要使用对等事务复制 首先要说明的是使用sqlserve ...