MentOS  0.8.0
The Mentoring Operating System
Macros | Functions
stdlib.h File Reference

Useful generic functions and macros. More...

Go to the source code of this file.

Macros

#define EXIT_SUCCESS   0
 Successful execution of a program.
 
#define EXIT_FAILURE   1
 Unsuccessful execution of a program.
 
#define RAND_MAX   ((1U << 31U) - 1U)
 The maximum value returned by the rand function.
 

Functions

size_t malloc_usable_size (void *ptr)
 Returns the number of usable bytes in the block pointed to by ptr. More...
 
void * malloc (unsigned int size)
 Provides dynamically allocated memory. More...
 
void * calloc (size_t num, size_t size)
 Allocates a block of memory for an array of num elements. More...
 
void * realloc (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 
void free (void *ptr)
 Frees dynamically allocated memory. More...
 
void abort (void)
 Cause an abnormal program termination with core-dump.
 
int setenv (const char *name, const char *value, int overwrite)
 Tries to adds the variable to the environment. More...
 
int unsetenv (const char *name)
 Tries to remove the variable from the environment. More...
 
char * getenv (const char *name)
 Returns the value of the given variable. More...
 
void srand (unsigned x)
 Allows to set the seed of the random value generator. More...
 
unsigned rand (void)
 Generates a random unsigned integer between 0 and RAND_MAX. More...
 
float randf (void)
 Generates a random floating point number between 0 and 1. More...
 
int randint (int lb, int ub)
 Generates a random integer between lb and ub. More...
 
unsigned randuint (unsigned lb, unsigned ub)
 Generates a random unsigned between lb and ub. More...
 
float randfloat (float lb, float ub)
 Generates a random float between lb and ub. More...
 

Detailed Description

Useful generic functions and macros.

Function Documentation

◆ calloc()

void* calloc ( size_t  num,
size_t  size 
)

Allocates a block of memory for an array of num elements.

Parameters
numThe number of elements.
sizeThe size of an element.
Returns
A pointer to the allocated memory.

◆ free()

void free ( void *  ptr)

Frees dynamically allocated memory.

Parameters
ptrThe pointer to the allocated memory.

◆ getenv()

char* getenv ( const char *  name)

Returns the value of the given variable.

Parameters
nameName of the variable.
Returns
A pointer to the value, or NULL if there is no match.

◆ malloc()

void* malloc ( unsigned int  size)

Provides dynamically allocated memory.

Parameters
sizeThe amount of memory to allocate.
Returns
A pointer to the allocated memory.

◆ malloc_usable_size()

size_t malloc_usable_size ( void *  ptr)

Returns the number of usable bytes in the block pointed to by ptr.

Parameters
ptrThe pointer for which we want to retrieve the usable size.
Returns
The number of usable bytes in the block of allocated memory pointed to by ptr. If ptr is not a valid pointer, 0 is returned.

◆ rand()

unsigned rand ( void  )

Generates a random unsigned integer between 0 and RAND_MAX.

Returns
the random value.

◆ randf()

float randf ( void  )

Generates a random floating point number between 0 and 1.

Returns
the random value.

◆ randfloat()

float randfloat ( float  lb,
float  ub 
)

Generates a random float between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ randint()

int randint ( int  lb,
int  ub 
)

Generates a random integer between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ randuint()

unsigned randuint ( unsigned  lb,
unsigned  ub 
)

Generates a random unsigned between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ realloc()

void* realloc ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Parameters
ptrThe pointer to the memory to reallocate.
sizeThe new size for the memory.
Returns
A pointer to the new portion of memory.

It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with a call to free or realloc. Otherwise, the results are undefined.

◆ setenv()

int setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Tries to adds the variable to the environment.

Parameters
nameName of the variable.
valueValue of the variable.
overwriteOverride existing variable value or not.
Returns
Zero on success, or -1 on error with errno indicating the cause.

◆ srand()

void srand ( unsigned  x)

Allows to set the seed of the random value generator.

Parameters
xThe new seed.

◆ unsetenv()

int unsetenv ( const char *  name)

Tries to remove the variable from the environment.

Parameters
nameName of the variable.
Returns
Zero on success, or -1 on error with errno indicating the cause.