POV-Ray : Newsgroups : povray.off-topic : c++ memory question Server Time
11 Oct 2024 17:47:13 EDT (-0400)
  c++ memory question (Message 3 to 12 of 22)  
<<< Previous 2 Messages Goto Latest 10 Messages Next 10 Messages >>>
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

From: Samuel Benge
Subject: Re: c++ memory question
Date: 2 Oct 2007 19:17:08
Message: <4702d174$1@news.povray.org>
Warp wrote:
>   Odd question, given that you answered it even before you asked it.

I knew I'd get this response from someone. I was just worried about the 
non-new declaration of arrays and variables, but I am clear on those now.

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

Indeed. This happened with my maze game, and I got a warning about it.

>> I didn't use a "new" flag to initialize the [static] array. Is the 
>> memory used by that array freed after the program is terminated?
> 
>   No. It's freed when it goes out of scope.

Cool. Thanks!

Sam


Post a reply to this message

From: Vincent Le Chevalier
Subject: Re: c++ memory question
Date: 2 Oct 2007 19:20:49
Message: <4702d251$1@news.povray.org>

> 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.
> 

I don't think the problem is in your program, then. When it does not run 
anymore, it does not use memory anymore, at least on modern OSes... 
Perhaps you could try to find a way to monitor the memory per process, 
rather than the whole.

-- 
Vincent


Post a reply to this message

<<< Previous 2 Messages Goto Latest 10 Messages Next 10 Messages >>>

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