Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-14-2004, 02:19 PM   #1 (permalink)
m8j
Registered User
 
Join Date: Nov 2004
Posts: 1
m8j is on a distinguished road
Post Problem with Memory (EXC_BAD_ACCESS)

i'm using gcc for a group theory project, and i feel quite bad now because my program always crashes with bigger data boundarys (maxn), and i have no idea why since about 2 days .

my data is a simple array [100] of permutations (a simple class which itself contains an array of size maxn=1000 of integers)
i'm using new [] to allocate the memory, and i delete[] it at the end of the program.

the software works great for maxn=1000, but it crashes at positions that do not make any sense if i set maxn=1500 or something bigger .

SIGSEGV, or gdb says EXC_BAD_ACCESS


what am i doing wrong with my memory ? i mean it should be possible to have 100*1500 int's if i allocate the memory ? i would really need 5000*5000 int's in future...



to test the programm paste the following input (whitespace does not matter) after start (it is a simple command line program):
100
2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99



the code is the following (i deleted everything not needed to make it as simple as possible to reproduce the error)

Code:
#include <iostream.h>
using namespace std;

const int maxn = 1500;		// Maximum size of omega = {1, ,n}

class Permutation {			// interface for permutations
	public:
		int p[maxn];											// the images of the points 0..maxn-1
		int n;													// size of omega = {1, ,n}
		Permutation () { n=maxn; };								// constructors
		Permutation (int m, char c) { n=m; switch(c) {
			case 'i': for (int i=0;i<n;i++) p[i] = i; break; // identity
			default:  for (int i=0;i<n;i++) p[i] =-1; break; // undefined
		} };
		void operator *= (Permutation param) {					// direct multiplication
			for (int i=0;i<n;i++) p[i] = param.p[ p[i] ];
		};							
		Permutation inverse() const {							// inverse
			Permutation result (n,'i');
			for (int i=0;i<n;i++) result.p[ p[i] ] = i;
			return (result);
		};	
		bool isdefined() const { return (p[0] > -1); };			// if it is defined
		void input()        { for (int i=0;i<n;i++) { cin >> p[i]; p[i]--; } };				// input
		void output() const { for (int i=0;i<n;i++) cout << p[i]+1 << " "; cout << endl; }; // output
		void setn(int m) { n=m; };
};

/****** globals ******/
int n;									// size of omega = {1, ,n}
int r;									// number of generators
Permutation* g;							// the generators
int cosreps = 0;						// number of    (= size of orbit of alpha)
Permutation* cosrep;					// coset representatives (to store output of SchreierTree)
Permutation gen (maxn,'i');		// group element moving original alpha to actual alpha

/****** ScheierTree ******/
void ScheierTree(int alpha) {			// depth first search to determine the orbit of alpha
	static int ag;				// image of actual alpha under generator g[j]
	cosrep[alpha] = gen; cosreps++;
	for (int j=0;j<r;j++) { ag = g[j].p[alpha];
		if (!cosrep[ag].isdefined()) {
			gen *= g[j]; ScheierTree(ag); gen *= g[j].inverse();
		}
	}
}

/****** main ******/
int main () {
	
	cout << "n, num generators, generators ? "; cin >> n;
	
			g = new Permutation[n] (n,'u');
			cosrep = new Permutation[n] (n,'u');
			gen.n = n;
				
	cin >> r;
	for (int j=0;j<r;j++) g[j].input();	
										// get the coset representatives for G(alpha)
	ScheierTree(0);
		
			cout << "Wow, it worked";
			//for (int i=0;i<n;i++) cosrep[i].output();
	
			delete[] g; delete[] cosrep;
	return 0;
}
m8j is offline   Reply With Quote
Old 11-18-2004, 04:50 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Sorry for late reply. Been busy.

Arrays of objects *must* be parameter-less.

The code below is not allowed:
Code:
g = new Permutation[n] (n,'u');
cosrep = new Permutation[n] (n,'u');
Do the this instead:
Code:
p=new Permutation[n];
p[0].set(n, 'u');
p[1].set(n, 'u');
//Etc. Make loop thus.
The fact that the faulty code works sometimes is bad luck. This is because the behaviour is undefined.

Don't forget to make a parameterless constructor in your class:
Code:
class Permutation
{
public:
   Permutation() {}
};
And obviously implement the set() method as well.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
omfg... well, i may need help just understanding this problem... .pakmon. Standard C, C++ 3 01-08-2004 09:44 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 07:17 AM


All times are GMT -8. The time now is 07:38 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting