动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C/C++教程 ]的信息



本月文章推荐
..
.进程和线程编程.
.C++之父Bjarne谈C++的未来发展.
.汇编语言的高级语言特性.
.用Delphi4的QReport部件生成报表.
.C语言图形处理.
.C++ Builder 初学问与答(十六).
.C语言编程常见问题解答之程序的编.
.C语言嵌入系统编程修炼-性能优化.
.用OLE操作Excel.
.autoconf 和automake生成Makefil.
.nsd启动.
.钩子的应用: 程序运行监视.
.kingofark关于学习C++和编程的50.
.VC Studio 使用技巧大全.
.API之控件与消息函数.
.C++ sizeof 使用规则及陷阱分析.
.深度探索C++对象模型(10).
.在C语言中引入类的概念.
.C程序开发初级讲座之分支结构.

The Standard C Library for Linux:stdlib.h

发表日期:2008-3-8 |



  Part Five: <stdlib.h> Miscellaneous Functions
By James M. Rogers

--------------------------------------------------------------------------------

The last article was on <ctype.h> character handling. This article is on <stdlib.h> which contains many small sections: integer math, sorting and searching, random numbers, string to number conversions, multibyte character conversions, memory allocation and environmental functions. Because this library contains so many small yet very important sections I want to discuss each of these groups in its own section. An example will be given in each section below because these functions are too diverse to have a single example for all of them.

I am assuming a knowledge of c programming on the part of the reader. There is no guarantee of accuracy in any of this information nor suitability for any purpose.

As always, if you see an error in my documentation please tell me and I will correct myself in a later document. See corrections at end of the document to review corrections to the previous articles.

Integer Math

#include <stdlib.h>
int abs(int x);
div_t div(int numerator, int denominator);
long int labs(long int x);
ldiv_t ldiv(long int numerator, long int denominator);

int x
int numerator
int denominator
The long int versions are the same as the three int arguments.
abs returns the absolute value of the argument.
div returns a data strUCture that contains both the quotient and remainder.
labs is the long version of the abs function.
ldiv is the long version of the div function.

Integer math is math using whole numbers. No fractions. This is math from the fourth grade. If you remember the numerator is divided by the denominator and the answer is the quotient with the left over stuff being the remainder then you have got it. The div_t and ldiv_t are structures that hold the quotient and the remainder. These structures look like this:

struct div_t {
int quot;
int rem;
}

struct ldiv_t {
long int quot;
long int rem;
}

These types are already defined for you in the library. The example file shows a few ways to use these four functions.

String to Number Conversions

#include <stdlib.h>
double atof(const char *string);
int atoi(const char *string);
long int atol(const char *string);
double strtod(const char *string, char **endptr);

long int strtol(const char *string, char **endptr, int base);
unsigned long int strtoul(const char *string, char **endptr, int base);

const char *string
char **endptr
int base
atof is acsii to float conversion.
atoi is ascii to integer conversion.
atol is acsii to long conversion.
strtod is string to double conversion.
strtol is string to long and the string can contain numbers in bases other than base 10.
strtoul is the same as strtol, except that it returns an unsigned long.

If you are reading in a number from user input then you will need to use these routines to convert from the digits '1' '2' '3' to the number 123. The easiest way to convert the other way, from a number to a string, is to use the sprintf() function.

The example program is just a sample of use of each of the above commands.

Searching and Sorting

#include <stdlib.h>
void qsort(void *base, size_t num_of_objs, size_t size_of_obj, int (*compar)(const void *, const void *));
void bsearch(const void *key, void *base, size_t num_of_objs, size_t size_of_obj, int (*compar)(const void *, const void *));

void *base
size_t num_of_objs
size_t size_of_obj
const void *
const void *key
qsort will sort the array of strings using a comparison function that you write yourself.
bsearch will search the sorted array using a comparison function that you write yourself.

You do not need to write your own sorting routines yourself. Through the use of these functions you can sort and search through memory arrays.

It is important to realize that you must sort an array before you can search it because of the search method used.

In order to generate the information to have something to sort I combined this example with the random number generation. I initialize a string array with a series of random numbers and then sort it. I then look to see if the string 1000 is in the table. I finally print out the sorted array.

Memory Allocation

#include <stdlib.h>
void *calloc(size_t num_of_objs, size_t size_of_objs);
void free(void *pointer_to_obj);
void *malloc(size_t size_of_object);
void *realloc(void *pointer_to_obj, size_t size_of_obj);

size_t num_of_objs
size_t size_of_objs
void *pointer_to_obj
free will free the specified memory that was previously allocated. You will core dump if you try to free memory twice.
malloc will allocate the specified number of bytes and return a pointer to the memory.
calloc will allocate the array and return a pointer to the array.
realloc allows you to change the size of a memory area "on-the-fly". You can shrink and grow the memory as you need, be aware that trying to Access memory beyond what you have allocated will cause a core dump.


Runtime memory allocation allows you to write a program that only uses the memory that is needed for that program run. No need to change a value and recompile if you ask for the memory at runtime. Also no need to setup arrays to the maximum possible size when the average run is a fraction the size of the maximum.

The danger of using memory this way is that in complex programs it is easy to forget to free memory when you are done with it. These "memory leaks" will eventually cause your program to use all available memory on a system and cause a dump. It is also important to not assume that a memory allocation will always work. Attempting to use a pointer to a memory location that your program doesn't own will cause a core dump. A more serious problem is when a pointer is overwriting your own programs memory. This will cause your program to work very erratically and will be hard to pinpoint the exact problem.

I had to write two different examples to demonstrate all the diversity of these functions. In order to actually demonstrate their use I had to actually program something halfway useful.

The first example is a stack program that allocates and deallocates the memory as you push and pop values from the stack.

The second example reads any file into the computers memory, reallocating the memory as it goes. I left debug statements in the second program so that you can see that the memory is only reallocated when the program needs more memory.

Environmental

#include <stdlib.h>
void abort ( void );
int atexit ( void ( *func )( void ) );
void exit ( int status);
char *getenv( const char *string);
int setenv ( const char *name, const char *value, int overwrite );
int unsetenv ( const char *name, const char *value, int overwrite );
int system ( const char *string );

void
void (*func)(void)
int status
const char *string
const char *name
上一篇:TLocateOptions的用法 人气:248
下一篇:Turbo C作图一例 人气:331
浏览全部C/C++的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-24 Sablog-X v2.0 预览版
2008-7-24 帝国备份王EmpireBak 2008 正式版
2008-7-24 网趣网上购物系统时尚版 v8.2
2008-7-24 纵横B2B电子商务系统XYECS!B2B v
2008-7-24 e路小说小偷 v1.2.0723
2008-7-24 凌风美女图片站程序 v2.2
2008-7-24 TOM15电影收索程序
2008-7-24 清风信息自动采集生成系统 v1.0
2008-7-24 QQ邮箱编辑器 v1.0 (小小菜刀ASP
2008-7-19 UltraEdit 简体中文增强版 14.10
2008-7-19 CentOS 5.2 i386 LiveCD
2008-7-19 Snapture多功能相机 v1.4
2008-7-19 iAcces中文输入法 v1.0Build016
2008-7-19 Cookbook烹饪秘籍 v2.5
2008-7-19 苹果专用DVD转换工具 v1.1.59汉化
2008-7-19 Modem修复软件ZiPhone修改版04.0
2008-7-19 AgileMessenger即时通讯工具美化
2008-7-19 Sketches画图软件 v0.7b6破解版


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵