Operator New
 
Operator to dynamically allocate memory and construct data of a specified type

Syntax
Usage

result = New datatype
or
result = New datatype ( initializers, ... )
or
result = New datatype[ count ]

Parameters

size
Number of bytes to allocate.
initializers
Initial value(s) for the variable.
datatype
Name of the data type to create.
count
Exact number of elements to allocate.

Return Value

A pointer of type datatype to the newly allocated data.

Description

The New operator dynamically allocates memory and constructs a specified data type. For simple types, like integers, an initial value can be given. For types without constructors, initial values can be specified for each field. Types that have constructors can have their constructors called by New as well. If no initializers are given, the default values for those types will be set.

New[] is the array-version of the New operator and allocates enough memory for the specified number of objects. The default constructor for the type will be used to set the initial values for each item.

Objects created with New must be freed with Delete. Memory allocated with New[] must be freed with Delete[], the array-version of Delete. You cannot mix and match the different versions of the operators.

Specifying an initial value of Any, as in new datatype(any) will allocate memory for the type, but not initialize the data. This is only valid on data types that do not have constructors.

Specifying an initial value of Any, as in new datatype[count]{any} will allocate memory for the array, but not initialize the data. This is only valid on data types that do not have constructors.

Example

Type Rational
    As Integer    numerator, denominator
End Type

Scope

    ' Create and initialize a Rational, and store it's address.
    Dim p As Rational Ptr = New Rational(3, 4)

    Print p->numerator & "/" & p->denominator

    ' Destroy the rational and give its memory back to the system. 
    Delete p

End Scope

Scope

    ' Allocate memory for 100 integers, store the address of the first one.
    Dim p As Integer Ptr = New Integer[100]

    ' Assign some values to the integers in the array.
    For i As Integer = 0 To 99
        p[i] = i
    Next

    ' Free the entire integer array.
    Delete[] p

End Scope

Dialect Differences

  • Only available in the -lang fb dialect.

Differences from QB

  • New to FreeBASIC

See also

Сайт ПДСНПСР. Если ты патриот России - жми сюда!


Знаете ли Вы, что "гравитационное линзирование" якобы наблюдаемое вблизи далеких галактик (но не в масштабе звезд, где оно должно быть по формулам ОТО!), на самом деле является термическим линзированием, связанным с изменениями плотности эфира от нагрева мириадами звезд. Подробнее читайте в FAQ по эфирной физике.

НОВОСТИ ФОРУМАФорум Рыцари теории эфира
Рыцари теории эфира
 10.11.2021 - 12:37: ПЕРСОНАЛИИ - Personalias -> WHO IS WHO - КТО ЕСТЬ КТО - Карим_Хайдаров.
10.11.2021 - 12:36: СОВЕСТЬ - Conscience -> РАСЧЕЛОВЕЧИВАНИЕ ЧЕЛОВЕКА. КОМУ ЭТО НАДО? - Карим_Хайдаров.
10.11.2021 - 12:36: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от д.м.н. Александра Алексеевича Редько - Карим_Хайдаров.
10.11.2021 - 12:35: ЭКОЛОГИЯ - Ecology -> Биологическая безопасность населения - Карим_Хайдаров.
10.11.2021 - 12:34: ВОЙНА, ПОЛИТИКА И НАУКА - War, Politics and Science -> Проблема государственного терроризма - Карим_Хайдаров.
10.11.2021 - 12:34: ВОЙНА, ПОЛИТИКА И НАУКА - War, Politics and Science -> ПРАВОСУДИЯ.НЕТ - Карим_Хайдаров.
10.11.2021 - 12:34: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Вадима Глогера, США - Карим_Хайдаров.
10.11.2021 - 09:18: НОВЫЕ ТЕХНОЛОГИИ - New Technologies -> Волновая генетика Петра Гаряева, 5G-контроль и управление - Карим_Хайдаров.
10.11.2021 - 09:18: ЭКОЛОГИЯ - Ecology -> ЭКОЛОГИЯ ДЛЯ ВСЕХ - Карим_Хайдаров.
10.11.2021 - 09:16: ЭКОЛОГИЯ - Ecology -> ПРОБЛЕМЫ МЕДИЦИНЫ - Карим_Хайдаров.
10.11.2021 - 09:15: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Екатерины Коваленко - Карим_Хайдаров.
10.11.2021 - 09:13: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Вильгельма Варкентина - Карим_Хайдаров.
Bourabai Research Institution home page

Боровское исследовательское учреждение - Bourabai Research Bourabai Research Institution