Friday, September 5, 2025
HomeLanguagesPHP | DsVector allocate() Function

PHP | Ds\Vector allocate() Function

The Ds\Vector::allocate() function is an inbuilt function in PHP which is used to allocate enough memory for a required capacity. It provides the custom size of the vector to allocate space.

Syntax:

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

Parameters: This function accepts a single parameter $capacity which holds the space to be allocated.

Note: Capacity will remain the same if this value is less than or equal to the current capacity.

Return Value: This function does not return any value.

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

Program 1:




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


Output:

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

Program 2:




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


Output:

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

Reference: http://php.net/manual/en/ds-vector.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