[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/memory/c/calloc [Back]  [Original]

std::calloc cppreference.com
cppreference.com

std::calloc

cppreference.com
< cpp | memory | c
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
no section name
no section name
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)



no section name
 
<tbody> </tbody>
void* calloc( std::size_t num, std::size_t size );

num size, ( ).

, () , .

size , ( , )

num
size

. , std::free() std::realloc().

.

- num*size.

, 0,0 ( ).

( C89) ,

OBJ *p = calloc(0, sizeof(OBJ)); //  " "
...
while(1)
{
    //  ,        
    p = realloc(p, c * sizeof(OBJ));
    ... // ,    c    
}

#include <iostream>
#include <cstdlib>

int main()
{
    //      4 int
    int* p1 = (int*)std::calloc(4, sizeof(int));
    //   ,    
    int* p2 = (int*)std::calloc(1, sizeof(int[4]));
    //  ,      
    int* p3 = (int*)std::calloc(4, sizeof *p3);

    if(p2)
        for(int n=0; n<4; ++n) //  
            std::cout << "p2[" << n << "] == " << p2[n] << '\n';

    std::free(p1);
    std::free(p2);
    std::free(p3);
}

:

p2[0] == 0
p2[1] == 0
p2[2] == 0
p2[3] == 0

C calloc

Web Proxy Viewer  |  New URL  |  Original Page