13.9 编写支持对齐分配的malloc和free函数,分配内存时,malloc函数返回的地址必须都能被2的n次方整除。

解法:

  一般来说,使用malloc,我们控制不了分配的内存会在堆里哪个位置。我们只会得到一个指向内存块的指针,指针的起始地址不定。

要克服这些限制条件,我们必须申请足够大的内存,要大到可以返回可被指定数值整除的内存地址。

假设需要一个100字节的内存块,我们希望它的起始地址为16的倍数。需要额外分配多少内存才够用呢?我们需要额外分配15字节。有了这15字节,加上紧随其后的100字节,就能得到可被16整除的内存地址,以及100字节的可用空间。

careercup-C和C++ 13.9的更多相关文章

  1. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  4. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  5. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  6. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

  7. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  8. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  9. [CareerCup] 13.7 Node Pointer 节点指针

    13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...

  10. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

随机推荐

  1. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  2. CSS布局中——导航是非常常见的

    导航绝对是页面布局中最常见的,为了不用每次去写,稍微贴个简单的导航模版出来,方便以后使用. <title>CSS菜单</title> <style type=" ...

  3. CPU affinity 进程和线程的亲缘性

    设置Processor Affinity 作用: 1.进程和线程的亲缘性(affinity),使进程或线程在指定的CPU(核)上运行.(比如程序A,在第4个核心上运行) 2.设置进程 或者 线程, 使 ...

  4. java中接口与多重继承的关系

    在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和int ...

  5. POJ 3177 Redundant Paths 边双(重边)缩点

    分析:边双缩点后,消环变树,然后答案就是所有叶子结点(即度为1的点)相连,为(sum+1)/2; 注:此题有坑,踩踩更健康,普通边双缩短默认没有无向图没有重边,但是这道题是有的 我们看,low数组是我 ...

  6. 用pip爽久了,竟然完了easy install安装过程了

    新换了mac,装python环境时才发现,一直用pip,反而忘了easy_install的安装方法了.这里记录一下: 1.下载ez_install.py文件:https://bootstrap.pyp ...

  7. js特效第九天

    offset家族(获取元素尺寸) offsetWidth得到对象的宽度,自己的,与他人无关 offsetWidth = width+border+padding,不包含margin offsetHei ...

  8. n个筛子的点数

    题目:把n个筛子扔到地上,所有筛子朝上一面的点数之和为s,输入n,打印出s的所有可能的值出现的概率. 分析: 方法1:递归. 要求概率,那么我们首先只需要求出每个s出现的次数/(6^n).怎么求s的次 ...

  9. 打印出从1到最大的n位十进制数

    首先这一题会溢出,要考虑的大数问题.所以不能用简单的是int类型数来表示(32位无符号int 范围是0x00000000···0xFFFFFFFF),下面主要是非递归的实现代码,自己做了注释方便以后回 ...

  10. Linux内核,文件系统移植过程中出现的一些问题与解决办法

    1.bootm地址和load address一样 此种情况下,bootm不会对uImage header后的zImage进行memory move的动作,而会直接go到entry point开始执行. ...