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