📜  PHP | Ds\Stack allocate()函数

📅  最后修改于: 2022-05-13 01:56:19.591000             🧑  作者: Mango

PHP | Ds\Stack allocate()函数

Ds\Stack::allocate()函数是PHP中的一个内置函数,用于为所需容量分配内存。此函数为 Stack 类实例的给定容量分配足够的内存。
句法:

void Ds\Stack::allocate( $capacity )

参数:此函数接受单个参数$capacity ,它是一个整数值,表示需要为其分配容量的值的数量。
返回值:此函数不返回任何值。
下面的程序说明了PHP中的 Ds\Stack::allocate()函数:
方案一:

php
capacity());
   
echo("Allocated space is: ");
   
// Use allocate() function to 
// allocate capacity
$stack->allocate(50);
   
// Display the allocated stack
// capacity
var_dump($stack->capacity());
 
?>


php
capacity());
 
echo("Allocated space is: ");
 
// Use allocate() function to
// allocate capacity
$stack->allocate(5);
 
// Display the stack capacity
var_dump($stack->capacity());
 
echo("Allocated space is: ");
 
// Use allocate() function to
// allocate capacity
$stack->allocate(120);
 
// Display the stack capacity
var_dump($stack->capacity());
?>


输出:
Allocated Space is: int(8)
Allocated space is: int(50)

方案二:

PHP

capacity());
 
echo("Allocated space is: ");
 
// Use allocate() function to
// allocate capacity
$stack->allocate(5);
 
// Display the stack capacity
var_dump($stack->capacity());
 
echo("Allocated space is: ");
 
// Use allocate() function to
// allocate capacity
$stack->allocate(120);
 
// Display the stack capacity
var_dump($stack->capacity());
?>
输出:
Allocated Space is: int(8)
Allocated space is: int(8)
Allocated space is: int(120)

参考: https://www. PHP.net/manual/en/ds-stack.allocate。 PHP