LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2005, 07:22 AM   #1
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Rep: Reputation: 45
memory managment problem; higher dimensional array doesn't delete


Hello

could someone tell me whats wrong with the following code;

Code:
int main()
{

  int i,j;
  int sizeXY=100;
  int sizeZ=50;
  float ***Image;

  Image = new float **[sizeXY];

  for(i=0;i<=sizeXY;i++)
  {
          Image[i] = new float *[sizeXY];
          for(j=0;j<=sizeXY;j++)
          {
			Image[i][j] = new float[sizeZ];
           }
  }

  delete Image;

}
I set the export MALLOC_CHECK_ to '1';

Code:
export MALLOC_CHECK_=1
But ruunig this code gives me;

Code:
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$ ./prog.exe
malloc: using debugging hooks
*** glibc detected *** free(): invalid pointer: 0x0804c050 ***
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$
Whats going on??? Apparently, something is going wrong when freeing the used memory.
 
Old 06-15-2005, 07:27 AM   #2
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Btw; I tried doing
Code:
delete [] Image;
But it gives me the same thing.

Doing something like
Code:
delete [][] Image;
doesn't even compile
 
Old 06-15-2005, 07:39 AM   #3
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Okey, i'v made some progress. I have changed the code to;

Code:
int main()
{

  int i,j;
  int sizeXY=100;
  int sizeZ=50;
  float ***Image;

  Image = new float **[sizeXY];

  for(i=0;i<=sizeXY-1;i++)
  {
          Image[i] = new float *[sizeXY];
          for(j=0;j<=sizeXY-1;j++)
          {
			Image[i][j] = new float[sizeZ];
           }
  }

  delete Image;

}
But then I want to check for memory leaks;

Code:
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$ free
malloc: using debugging hooks
             total       used       free     shared    buffers     cached
Mem:        385464     299264      86200          0      23708     173696
-/+ buffers/cache:     101860     283604
Swap:       497972          0     497972
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$ make test
malloc: using debugging hooks
c++ main.cpp -o prog.exe -lgsl -lgslcblas -lm -O2
malloc: using debugging hooks
malloc: using debugging hooks
malloc: using debugging hooks
malloc: using debugging hooks
malloc: using debugging hooks
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$ ./prog.exe
malloc: using debugging hooks
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$ free
malloc: using debugging hooks
             total       used       free     shared    buffers     cached
Mem:        385464     299480      85984          0      23880     173660
-/+ buffers/cache:     101940     283524
Swap:       497972          0     497972
sohail@thabox:~/docs/tustuff/BEP/cpp/threeD$
as you can see, I have a memory leak (llok at the free memory before and after running the program.)

But the program runs without error :-)
 
Old 06-15-2005, 08:49 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
You need to delete[] everything allocated with new[].

(pseudo-C++)
Code:
for(i) {
    for(j) delete [] array[i][j];
    delete [] array[i];
}
delete [] array;
Quote:
as you can see, I have a memory leak (llok at the free memory before and after running the program.)
I think the kernel would see that your process has finished and collect all its virtual memory--but you may have been faster than it.

Also, memory usage generally fluctuates on a desktop machine.

But I think you're right you're having a leak--when you []-allocate something, the allocator has to store the size (in order to call the correct number of destructors), and the size doesn't get freed when you delete (as opposed to delete []).

hth --Jonas
 
Old 06-15-2005, 10:44 AM   #5
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Thanks dude!
 
Old 06-15-2005, 04:18 PM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
Thanks dude!
You're welcome.

--Jonas
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
building a three dimensional array in vb6 mrobertson Programming 0 08-18-2005 09:12 AM
Bash 2 dimensional array ldp Linux - General 5 07-29-2005 11:29 AM
3 dimensional array help leeach Programming 11 06-15-2005 10:46 AM
Memory Managment Made Easy Newbie85 Linux - Newbie 1 02-28-2005 12:51 PM
memory managment restless Linux - Software 2 03-12-2004 05:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration