亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

The Standard C Library for Linux:stdlib.h

2019-11-17 05:39:27
字體:
來源:轉載
供稿:網友

  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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品成人免费视频| 亚洲精品999| 国产精品久久一区主播| 欧美黄色片在线观看| 亚洲成色999久久网站| 久久久久久尹人网香蕉| 国产精品99一区| 日韩欧美中文字幕在线播放| 国产精品视频色| 日韩在线播放一区| 色狠狠av一区二区三区香蕉蜜桃| 亚洲欧美日韩图片| 亚洲人免费视频| 亚洲精选在线观看| 国模精品视频一区二区三区| 亚洲一区二区中文| 国产精品网站入口| 亚洲国产中文字幕在线观看| 粉嫩老牛aⅴ一区二区三区| 欧美激情二区三区| 亚洲男人天堂2023| 91国偷自产一区二区三区的观看方式| 日韩欧美亚洲成人| 亚洲人成在线播放| 欧美激情va永久在线播放| 国产成人精品午夜| 国产精品一区二区在线| 久久久久久久久久亚洲| 亚洲欧洲美洲在线综合| 国产一区二区在线播放| 国产做受高潮69| 国产精品ⅴa在线观看h| 秋霞成人午夜鲁丝一区二区三区| 成人免费福利在线| 日韩精品极品视频| 日韩av在线免费播放| 日韩欧美在线视频| 亚洲伊人久久大香线蕉av| 亚洲国产一区二区三区四区| 视频在线观看99| 国产精品久久久久影院日本| 久久精品国产99国产精品澳门| 岛国视频午夜一区免费在线观看| 国产精品免费看久久久香蕉| 日本亚洲精品在线观看| 2024亚洲男人天堂| 亚洲欧洲美洲在线综合| 欧美亚洲另类激情另类| 日韩一区二区三区国产| 亚洲区在线播放| 国产精品白丝jk喷水视频一区| 在线免费观看羞羞视频一区二区| 日韩国产欧美区| 日韩成人在线免费观看| 日韩在线小视频| **欧美日韩vr在线| 中文国产成人精品| 精品自在线视频| 91高潮精品免费porn| 色婷婷综合成人av| 久久人人爽人人爽爽久久| 7777kkkk成人观看| 成人av在线亚洲| 亚洲精品免费一区二区三区| 亚洲精品国产精品国自产在线| 国产91热爆ts人妖在线| 在线播放日韩欧美| 日韩在线播放av| 成人在线国产精品| 久久国产精品亚洲| 国产一区二区三区中文| 亚洲女人天堂成人av在线| 日韩av中文字幕在线播放| 在线观看亚洲视频| 国产精品视频久久久久| 97久久久久久| 欧美一区二区三区免费视| 国产97在线|日韩| 91社影院在线观看| 亚洲精品永久免费精品| 欧美电影在线观看网站| 亚洲激情 国产| 久久综合久久美利坚合众国| 亚洲第一男人av| 神马国产精品影院av| 性夜试看影院91社区| 97人人爽人人喊人人模波多| 国产精品久久网| 久久天天躁日日躁| 狠狠操狠狠色综合网| 在线日韩第一页| 国产成人综合亚洲| 久久久久久久av| 国产成人在线视频| 日韩精品在线电影| 欧美视频在线免费| 欧美激情综合色综合啪啪五月| 久久激情五月丁香伊人| 久久精品一偷一偷国产| 在线播放日韩欧美| 国产精品久久久久久av福利软件| 欧美性一区二区三区| 91精品国产九九九久久久亚洲| 欧美另类99xxxxx| 超碰精品一区二区三区乱码| 国产精品一区二区电影| 欧美一级大胆视频| 91精品国产自产91精品| 一区二区三区久久精品| 欧美激情综合色综合啪啪五月| 国产精品久久久久77777| 日韩免费高清在线观看| 精品久久久久久中文字幕大豆网| 97国产精品久久| 琪琪亚洲精品午夜在线| 日韩精品视频中文在线观看| 日韩中文字幕视频| 日韩欧美亚洲一二三区| 一区二区成人av| 91豆花精品一区| 亚洲男人天堂2023| 国产精品十八以下禁看| 久久91精品国产91久久跳| 欧美中文字幕视频在线观看| 亚洲视频在线播放| 国产精品九九久久久久久久| 亚洲国产另类 国产精品国产免费| 粉嫩av一区二区三区免费野| 亚洲国产精品va| 一本大道亚洲视频| 黄色成人av网| 日韩av中文字幕在线播放| 亚洲影院色在线观看免费| 色噜噜亚洲精品中文字幕| 亚洲丝袜av一区| 亚洲性生活视频| 亚洲国产第一页| 97国产成人精品视频| 亚洲天堂日韩电影| 精品欧美国产一区二区三区| 欧美一区二粉嫩精品国产一线天| 亚洲精品美女在线观看播放| 国产日本欧美在线观看| 永久555www成人免费| 国产欧美婷婷中文| 日韩久久免费视频| 国产午夜精品视频| 亚洲精品xxx| 国产精品吹潮在线观看| 中文字幕日韩精品在线观看| 国产亚洲精品91在线| 国模精品视频一区二区三区| 欧美成人精品影院| 日韩欧美在线免费观看| 亚洲女同精品视频| 亚洲区一区二区| 91精品国产91久久久久久久久| 福利视频第一区| 久久精品国产96久久久香蕉| 精品一区二区三区四区| 亚洲免费视频观看| 8x拔播拔播x8国产精品| 久久综合伊人77777尤物| 亚洲欧美另类在线观看|