The Standard C Library for Linux:stdlib.h

2016-02-19 12:35 1 1 收藏

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

【 tulaoshi.com - 编程语言 】


  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

来源:https://www.tulaoshi.com/n/20160219/1601232.html

延伸阅读
标签: Web开发
使用库元素必须首先在DW中正确建立站点。 库被设计用来使重复性的工作更快、更容易并尽可能地无差错。 任何网页中的元素,无论文本还是图形均可以指定为库元素。 库元素可以用来放置在同一个站点内的任何页面中,而不需要重新输入文本或插入图片等。 可以在任何时候修改库文件。编辑完成,保存,DW会同时更新所有...
标签: Web开发
    使用库元素必须首先在DW中正确建立站点。 库被设计用来使重复性的工作更快、更容易并尽可能地无差错。 任何网页中的元素,无论文本还是图形均可以指定为库元素。 库元素可以用来放置在同一个站点内的任何页面中,而不需要重新输入文本或插入图片等。 可以在任何时候修改库文件。编辑完成,保存,DW...
处理 C++ 中的异常会在语言级别上碰到少许隐含限制,但在某些情况下,您可以绕过它们。学习各种利用异常的方法,您就可以生产更可靠的应用程序。保留异常
crypt(将密码或数据编码) 相关函数 getpass表头文件 #define _XOPEN_SOURCE#include 定义函数 char * crypt (const char *key,const char * salt);函数说明 crypt()将使用Data EncryptionStandard(DES)演算法将参数key所指的字符串加以编码,key字符串长度仅取前8个字符,超过此长度的字符没有意义。参数salt为两个字符组成的字符串,由a-z...
    前面提到,Oracle10g重建Procedure的处理有所增强,最初看到这个增强的时候,我想这个增强是否可以减少困扰已久的Library Cache的竞争呢?     我们看一下以下测试,首先在第一个session执行操作: SQL create or replace PROCEDURE pining 2 IS 3 BEGIN 4 NULL; 5 END; 6 / Procedure created. SQL SQL ...

经验教程

749

收藏

97
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部