problem deallocating memory for 3d aray


 
Thread Tools Search this Thread
Top Forums Programming problem deallocating memory for 3d aray
# 1  
Old 09-06-2002
problem deallocating memory for 3d aray

Can anyone tell me why this section of code causes a segmentation fault when 'deallocate' is called?. Although it works when n>=number.
This code segment is intended to be used in a program in which the tensor is very large, (5x512x512) or greater.

i tryed to compile this using gcc version 2.95.4


#include <stdio.h>
#include <malloc.h>

int number = 8;
double ***tensor;

void allocate()
{
int i,j;
int n = 3;
tensor = (double***)malloc(sizeof(double**)*n);
for(j=0;j<number;j++)
{
tensor[j] = (double**)malloc(sizeof(double*)*number);
for(i=0;i<number;i++)
tensor[j][i] = (double*)malloc(sizeof(double)*number);
}
}

void deallocate()
{
int i,j;
for(i=0;i<number;i++)
{
for(j=0;j<number;j++)
free(tensor[i][j]);
free(tensor[i]);
}
free(tensor);
}

int main(void)
{
allocate();
deallocate();
return 0;
}
# 2  
Old 09-06-2002
# 3  
Old 09-30-2002
Re: problem deallocating memory for 3d aray

I think I have got the answer.

The deallocate function is right. There is a problem in the allocate function.

When you allocate memory to 'tensor', you use:
tensor = (double***)malloc(sizeof(double**)*n);
that is to say, you just can access tensor through 0 to n. If you want to access tensor[i] which i > n , then a problem will arise.
That's the reason why it works when number < n and doesn't work when number > n.

By the way, I think there is a simple way to allocate a block of memory to the big array such as 'tensor'.
allocate:
tensor = (double***)malloc(sizeof(double)*number*number*n);
deallocate:
free(tensor);

Hope it be helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Deallocating memory in multi-threaded environment.

I'm having a hard time figuring out how to manage deallocation of memory in multithreaded environments. Specifically what I'm having a hard time with is using a lock to protect a structure, but when it's time to free the structure, you have to unlock the lock to destroy the lock itself. Which will... (5 Replies)
Discussion started by: gngrwzrd
5 Replies

2. Programming

fortran: segmentation fault when deallocating

Hi, I'm looking for a quite "interesting" bug at the moment - any idea could help! (o; Language is fortran 90, compiler gfortran (sry, dont find which version it is). As I suppose you dont want to read like 10k lines of code here, I won't post the entire thing ^^ The problem is that I get... (3 Replies)
Discussion started by: Giogio
3 Replies

3. Red Hat

1Gb+ memory problem

Hi, I have a Linux distribution ( Oralce Enterprise Linux 5.3 i.e. Redhat ) that I have installed. It works fine when I used 2*512Mb dimms or replace them with a single 1Gb dimm. However when I try to go above 1 Gb the bootup and general performance deteriorates badly. The BIOS picks up the memory... (3 Replies)
Discussion started by: jimthompson
3 Replies

4. Programming

memory leak problem

hi all Can any one plz explain me about memory leak problem Thankx (5 Replies)
Discussion started by: sonali
5 Replies

5. Solaris

out of memory problem

i am building open source software on a solaris 8 machine for couple of days i am getting error messages like out of memory or no more processes and then build is failing i tried with rebooting the machine but then after couple of days same problem is arising again. can any one... (1 Reply)
Discussion started by: mobydick
1 Replies

6. Programming

how to round up a memory address(memory alignment problem)

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10. My code to marshal... (6 Replies)
Discussion started by: nj302
6 Replies

7. Programming

memory stack problem

Hi, I am writing a C program under SCO Unix. I have a memory stack problem but do not know how to go about fixing it. I have tried running INSURE but that does not detect any problems. Essentially the problem is that the memory address shifts on return from a routine. I pass a pointer to... (3 Replies)
Discussion started by: jkeagy
3 Replies

8. Filesystems, Disks and Memory

memory Problem

Hi , One of java programs in our machine allocates 256M of heap memory and max of 512 memory(Tomcat to be specific). Our ulimit per process is 32M.How can a process allocates such a big amount of memory ps v output is PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND 24 220776 220892... (0 Replies)
Discussion started by: ganti
0 Replies

9. Programming

memory problem

Hi , One of java programs in our machine allocates 256M of heap memory and max of 512 memory(Tomcat to be specific). Our ulimit per process is 32M.How can a process allocates such a big amount of memory ps v output is PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND ... (0 Replies)
Discussion started by: ganti
0 Replies

10. Programming

Memory problem

Hi: I have a server with a very little memory leak (29 bytes) as reported by Purify. Itīs a pooled server, so each process lives forever accepting conections.But top or ps commands show that used memory it is always increasing much more than 29 bytes (30K mor or less) in each conection. How... (0 Replies)
Discussion started by: Pablo J. Royo
0 Replies
Login or Register to Ask a Question