-
February 19th, 2005, 06:04 PM
#1
Array deletion
What happens if you don't delete arrays that you've declared and defined? Is there any harm done to the memory of the computer?
Another problem is that when I declare arrays as class members, everything works fine. But when I try to delete the arrays in the destructor of the class, the program runs but also gives an assertion error!!! Why?
-
February 19th, 2005, 06:30 PM
#2
Re: Array deletion
You're only supposed to delete arrays that you allocated memory for using new or new[]. Deleting static arrays (arrays not allocated with new or new[]) is undefined behaviour.
Old Unix programmers never die, they just mv to /dev/null
-
February 19th, 2005, 06:51 PM
#3
Re: Array deletion
If U delete array (created with new operator !!!) u should do it that way:
Code:
int *pArray= new int [10];
...
delete [] pArray;
Best regards,
Krzemo.
-
February 19th, 2005, 07:13 PM
#4
Re: Array deletion
Oh! I didn't know that I only need to delete static arrays, thank you.
-
February 19th, 2005, 07:32 PM
#5
Re: Array deletion
Oh! I didn't know that I only need to delete static arrays, thank you.
It should be:
Oh! I didn't know that I only need to delete nonstatic arrays, thank you.
-
February 20th, 2005, 04:56 AM
#6
Re: Array deletion
See this FAQ about memory leaks.
Do not make the confussion between statically declared arrays
and static arrays:
Code:
static char array[10]; //static statically declared array
static char* parray = new char[..]; // static dinamically declared array
When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).
A variable declared static in a function retains its state between calls to that function.
When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.
-
February 20th, 2005, 05:26 PM
#7
Re: Array deletion
To cut it short... When you new you delete, when you new[] you delete[], and when you malloc you free. Other than that, don't worry about it!
Insert entertaining phrase here
-
February 21st, 2005, 01:17 AM
#8
Re: Array deletion
A vector (from the C++ standard library) is often better than an ordinary array. It has automatic memory management.
-
February 21st, 2005, 08:24 AM
#9
Re: Array deletion
...and in any case, no harm will ever occur to the memory of the computer, and in general even sloppy programs will not hold onto memory after the process completes... NOT advocating sloppy programming
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 21st, 2005, 02:33 PM
#10
Re: Array deletion
Originally Posted by TheCPUWizard
...and in any case, no harm will ever occur to the memory of the computer, and in general even sloppy programs will not hold onto memory after the process completes... NOT advocating sloppy programming
unless you could use up all the free mem... but that would have to be one big program...
-
February 21st, 2005, 02:52 PM
#11
Re: Array deletion
Actually a run-away can easily exhaust the entire 2GB address space for the process. Depending on virtual memory limits, this may have a fatal impact on the execution of other processes, but still no harm will come to the memory
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|