'free functions'에 해당되는 글 1건

  1. 2021.06.26 Watcom C Library : free Functions, _freect, freopen, frexp

 

 

Watcom C Library Reference : free Functions, _freect, freopen, frexp

 

 

 

 

free Functions

 

Synopsis : #include <stdlib.h>   For ANSI compatibility (free only)

              #include <malloc.h>  Required for other function prototypes

              void free( void *ptr );

              void _bfree( __segment seg, void __based (void) *ptr );

              void _ffree ( void __far *ptr );

              void _nfree ( void __near *ptr );

 


Description : When the value of the argument ptr is NULL, the free function does nothing; otherwise, the free function deallocates the memory block located by the argument ptr which points to a memory block previously allocated through a call to the appropriate version of calloc, malloc or realloc. After the call, the freed block is available for allocation.


Each function deallocates memory from a particular heap, as listed below :

 Function  Heap
 free  Depends on data model of the program
 _bfree  Based heap specified by seg value
 _ffree  Far heap (outside the default data segment)
 _nfree  Near heap (inside the default data segment)

 

In a large data memory model, the free function is equivalent to the _ffree function; in a small data memory model, the free function is equivalent to the _nfree function.

 


Returns : The free functions return no value.

 

 

See Also : calloc Functions, _expand Functions, halloc, hfree, malloc Functions, _msize Functions, realloc Functions, sbrk

 


Example :

#include <stdio.h> 

#include <stdlib.h>


void main( )

{
    char     *buffer;

 

    buffer = (char *) malloc ( 80 );

    if( buffer == NULL) {
        printf( "Unable to allocate memory\n" );

    } else {
        /* rest of code goes here */
        free ( buffer );  /* deallocate buffer */

    }

 

Classification : free is ANSI; _bfree, _ffree, and _nfree are not ANSI
Systems : free - All 

             _bfree - DOS/16, Win/16, QNX/16, OS/2 1.x(all) 

             _ffree - DOS/16, Win/16, QNX/16, OS/2 1.x(all) 

             _nfree - DOS, Win, QNX, OS/2 1.x, OS/2 1.x(MT), OS/2 2.x, NT

 

 

 

 

 

_freect

 

Synopsis : #include <malloc.h> 

              unsigned int _freect ( size_t size );

 


Description : he _freect function returns the number of times that _nmalloc (or malloc in small data models) can be called to allocate a item of size bytes. In the tiny, small and medium memory models, the default data segment is only extended as needed to satisfy requests for memory allocation. Therefore, you will need to call _nheapgrow in these memory models before calling _freect in order to get a meaningful result.

 

 

Returns : The _freect function returns the number of calls as an unsigned integer.

 

 

See Also : calloc, _heapgrow Functions, malloc Functions, _memav1, _memmax

 


Example :

#include <stdio.h> 

#include <malloc.h>


void main( )

{
    int     i;

 

    printf ("Can allocate %u longs before _nheapgrow\n", _freect ( sizeof( long ) ) ); 

 

    _nheapgrow( );

    printf ( "Can allocate %u longs after _nheapgrow\n", _freect ( sizeof( long ) ) ); 

 

    for( i = 1; i < 1000; i++ ) {
        _nmalloc( sizeof( long ) );

    }
    printf( "After allocating 1000 longs : \n" );

    printf( "Can still allocate %u longs\n", _freect ( sizeof( long ) ) );

}

 


produces the following :
Can allocate 0 longs before _nheapgrow 

Can allocate 10447 longs after _nheapgrow 

After allocating 1000 longs :

Can still allocate 9447 longs

 

Classification : WATCOM
Systems : All

 

 

 

 

 

freopen

 

Synopsis : #include <stdio.h> 

              FILE *freopen ( const char *filename, const char *mode, FILE *fp );


Description : The stream located by the fp pointer is closed. The freopen function opens the file : whose name is the string pointed to by filename, and associates a stream with it. The stream information is placed in the structure located by the fp pointer.


The argument mode is described in the description of the fopen function.

 

 

Returns : The freopen function returns a pointer to the object controlling the stream. This point must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, freopen returns NULL. When an error has occurred, errno contains a value indicating the type of error that has been detected.

 

 

See Also : fclose, fcloseall, fdopen, fopen, _fsopen

 


Example :

#include <stdio.h>


void main( )

{
    FILE     *fp;

    int       c;


    fp = freopen( "file", "r", stdin );

    if( fp != NULL) {

        while( (c = fgetchar( ) ) != EOF )
            fputchar( c );

        fclose( fp );

    }

}

 

Classification : ANSI
Systems : All

 

 

 

 

 

frexp

 

Synopsis : #include <math.h> 

              double frexp( double value, int *exp );

 


Description : The frexp function breaks a floating-point number into a normalized fraction and an integral power of 2. It stores the integral power of 2 in the int object pointed to by exp.

 

 

Returns : The frexp functionreturns the value of x, such that x is a double with magnitude in the interval [0.5, 1] or zero, and value equals x times 2 raised to the power *exp. If value is zero. then both parts of the result are zero.

 

 

See Also : ldexp, modf

 


Example :

#include <stdio.h> 

#include <math.h>


void main( )

{
    int       expon; 

    double  value;


    value = frexp ( 4.25, &expon );

    printf( "%f  %d\n", value, expon );

 

    value = frexp ( -4.25, &expon );

    printf( "%f  %d\n", value, expon );

}

 


produces the following :
0.531250  3 

-0.531250  3

 

Classification : ANSI
Systems : All

 

 

 

 

 

 

 

 

This manual describes the WATCOM C library for DOS, Windows, and OS/2, It includes the Standard C Library (as defined in the ANSI C Standard).

 

WATCOM C Language Reference manual describes the ANSI C Programming language and extensions to it which are supported by WATCOM C/C++ (32bit)

 

 

 

 

728x90
반응형
Posted by 전화카드
,