<math.h>文件中对malloc()函数原型:

_CRTIMP void *  __cdecl malloc(size_t);

MSDN中对malloc()的解释:

malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
To return a pointer to a type other than void, use a type cast on the return value.
  The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object.
If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.
Always check the return from malloc, even if the amount of memory requested is small.

即:malloc总是返回void类型的指针,如果需要该指针指向特定的类型必须进行强张类型转换。

下例:

 /*
INPUT: NUM
OUTPUT:从2开始的NUM个素数
*/
#include<stdio.h>
#include<math.h>
#include<malloc.h>
int isprime(long n);// if integer n is prime,return 1,else return 0.
int main()
{
long *ps;
long n;
int i=;
int num;
printf("INPUT THE NUMBER OF PRIMES YOU WANT(START FROM 2):\n");
scanf("%d",&num);
ps=(long*)malloc(num*sizeof(long));// 强制类型转换
if(ps==NULL)
{
printf("NO ENOUGH SPACE TO STORE THESE PRIME NUMBERS:\n");
return ;
}
ps[i++]=;//最小素数进组
n=;//设置遍历初始值
while(i<num)
{
if(isprime(n))
ps[i++]=n;
n+=;//跳过偶数
} //output
printf("\nPRIME NUMBERS:\n");
for(i=;i<num;i++)
printf("%ld\t",ps[i]);
printf("\n");
free(ps);// 释放堆空间.
return ;
} int isprime(long n)
{
int bound;
int i;
bound=(int)sqrt(n);
for(i=;i<=bound;i++)
if(n%i==)
return ;
return ;
}

【C/C++】malloc()的更多相关文章

  1. 【VS开发】malloc申请内存错误分析

    每个进程会有4G的虚拟地址空间, malloc得到的的地址都是虚拟地址, 并且当malloc的时候, 操作系统并不会将实际的内存分配给进程的, 所以malloc只会占用进程自身的虚拟地址空间.我以前也 ...

  2. 【C/C++】malloc和new的区别

    malloc和new的区别 malloc是C语言的内存申请函数.new是C++语言的运算符.所以在.c文件中无法使用new. malloc申请空间时,传递的是size.new申请空间时,传递的是typ ...

  3. 【Tsinghua OJ】多米诺骨牌(domino)问题

    (domino.c/cpp)[问题描述] 小牛牛对多米诺骨牌有很大兴趣,然而她的骨牌比较特别,只有黑色和白色的两种.她觉 得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便是不美观的.现在她有n个 ...

  4. 【硅谷问道】Chris Lattner 访谈录(下)

    [硅谷问道]Chris Lattner 访谈录(下) Chris Lattner 访谈录(下) 话题 Swift 在 Server 和操作系统方面有着怎样的雄心抱负? Swift 与 Objectiv ...

  5. 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂

    C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...

  6. 用户空间和内核空间通讯之【Netlink 中】

    原文地址:用户空间和内核空间通讯之[Netlink 中] 作者:wjlkoorey258 今天我们来动手演练一下Netlink的用法,看看它到底是如何实现用户-内核空间的数据通信的.我们依旧是在2.6 ...

  7. 【Java_基础】JVM内存模型与垃圾回收机制

    1. JVM内存模型 Java虚拟机在程序执行过程会把jvm的内存分为若干个不同的数据区域来管理,这些区域有自己的用途,以及创建和销毁时间. JVM内存模型如下图所示 1.1 程序计数器 程序计数器( ...

  8. 【algo&ds】4.B树、字典树、红黑树、跳表

    上一节内容[algo&ds]4.树和二叉树.完全二叉树.满二叉树.二叉查找树.平衡二叉树.堆.哈夫曼树.散列表 7.B树 B树的应用可以参考另外一篇文章 8.字典树Trie Trie 树,也叫 ...

  9. 【DSP开发】利用CCS5.4开发基于DSP6455的JPEG2000图像解压缩过程

    [DSP开发]利用CCS5.4开发基于DSP6455的JPEG2000图像解压缩过程 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:前端是时间基于VS ...

随机推荐

  1. PhantomJS 基础及示例 (转)

    概述 PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support f ...

  2. 5.【nuxt起步】-swiper组件

    接下来是一个比较常用,也比较重要的组件 swiper,可以自行搜索 vue swiper,有很多开源组件,我这里就复用之前一个熟悉的, 1.新建component/banner.vue 刷新报错: 要 ...

  3. C++ 用libcurl库进行http通讯网络编程 【转】

    http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html C++ 用libcurl库进行http通讯网络编程 目录索引: 一.Lib ...

  4. Solaris设备管理

    接手一台服务器,如何了解它的软硬件配置 数据库可以装在裸设备上,何为裸设备 知识点: 内核 驱动程序 设备命名 设备访问方式 磁盘的管理 内核: 指挥硬件干活的工具,shell翻译官,将人类语言翻译为 ...

  5. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

  6. trigger_error

    PHP trigger_error 自定义错误级别抛出错误. //如果设置成notice或warning则程序继续向下执行,如果是致命级别的错误则终止执行 $num1 = 1; if (is_numb ...

  7. An unexpected error occured when contacting the server .

    I logged into to the arcsight command center ,however I found an unexpected error occurred when cont ...

  8. swich-----case语句的用法

    转:  http://xinzhi.wenda.so.com/a/1517927252619839

  9. bootstrap table api

    http://blog.csdn.net/rickiyeat/article/details/56483577

  10. NTAG 标签

    NTAG 标签 这里描述针对 NTAG213.而 NTAG215/216只是容量不同,其它功能都一样.  UID UID 有 7 bytes.上图中有 9 bytes 的 serial number ...