POV-Ray : Newsgroups : povray.off-topic : c++ memory question Server Time
5 Nov 2024 05:23:47 EST (-0500)
  c++ memory question (Message 1 to 10 of 22)  
Goto Latest 10 Messages Next 10 Messages >>>
From: Samuel Benge
Subject: c++ memory question
Date: 2 Oct 2007 18:45:30
Message: <4702ca0a@news.povray.org>
Hi,

It's probably a stupid question, but here goes. I know that for every 
"new", there must be a delete. I don't deal with malloc statements, so 
let's disregard that branch of memory management.

So here's the question. If I create an array like so: int i[20];, and 
then proceed to assign values for each entry, must I then delete it at 
some point? I didn't use a "new" flag to initialize the array. Is the 
memory used by that array freed after the program is terminated?

I can't find information regarding this, as all the texts I found dealt 
with the "new" and "delete []" statements.

I ask this because my Light's Out game seem to use more and more memory 
each time it is run and closed, but the memory consumption actually goes 
back down to what it originally was after the 11th or 12th time. I don't 
get why that would happen...

Sam


Post a reply to this message

From: Vincent Le Chevalier
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:45:37
Message: <4702ca11$1@news.povray.org>

> Hi,
> 
> It's probably a stupid question, but here goes. I know that for every 
> "new", there must be a delete. I don't deal with malloc statements, so 
> let's disregard that branch of memory management.
> 
> So here's the question. If I create an array like so: int i[20];, and 
> then proceed to assign values for each entry, must I then delete it at 
> some point? I didn't use a new flag. Is the memory used by that array 
> freed after the program is terminated?
> 

What you make whith the instruction int i[20]; is a static array, that 
is allocated in the stack if I remember correctly. You do not have to 
manage the memory in the stack, so no free or delete.

Try googling for "static dynamic array C++", it should bring you 
something useful...

Hope this helps

-- 
Vincent


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:46:08
Message: <op.tzldjaptcs6ysw@e6600>
On Wed, 03 Oct 2007 00:36:05 +0200, Samuel Benge  
<stb### [at] THIShotmailcom> wrote:
> If I create an array like so: int i[20];, and then proceed to assign  
> values for each entry, must I then delete it at some point?

No. Indeed, you must not, as it is not dynamically allocated.


> Is the memory used by that array freed after the program is terminated?

It is freed when 'i' goes out of scope. If 'i' is in namespace scope, it  
goes out of scope when the program ends.



-- 
FE


Post a reply to this message

From: Samuel Benge
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:47:33
Message: <4702ca85@news.povray.org>
Vincent Le Chevalier wrote:
> What you make whith the instruction int i[20]; is a static array, that 
> is allocated in the stack if I remember correctly. You do not have to 
> manage the memory in the stack, so no free or delete.

Hmm, that's good to know.

> Try googling for "static dynamic array C++", it should bring you 
> something useful...
> 
> Hope this helps

Yes, it does. Thanks! I'm off to use Google for the umpteenth time today!

Sam


Post a reply to this message

From: Samuel Benge
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:49:02
Message: <4702cade$1@news.povray.org>
Fredrik Eriksson wrote:
> 
> No. Indeed, you must not, as it is not dynamically allocated.
> 
>> Is the memory used by that array freed after the program is terminated?
> 
> It is freed when 'i' goes out of scope. If 'i' is in namespace scope, it 
> goes out of scope when the program ends.

Thank you. My program should not have any memory leaks, then. I assume 
this also applies to other variables as well?

Sam


Post a reply to this message

From: Warp
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:49:47
Message: <4702cb0b@news.povray.org>
Samuel Benge <stb### [at] thishotmailcom> wrote:
> It's probably a stupid question, but here goes. I know that for every 
> "new", there must be a delete.

> So here's the question. If I create an array like so: int i[20];, and 
> then proceed to assign values for each entry, must I then delete it at 
> some point?

  Odd question, given that you answered it even before you asked it.

  If you deleted it, you would have more deletes than news, which would
break the "for each new, delete" rule.

> I didn't use a "new" flag to initialize the array. Is the 
> memory used by that array freed after the program is terminated?

  No. It's freed when it goes out of scope.

-- 
                                                          - Warp


Post a reply to this message

From: Vincent Le Chevalier
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:52:52
Message: <4702cbc4$1@news.povray.org>

> Fredrik Eriksson wrote:
>>
>> No. Indeed, you must not, as it is not dynamically allocated.
>>
>>> Is the memory used by that array freed after the program is terminated?
>>
>> It is freed when 'i' goes out of scope. If 'i' is in namespace scope, 
>> it goes out of scope when the program ends.
> 
> Thank you. My program should not have any memory leaks, then. I assume 
> this also applies to other variables as well?
> 

Everything that wasn't new-ed.

Normally the OS frees the memory used when the program ends anyway... So 
I don't understand exactly what you observe in your game. You mean it 
takes more memory when it runs for the 6th time than it took the first time?

-- 
Vincent


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: c++ memory question
Date: 2 Oct 2007 18:55:05
Message: <op.tzldx8fmcs6ysw@e6600>
On Wed, 03 Oct 2007 00:50:20 +0200, Samuel Benge  
<stb### [at] THIShotmailcom> wrote:
> Fredrik Eriksson wrote:
>>  No. Indeed, you must not, as it is not dynamically allocated.
>>
>>> Is the memory used by that array freed after the program is terminated?
>>  It is freed when 'i' goes out of scope. If 'i' is in namespace scope,  
>> it goes out of scope when the program ends.
>
> Thank you. My program should not have any memory leaks, then. I assume  
> this also applies to other variables as well?


Only objects that are dynamically allocated - i.e. with 'new' - need to be  
explicitly deallocated. You may not need to use 'delete' at all if you use  
standard library containers and/or smart pointers.



-- 
FE


Post a reply to this message

From: Samuel Benge
Subject: Re: c++ memory question
Date: 2 Oct 2007 19:13:06
Message: <4702d082@news.povray.org>
Vincent Le Chevalier wrote:
> Everything that wasn't new-ed.
> 
> Normally the OS frees the memory used when the program ends anyway... So 
> I don't understand exactly what you observe in your game. You mean it 
> takes more memory when it runs for the 6th time than it took the first 
> time?

I mean when I monitor my system's memory usage, more memory is being 
used AFTER I close my program. But after several runs, the memory is 
returned to an earlier state.

Sam


Post a reply to this message

From: Samuel Benge
Subject: Re: c++ memory question
Date: 2 Oct 2007 19:14:17
Message: <4702d0c9$1@news.povray.org>
Fredrik Eriksson wrote:
> Only objects that are dynamically allocated - i.e. with 'new' - need to 
> be explicitly deallocated. You may not need to use 'delete' at all if 
> you use standard library containers and/or smart pointers.

Okay, it's good to hear it from several people.

I use a DirectX game engine which requires a few "news", but I always 
delete them when I am through with 'em.

Sam


Post a reply to this message

Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.