WEB开发网
开发学院软件开发C++ The Standard C Library for Linux:stdlib.h 阅读

The Standard C Library for Linux:stdlib.h

 2008-03-08 12:39:30 来源:WEB开发网   
核心提示:Part Five: <stdlib.h> Miscellaneous Functions By James M. Rogers The last article w

  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

Tags:The Standard Library

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接