Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-01-2011, 02:12 AM   #1
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 235

Rep: Reputation: 15
Question C: Segmentation fault when freeing allocated 2D array

Dear all,

I know that seems to be a common question, but information from the web does not help me.
So problem is in the title.

Here is what I do:

First I declare the pointers.

double **rijx,**rijy,**rijz;
Then I allocate the require memory.

  rijx = malloc (atom_number*sizeof(double*));
  rijy = malloc (atom_number*sizeof(double*));
  rijz = malloc (atom_number*sizeof(double*));
  if ( (rijx == NULL) || (rijy == NULL) || (rijz == NULL) ){ (void)fprintf(stdout,"compute_energy_cuda_: failed to allocate rijx or rijy or rijz\n");exit (EXIT_FAILURE);}			   
  for (i=0;i<=atom_number-1;i++) {
        rijx[i] = malloc (atom_number*sizeof(double));
        rijy[i] = malloc (atom_number*sizeof(double));       
        rijz[i] = malloc (atom_number*sizeof(double));
	if ( (rijx[i] == NULL) || (rijy[i] == NULL) || (rijz[i] == NULL) ){ (void)fprintf(stdout,"compute_energy_cuda_: failed to allocate rijx[%d] or rijy[%d] or rijz[%d]\n",i,i,i);exit (EXIT_FAILURE);}
Then I initialize the arrays ....

   for (i=0;i<=atom_number-1;i++) {
    for (j=0;j<=atom_number-1;j++) {
      rijx[i][j] = 0.0;
      rijy[i][j] = 0.0;
      rijz[i][j] = 0.0;
I do some stuff with them ...

Then I free the memory by doing

  for (i=0;i<=atom_number-1;i++) {
and in crash during this step.
I do not see what's going wrong here.
Then I do this

But it does not get to here so far.

So, some hints are more than welcome.
Thanks in advance,

Old 06-01-2011, 03:41 AM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Originally Posted by aihaike View Post
Dear all,

I know that seems to be a common question...
Yes, it is indeed a common question, and one I just answered a couple of days ago. Please click here for an example.

P.S. I did not see anything wrong in the code you posted. Personally I am not fond of seeing if-statement condition such as i <= (N-1), preferring instead i < N .... but that is beside the point. Your code is probably crashing elsewhere.

Last edited by dwhitney67; 06-01-2011 at 03:45 AM.
Old 06-01-2011, 11:12 PM   #3
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 235

Original Poster
Rep: Reputation: 15
Thanks to your post, I looked for elsewhere and solve the issue.
I initialized an array with an index bigger that its size.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Fortran Code with Large arrays statically allocated -> segmentation fault eiffel Linux - Kernel 2 07-19-2006 10:14 AM
Segmentation fault with char* array simopal6 Programming 8 06-16-2006 01:05 PM
how big can be an array to give segmentation fault!!! mlaich Programming 7 01-21-2006 07:58 AM
Segmentation fault after declaring a large array. oulevon Programming 6 11-08-2005 02:41 AM
segmentation fault when array size exceed 1GB ymei Programming 14 11-11-2003 10:27 AM > Forums > Non-*NIX Forums > Programming

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

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