Friday, September 5, 2025
HomeLanguagesPHP DsQueue allocate() Function

PHP Ds\Queue allocate() Function

The Ds\Queue::allocate() Function in PHP is used to allocate memory for a Queue class instance. This function allocates sufficient memory for a given capacity for an instance of Queue class.

Syntax:

void public Ds\Queue::allocate ( int $capacity )

Parameters: This function accepts a single parameter $capacity which is an integral value denoting the number of values for which capacity is needed to be allocated.

Return Value: This method does not returns any value.

Below programs illustrate the Ds\Queue::allocate() function in PHP:

Program 1:




<?php 
  
// Declare new PriorityQueue 
$pq = new \Ds\PriorityQueue(); 
  
echo("Allocated Space is: "); 
  
// Use capacity() function 
var_dump($pq->capacity()); 
  
echo("Allocated space is: "); 
  
// Use allocate() function to 
// allocate capacity 
$pq->allocate(50); 
  
// Display the allocated vector 
// capacity 
var_dump($pq->capacity()); 
  
?> 


Output:

Allocated Space is: int(8)
Allocated space is: int(64)

Program 2:




<?php 
   
// Declare new Queue 
$q = new \Ds\Queue(); 
   
echo("Allocated Space is: "); 
   
// Use capacity() function 
var_dump($q->capacity()); 
   
echo("Allocated space is: "); 
   
// Use allocate() function to 
// allocate capacity 
$q->allocate(5); 
   
// Display the allocated vector 
// capacity 
var_dump($q->capacity()); 
   
// Use allocate() function to 
// allocate capacity 
$q->allocate(120); 
   
// Display the allocated vector 
// capacity 
var_dump($q->capacity()); 
   
?> 


Output:

Allocated Space is: int(8)
Allocated space is: int(8)
int(128)

Reference: http://php.net/manual/en/ds-queue.allocate.php

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS