LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-01-2011, 03:12 AM   #1
aihaike
Member
 
Registered: Feb 2006
Location: Shanghai, China
Distribution: Slackware
Posts: 210

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.

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

Code:
  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 ....

Code:
   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

Code:
  for (i=0;i<=atom_number-1;i++) {
        (void)free(rijx[i]);
        (void)free(rijy[i]);	
        (void)free(rijz[i]);
                                 }
and in crash during this step.
I do not see what's going wrong here.
Then I do this

Code:
(void)free(rijx);(void)free(rijy);(void)free(rijz);
But it does not get to here so far.

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

Éric.
 
Old 06-01-2011, 04:41 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
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 04:45 AM.
 
Old 06-02-2011, 12:12 AM   #3
aihaike
Member
 
Registered: Feb 2006
Location: Shanghai, China
Distribution: Slackware
Posts: 210

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.
 
  


Reply


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 11:14 AM
Segmentation fault with char* array simopal6 Programming 8 06-16-2006 02:05 PM
how big can be an array to give segmentation fault!!! mlaich Programming 7 01-21-2006 08:58 AM
Segmentation fault after declaring a large array. oulevon Programming 6 11-08-2005 03:41 AM
segmentation fault when array size exceed 1GB ymei Programming 14 11-11-2003 11:27 AM


All times are GMT -5. The time now is 12:59 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration