delete operator - multidimensional array

Hi to everybody,
I'm writing a c++ based program on Ubuntu OS using code::blocks IDE and g++ compiler.
I'm a beginner with this language so this may be a dumb question, but I've searched both the forum and the web but nothing seems to fix my problem.

I've got some issues using delete operator while trying to de-allocate memory.
This is the part of code that doesn't seem to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
try{AIC=new float*[Ne];}
    catch (bad_alloc xa)
{cout << "Error! Allocation ---> FAULT\n"; cin.get(); }
    
for (i=0; i<Ne; i++)
    {
        try{AIC[i]=new float[Ne];}
        catch (bad_alloc xa){cout << " Error! Allocation ---> FAULT\n";cin.get();break;}
    }

//Here's the code that works on AIC which I haven't report

for (i=0; i<Ne; i++)
    {
        delete [] AIC[i];
    }
    delete [] AIC;


the code is compiled without any problem and the program runs like everything is working perfectly.
But I've noticed, using Linux system monitor utility, that the heap isn't actually released when delete is invoked., and this memory leak obviously may cause a crash in the following part of the code, when I try to store other variables.
So the question is: am I using delete in an appropriate way or it may be something else?

Moreover I get trouble when the heap is filled, I thought that a virtual memory would be automatically created when the heap is full but this don't happen and I get an inevitable crash.

thank you all
Giulio
Last edited on
As a beginner to the language, you should get comfortable using vectors instead of this.

Also, the tool to do memory profiling in Linux is called valgrind. Just run valgrind your_progrsm, and it will report all memory leaks and other related errors, and what lines of code caused them. What you're seeing in a system monitor (which system monitor?) isn't directly relevant.
ok thanks, I'm running valgrind to find memory leaks.
But my problem actually isn't to find them.

I already know that those code lines I quoted cause the leak. My problem is that the allocated memory for AIC isn't actually freed.
I mean: when I try to go further with my code, for example, if I try to store another variable with the same size of AIC, I get a segmentation fault error that means I've reached the heap memory limit.
This should not happen if the deallocation had finished successfully, should it?

I could see the memory reaching its limit by using "system monitor", which is one of ubuntu's utility that shows the RAM usage in real time. I know that this is a rude method but It points out the problem.


As a beginner to the language, you should get comfortable using vectors instead of this.


My opinion is the opposite.

The code you posted shouldn't be causing a memory leak, perhaps you never actually make the delete loop happen.

I mean: when I try to go further with my code, for example, if I try to store another variable with the same size of AIC, I get a segmentation fault error that means I've reached the heap memory limit.
This should not happen if the deallocation had finished successfully, should it?


Once you de-allocate, you can't use that memory.

I'm running valgrind to find memory leaks. But my problem actually isn't to find them. I already know that those code lines I quoted cause the leak

The code you posted has no leaks (but has big problems if any allocation fails, since you aren't handling exceptions correctly)

given
1
2
3
    int Ne = 10;
    int i;
    float** AIC;

valgrind says:
==9648== HEAP SUMMARY:
==9648==     in use at exit: 0 bytes in 0 blocks
==9648==   total heap usage: 11 allocs, 11 frees, 480 bytes allocated
==9648== 
==9648== All heap blocks were freed -- no leaks are possible


If you want help with a program that leaks or segfaults, you should post a compilable, complete, program that exhibits those problems.
Last edited on
@cubbi
Sorry I can't post the entire code because till now it's a couple of thousand lines long :)...it would be useless and I do not want bother anyone.
I just wanted to be sure that I was doing everything in the right way, probably I have to search for another cause to my memory filling. Thank you anyway, working with valgrind had simplified my work and I could fix some other problems.

@LowestOne
why are you saying that de-allocating mamory cannot be utilized? I thought that the aim of freeing pre-allocated memory was to make some blocked resources available again...am I missing some basics
De-allocating the memory does make it available again, available to the operating system. Your program has given the memory up.

If you need the array again then you need to make new again, but this will probably have your array being in a different place in memory. If you need/want the memory in the same place, simply don't delete the memory until the program/use is done.

I believe Cubbi means you to post something like this:
1
2
3
4
5
6
7
8
int main(void)
{
	int* data = new int;
	int count = 0;
	while (true)
		*(data + count++) = 0;
	return 0;
}


Which, for the one time I ran it, produced a seg fault when count equaled 2312
Last edited on
In my case I need to free the memory allocated for AIC, because in the future I will need to store very huge variables, I'm talking about square matrix of order more than 100.000, maybe even some million.
(CFD simulation =P)
AIC now is something up to 10.000, but only because I'm still at a test phase..AIC itself is intended to reach an order of hundred of thousand, so I really need every piece of memory.

I think I'm misunderstanding your last post, are you stating that deleting a variable will make the memory available for the OS but not for my program? so the consequence is that sooner or later I will fill the heap?

I don't care of having the new array in a different address of memory, the point is to have as much available memory as I can. Of course I will use new operator again when I will declare new variables. But as far as I know using delete should give back to my program the deallocated memory.

Thanks for your help
Giulio
I'm talking about square matrix

if that program is supposed to do something meaningful with a matrix, use a matrix library (Eigen, boost.ublas, etc) or at *least* use vectors. If you want this to be an educational experience, write your own matrix class (that uses a vector or valarray as storage). Don't use new to allocate arrays.
Last edited on
Topic archived. No new replies allowed.