Thursday, January 22, 2026
HomeLanguagesPHP | DsMap allocate() Function

PHP | Ds\Map allocate() Function

The Ds\Map::allocate() function is an inbuilt function in PHP which is used to allocate enough memory for required capacity.

Syntax:

void public Ds\Map::allocate( $capacity )

Parameters: This function accepts single parameter $capacity which indicate number of capacity allocated.

Return Value: This function does not return any values.

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

Program 1:




<?php 
  
// Create new map 
$map = new \Ds\Map(); 
  
// Use capacity() function to 
// display the capacity of map
var_dump($map->capacity()); 
  
// Allocate capacity 
$map->allocate(50); 
  
// Display capacity 
var_dump($map->capacity()); 
  
// Allocate capacity 
$map->allocate(80); 
  
// Display capacity 
var_dump($map->capacity()); 
?> 


Output:

int(8)
int(64)
int(128)

Program 2:




<?php 
  
// Create new map 
$map = new \Ds\Map(); 
  
// Declare capacity array 
$arr = array(10, 20, 30, 40); 
  
// Loop run for every array element 
foreach ($arr as $val) { 
      
    // Allocate capacity 
    $map->allocate($val); 
  
    // Display capacity of map
    var_dump($map->capacity()); 
} 
  
?> 


Output:

int(16)
int(32)
int(32)
int(64)

Reference: https://www.php.net/manual/en/ds-map.allocate.php

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7221 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS