3 Replies - 1399 Views - Last Post: 08 November 2005 - 11:24 AM Rate Topic: -----

#1 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Memory Management Issues

Posted 08 November 2005 - 10:45 AM

Hi

I have finally finished my project. The thing is, it consists of a number of statistical tests(15). To test them, I tested each one separately against input and output that I know is correct. The problem is, if I run them separately, they work perfectly. However, when I run them all (one after the other), I get a random segmentation error when I try to free an array! I can't really show you any code, because I found that if I simply exclude the test that gives the problem, the next one gives that problem. So obvioustly this is a memory management issue, right? The weird thing is, I actually always free pointers that I allocate using malloc and calloc... So how is it possible that I am running out of memory. I also do checks for whether an assigned pointer is NULL, (meaning there is not enough memory to allocate the structure) but the program never triggers those loops... Can anyone give my any hints as to where I should start looking?

This is a really big program (I think around 5000 lines of code), but I am certain I have not been wasteful in my memory management...

Any help anyone could give me would be greatly appreciated.
Thx

Is This A Good Question/Topic? 0
  • +

Replies To: Memory Management Issues

#2 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Memory Management Issues

Posted 08 November 2005 - 10:47 AM

Have you run the program through a debugger to trace where the segfault happens and what data it segfaults on?
Was This Post Helpful? 0
  • +
  • -

#3 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Memory Management Issues

Posted 08 November 2005 - 11:20 AM

Er, no. I have never used a debugger for C. But like I said, I traced the segmentation error manually, and it seems to happen in places where it is not really possible. That's why I thought it was a memory issue. For example, one of the places where it would error would look like this:

if (Tree == NULL) printf("Tree is NULL\n");
printf("Just before free\n");
free(Tree);
printf("Just after free\n");



Then of course it prints out the "Just before free" but not the "Just after free". Are there times when you shouldn't free a variable (except of course if you are going to use it later on). If I take out that free, the next procedure throws an segmentation error.

Hang on, if I take out the entire procedure, the whole project works... So maybe my error is within this one procedure. BUT if I run it on its' own (or even with a few other procedures) it works fine... Here's the code if someone can maybe spot the error...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "StatisticalFunctions.h"
#include "HypothesisTesting.h"


/* The focus of this test is the number of bits between matching patterns (a measure
   that is related to the length of a compressed sequence).  The purpose of the test
   is to detect whether or not the sequence can be significantly compressed without
   loss of information.  A significantly compressible sequence is considered to be
   non-random.  
   
    n = The number of bits in the sequence
	L = The length of each block
	Q = The number of blocks used in the initialization sequence */

Result UniversalTest(int L, int Q, long n) {

	int i, j, treeSize, index;
	long oldValue, difference;
	double c, sigma, sqrt2, sum, phi, arg, P;
	long* Tree;
	int K = (int)(floor(n/L) - (double)Q);
	double expected_value[17] = {0,0,0,0,0,0,5.2177052,6.1962507,7.1836656,
 	 8.1764248, 9.1723243, 10.170032, 11.168765, 12.168070,
 	 13.167693, 14.167488, 15.167379};
	double variance[17] = {0,0,0,0,0,0,2.954,3.125,3.238,3.311,3.356,3.384,
 	 3.401,3.410,3.416,3.419,3.421};
	Result r = newResult(n);
	boolean block[L];

	// Compute the expected value according to Marsaglia's Paper
	c = 0.7 - 0.8/(double)L + (4 + 32/(double)L)*pow(K,-3/(double)L)/15;
	sigma = c * sqrt(variance[L]/(double)K);
	sqrt2 = sqrt(2);

	// Initialize the table, represented as a binary tree
	treeSize = (int)pow(2,L);
	Tree = (long*)calloc(treeSize, sizeof(long));

	if (Tree == NULL) {
  addError(r,"Not enough memory to perform test.");
  printf("Not enough memory!\n");
  r->success = FALSE;
  return r;
	}

	for (i = 1; i < treeSize - 1; i++)
  Tree[i] = 0;
	
	// Use the initialization blocks
	for (i = 1; i <= Q; i++) {
  index = 1;
  for (j = 0; j < L; j++) {
 	 if (getNextBoolean() == 0) 
    index *= 2;
 	 else 
    index = 2*index+1;
  }
  Tree[index-1] = i;
	}
	
	// Process the rest of the blocks
	sum = 0.0;
	for (i = Q+1; i <= Q+K; i++) {
  index = 1;
  for (j = 0; j < L; j++) {
 	 if (getNextBoolean() == 0)
    index *= 2;
 	 else
    index = 2*index+1;
  }
  difference = i - Tree[index-1];
  Tree[index-1] = i;
  sum += log(difference)/log(2);
	}
	free(Tree);

	// Calculate the test statistic and p-value
	phi = (double)(sum/(double)K);
	arg = fabs(phi-expected_value[L])/(sqrt2 * sigma);
	P = erfc(arg);

	r->testStatistic = phi;
	r->p_value = P;

	if ((P < 0) || (P > 1)) addError(r,"Error:  P-value is out of range.");

	if (P < ALPHA) {
  r->success = FALSE;  // The sequence is non-random
	} else {
  r->success = TRUE;  // The sequence is random
	}

	return r;
	
};



I'm so confused... :crazy:
Oh and the error is always when I try to free my Tree.
Was This Post Helpful? 0
  • +
  • -

#4 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Memory Management Issues

Posted 08 November 2005 - 11:24 AM

Ok I changed the Tree from long to int, and now it works....

But I actually want to keep is a long, any ideas why this should cause an error?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1