473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Free Memory

29
Hi All,
I have a simple question. about free memery, I feel this is one of many reasons that make the memory crashing.
if we have the following array

Expand|Select|Wrap|Line Numbers
  1.  
  2. x=new float*[n];
  3. if(x=NULL)
  4. {
  5. exit(1);
  6. }
  7. for(int i=0;i<n;i++)
  8. {
  9.    x[i]=new float[4];
  10.   if(x[i]=NULL)
  11.    {
  12.       exit(1);
  13.     }
  14.  x[i][0]=0;
  15. x[i][1]=0;
  16. x[i][2]=0;
  17. x[i][3]=0;
  18. }
  19.  

My question is the following code will free all memory reserved? I think there is something missing
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<n;i++)
  2. {
  3.      delete x[i];
  4. }
  5. delete [] x;
  6.  
Thanks in advance
Dec 24 '07 #1
5 2044
Meetee
931 Expert Mod 512MB
Hi All,
I have a simple question. about free memery, I feel this is one of many reasons that make the memory crashing.
if we have the following array

Expand|Select|Wrap|Line Numbers
  1.  
  2. x=new float*[n];
  3. if(x=NULL)
  4. {
  5. exit(1);
  6. }
  7. for(int i=0;i<n;i++)
  8. {
  9.    x[i]=new float[4];
  10.   if(x[i]=NULL)
  11.    {
  12.       exit(1);
  13.     }
  14.  x[i][0]=0;
  15. x[i][1]=0;
  16. x[i][2]=0;
  17. x[i][3]=0;
  18. }
  19.  

My question is the following code will free all memory reserved? I think there is something missing
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<n;i++)
  2. {
  3.      delete x[i];
  4. }
  5. delete [] x;
  6.  
Thanks in advance
Hi,

delete x[i] is suffiecient to free memory. Don't use delete [] x;

Regards
Dec 24 '07 #2
I think you are assigning memory to a pointer which stores the memory space of array of pointers................isnt it so?
To delete it I think you have done it correct.
Dec 24 '07 #3
aeo3
29
What I am talking about, I reserved a space for the array x and another space for each x[i], but in x[i] there is a space for 4 floats, What I am worried about is that space for 4 floats, In my opinion I didnot free memory for this space.
Dec 24 '07 #4
Hi,

Basically here x is a 2Dimensional array. Memory of a 2d array can be freed completely by just freeing x []. There 'll be no memory loss.
Dec 24 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Memory of a 2d array can be freed completely by just freeing x []. There 'll be no memory loss.
Not so. A delete [] deletes the array. If the array elements are pointers, destructors are not called since pointers don't have destructors.

Expand|Select|Wrap|Line Numbers
  1. class Data
  2. {
  3. public:
  4.     ~Data() {cout << "dtor" << endl;}
  5. };
  6. int main()
  7. {
  8.    Data** array = new Data*[2];
  9.    array[0] = new Data;
  10.    array[1] = new Data;
  11.    delete [] array;    //Data destrcutors not called.
  12. }
  13.  
You have to delete each element and then delete the array.
Dec 24 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: RoSsIaCrIiLoIA | last post by:
why not to build a malloc_m() and a free_m() that *check* (if memory_debug=1) if 1) there are some errors in bounds of *all* allocated arrays from them (and trace-print the path of code that make...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
by: david wolf | last post by:
I have a problem that is used to test memory leak as follows, however, after each run of the program, if I am using the command "free" on the linux box, I did not see any difference about the...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
1
by: JOCOPVA | last post by:
i try to open up microsoft access 2003 and i get an error message it reads A system error occurred, or there isnt enough free memory to start. Close unneeded programs and tyry again. I have...
3
by: mahmoodn | last post by:
Hi, I have defined s struct and use it is this way: struct bpred_t * backup =p->core.thread.bpred; Then I want to free the memory like this: free( backup ); Is that true? However I get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.