Share this page

# malloc

Memory Allocation Functions

Allocates a block of memory.

    void *malloc( size_t num_bytes);

num_bytes | The size of the block of memory that is allocated.

## Return Values

Returns a void type pointer to the allocated space. If the system could not allocate the requested block of memory or if num_bytes is 0, returns a NULL pointer.

You must free the memory when you are finished with it: `free(bufferPointer);`

If you try to create a local variable that is too large or if the total size of the local variables is to large, the C interpreter throws an error "Too many local variables" instead of indicating that the buffer is too large. For example, this is an error: `char buffer[4096*8];.`

To allocate large buffers, use malloc instead of declaring the buffer as a local variable.</p>

For malloc details, refer to your C language documentation.

## Example

The following example uses malloc to allocate a buffer of length 1024.

Copy code

    #include <malloc.h>
        char * buf;
        if ((buf = (char *)malloc(1024 * sizeof(char))) == NULL) {
            lr_output_message ("Insufficient memory available");
            return -1;
        }
        lr_output_message ("Memory allocated . Buffer address = %.8x", buf);
        // Do something with the buffer here ...
        // Now free the buffer
        free(buf);

    Example: Output:
    Action.c(11): Memory allocated. Buffer address = 007b9d88

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: