Ask about "new" and "delete"

General FreeBASIC programming questions.
lrcvs
Posts: 575
Joined: Mar 06, 2008 19:27
Location: Spain

Ask about "new" and "delete"

Postby lrcvs » Mar 26, 2020 21:50

Hi, all:

My question is about "delete".
According to the manual, "delete" deletes the portion of memory created by "new".
According to my example, there are values that are not removed, they are still in memory.
The operation of "delete" is correct?

Code: Select all

CLS

'We assign a portion of memory to the variable "p", creating a pointer with 10 indexes.
DIM p AS INTEGER PTR = NEW INTEGER [10]
DIM i AS INTEGER

'we check the initial values of the 10 indices = 0, Ok!
FOR i = 0 TO 9
    PRINT i,p[i]
NEXT i
print

'we assign a value to each index, Ok!
FOR i = 0 TO 9
    p[i] = i * 5
    PRINT i,p[i]
NEXT

'assume that here we remove the pointer variable "p" from memory, OK ???
DELETE p '????
PRINT

'We check again the values of the indices of the variable "p", supposedly the space and the values have been removed from memory.
FOR i = 0 TO 9
    PRINT i,p[i]
NEXT i

SLEEP


'Why are some of the indices and values still in memory?

Thanks
paul doe
Posts: 1139
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Ask about "new" and "delete"

Postby paul doe » Mar 26, 2020 22:01

What happens is simply that the memory is marked as 'free' by the underlying OS (since we reclaimed it when we called new). The values are not changed unless the memory is occupied again with other data (which in your example isn't the case; processes cannot access the memory space of other processes). The values aren't cleared, but they are indeed freed, so assume nothing about the contents of them after you called delete on an address.
lrcvs
Posts: 575
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Ask about "new" and "delete"

Postby lrcvs » Mar 26, 2020 22:27

Hi, paul doe:

ok, thanks for your explanation
it is my mistake of interpretation.

Cheers
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Ask about "new" and "delete"

Postby fxm » Mar 26, 2020 22:42

Just an important note about your code:
- 'NEW' must be finished with a 'DELETE'.
- 'NEW []' must be finished with a 'DELETE []'.
So the correct code line is : 'DELETE [] p'.

Fortunately, in the particular case of a data-type without destructor (explicit or implicit) as 'INTEGER', there is no consequence, but if using for example the 'STRING' data-type (which has an implicit destructor), all string data being non-destructed, except for the first string, would induce memory leaks.
badidea
Posts: 1901
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Ask about "new" and "delete"

Postby badidea » Mar 26, 2020 22:46

Note that new and delete do more than allocating and freeing memory. They also call the constructor and destructor as I recently learned (viewtopic.php?f=3&t=28409&p=270043). This is unimportant for your example code posted, but if you start using it in combination with classes (UDT), it is.

Edit: fxm was faster.
lrcvs
Posts: 575
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Ask about "new" and "delete"

Postby lrcvs » Mar 27, 2020 6:18

Hi, all:

Ok, thanks everyone!

Cheers

Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests