C Standard Libraries C++

realloc()

Declaration

void* realloc(void* vpOldMemory, size_t qSizeInBytes);

Description

Allocate a new memory buffer and deallocate the old one. The argument "vpOldMemory" points to the old memory allocation and "qSizeInBytes" is the size of the new allocation. If the reallocation fails, NULL is returned. If the reallocation is successful, a pointer to the new allocation is returned.

Example

#include <iostream>
#include <cstdlib>

int main() {
    using namespace std;

    char* cpOldMemory = (char*)malloc(10);

    char* cpMemory = (char*)realloc(cpOldMemory, 20);
    if (cpMemory == 0) {
        free(cpOldMemory);
        cout << "realloc failed" << endl;
    } else {
        cout << "realloc successful" << endl;
    }

    free(cpMemory);

    return 0;
}

Output

realloc() Output
 

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