分享
 
 
 

The Standard C Library for Linux:stdlib.h

王朝c/c++·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有