C Standard Libraries C++

malloc()

Declaration

void* malloc(size_t qMemorySize);

Description

Dynamically allocates an array of bytes in memory. The argument "qMemorySize" defines the number of bytes to be allocated. The value returned is a pointer to the allocated memory.

Example

#include <iostream>
#include <cstdlib>

int main() {
    using namespace std;

    char* cpArray = (char*)malloc(9);
    cpArray[0] = 'X';
    cpArray[1] = 'o';
    cpArray[2] = 'a';
    cpArray[3] = 'X';
    cpArray[4] = '.';
    cpArray[5] = 'n';
    cpArray[6] = 'e';
    cpArray[7] = 't';
    cpArray[8] = 0;
    cout << cpArray << endl;

    // Deallocate the memory
    free(cpArray);

    return 0;
}

Output

malloc() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.