LinuxQuestions.org
Help answer threads with 0 replies.
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 04-18-2008, 04:54 AM   #1
jbd
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
Memory problem detected by Valgrind for large array - C programming


Hi

I'm writing a program that needs to deal with large multidimensional arrays.

My program works fine for matrices of size 125 x 125 and no errors occur when running with Valgrind.
For the next size up which is matrix size 343 x 343 I get a segmentation fault and Valgrind gives me "Invalid read".

The program runs into trouble when it calls the following Numerical recipes routine choldc.c to do the Cholesky decomposition of matrix a and then inverts it to give matrix a_inv:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "globals.h"
#include "structs_and_vars.h"

void choldc(int n, double **a, double **a_inv)
{
#include "header.h"

int i,j,k,g,g_dash; /* Dummy variables */
double sum; /* Temp variable */
vector p;

for (i=1;i<=n;i++) {
for (j=i;j<=n;j++) {
LINE WHERE ERROR OCCURS for (sum=a[i][j],k=i-1;k>=1;k--) sum -= a[i][k]*a[j][k];
if (i == j) {
if (sum <= 0.0)
printf("error: choldc failed\n");
p[i]=sqrt(sum);
} else a[j][i]= sum/p[i];
}
}

/* find inverse */
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
a_inv[i][j] = a[i][j];
}


for (i=1;i<=n;i++)
{
a_inv[i][i]=1.0/p[i];

for (j=i+1; j<=n; j++)
{
sum=0.0;
for (k=i;k<j;k++) sum -= a_inv[j][k]*a_inv[k][i];
a_inv[j][i]=sum/p[j];
}
}

for (i=1;i<=n-1;i++)
{
for (j=i+1;j<=n;j++)
{
a[i][j]=0.;
a_inv[i][j]=0.;
}
}

}

The valgind erro message is:

==22212== Invalid read of size 4
==22212== at 0x804B705: choldc (choldc.c:18)
==22212== by 0x8049818: main (main.c:332)
==22212== Address 0x1B228C80 is not stack'd, malloc'd or (recently) free'd
==22212==
==22212== Process terminating with default action of signal 11 (SIGSEGV)
==22212== Access not within mapped region at address 0x1B228C80
==22212== at 0x804B705: choldc (choldc.c:18)
==22212== by 0x8049818: main (main.c:332)
==22212==
==22212== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
==22212== malloc/free: in use at exit: 9449752 bytes in 26 blocks.
==22212== malloc/free: 26 allocs, 0 frees, 9449752 bytes allocated.
==22212== For counts of detected errors, rerun with: -v
==22212== searching for pointers to 26 not-freed blocks.
==22212== checked 37667096 bytes.

I have noted line 18 where the error occurs in the code.
I don't understand what it is about this line that can't deal with the increased size. Other bits of my program can cope.

Any help would be greatly appreciated!!

Last edited by jbd; 04-18-2008 at 05:11 AM. Reason: spelling error
 
Old 04-18-2008, 07:13 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
When you are allocating the memory for the array (presumably in main.c), did it work?
 
Old 04-18-2008, 08:52 AM   #3
jbd
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Original Poster
Rep: Reputation: 0
There is no problem when the memory is allocated (in main) and when I take out this particular function and let it run through another function, again there is no problem.
 
Old 04-18-2008, 10:44 AM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Next time, use code blocks, the formating is terrible.

What is a vector p?

How is n meaningful? The checks have "x<=n", meaning if the array is allocated to size n, x will be accessing it as n+1 at the last iteration.

Why did you use multi-dimensional arrays? Just curious because I wouldn't have thought the compiler would treat them correctly without knowing how big they where.

Also, use a normal debugger to check on those temporary variables, there is a chance they are being set to invalid numbers then used to address a array, meaning the memory lookup would be invalid and segfault. (It would have to be off by more then 1 for valgrind to give that error message).
 
  


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
Valgrind reports memory leak at opendir Rayven Programming 11 11-02-2006 02:03 PM
Problem Accessing Large RAID Array FlyingPenguin128 Debian 1 05-16-2005 10:51 PM
Valgrind and Shared Memory nodger Programming 0 04-27-2005 07:29 AM
valgrind reports un-free'd memory in crypt()? Xagafinelle Programming 3 08-21-2004 11:02 AM
Large Memory Problem Ravenmoonheart Linux - Newbie 7 07-09-2003 02:06 PM

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

All times are GMT -5. The time now is 03:40 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