Where is the bugs?!

This is a discussion on Where is the bugs?! within the C Programming forums, part of the General Programming Boards category; The compiler(GCC) hasn't given the bugs. And I can't find the problem. Code: #include <stdio.h> #include <stdlib.h> #define min(a,b) ((a)<(b)?(a):(b)) ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    Where is the bugs?!

    The compiler(GCC) hasn't given the bugs. And I can't find the problem.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define min(a,b) ((a)<(b)?(a):(b))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define libSize 20
    #define libPointSize 20
    #define libTimeSize 30
    
    int n, k;
    int lib[libSize][libSize];
    int markLib[libPointSize][libPointSize][libPointSize][libPointSize][libTimeSize];
    
    void constructor()
    {
    	FILE *fr;
    	int a, b, c, d, e;
    	fr = fopen("library.dat", "+r");
    	fscanf(fr, "%d %d", &n, &k);
    	printf("haha");
    	for (a = 0; a < libSize; a++)
    		for (b = 0; b < libSize; b++)
    			lib[a][b] = 0;
    	for (a = 0; a < libPointSize; a++)
    		for (b = 0; b < libPointSize; b++)
    			for (c = 0; c < libPointSize; c++)
    				for (d = 0; d < libPointSize; d++)
    					for (e = 0; e < libTimeSize; e++)
    						markLib[a][b][c][d][e] = 10000;
    
    	for (a = 0; a < n; a++)
    		for (b = 0; b < n; b++)
    			fscanf(fr, "%d", &lib[a][b]);
    
    	fclose(fr);
    }
    
    int areaValue(int p1X, int p1Y, int p2X, int p2Y)
    {
    	int i, j, totalValue;
    	totalValue = 0;
    	for (i = p1X; i < p2X; i++)
    		for (j = p1Y; j < p2Y; j++)
    			totalValue += (int)lib[i][j];
    	return totalValue;
    }
    
    /*	data for minCutting function	*/
    int a, b, time;
    /*	spX = start point X; epY = end point Y	*/
    int minValue(int spX, int spY, int epX, int epY, int freq)
    {
    	if ((markLib[spX][spY][epX][epY][freq] > 0) || 
    		(markLib[spX][spY][epX][epY][freq] <= 200000000))
    		return markLib[spX][spY][epX][epY][freq];
    	else {
    		if (freq == 0)
    			markLib[spX][spY][epX][epY][freq] = areaValue(spX, spY, epX, epY);
    		else {
    			for (a = spX+1; a < epX; a++)
    				for (time = 0; time < freq; time++)
    					markLib[spX][spY][epX][epY][freq] = 
    						min(markLib[spX][spY][epX][epY][freq],
    						max(minValue(a, spY, epX, epY, freq-time-1),
    							minValue(spX, spY, a, epY, time)));
    
    			for (a = spY+1; a < epY; a++)
    				for (time = 0; time < freq; time++)
    					markLib[spX][spY][epX][epY][freq] = 
    						min(markLib[spX][spY][epX][epY][freq],
    						max(minValue(spX, spY, epX, a, freq-time-1),
    							minValue(spX, a, epX, epY, time)));
    			
    			return markLib[spX][spY][epX][epY][freq];
    		}
    			
    	}
    }
    
    int main()
    {
    	FILE *fw;
    	int result;
    	constructor();
    	result = minValue(0, 0, n-1, n-1, k);
    	fw = fopen("library.out", "+w");
    	fprintf(fw, "%d\n", result);
    	fclose(fw);
    	return 0;
    }
    library.dat
    Code:
    3 4
    1 2 3
    4 5 6
    7 8 9

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    What exactly is "the problem"? Is it crashing, giving the wrong results, what?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    5

    read the man page, check the modes

    i compiled your code ok. it causes a segv.

    read the man page or other documentation for fscanf. use a debugger to see that the problem is the last line of this part of your code:

    Code:
    void constructor()
    {
    	FILE *fr;
    	int a, b, c, d, e;
    	fr = fopen("library.dat", "+r");
    	fscanf(fr, "%d %d", &n, &k);
    also, you should check fr to make sure it is not null before you use it.......

    read the man page for fopen too, especially the part about modes
    Last edited by jmholber; 05-15-2006 at 12:18 PM. Reason: add more details

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by Mathsniper
    The compiler(GCC) hasn't given the bugs. And I can't find the problem.
    I believe that GCC won't tell you what the bugs are anyway. What you want is GDB. This will tell you where you have gone wrong (if, indeed, you have gone wrong). GCC is just a compiler.

    It's not clear from your post whether or not you understand the distinction so I thought it was worth pointing out. My apologies if you were already aware of this.

    Hope this helps.

    TV

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you have that huge five dimensional array? Is there a reason you need four point eight million integers?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Other than the occasion question of why you need the giganto array...
    Code:
    	for (a = 0; a < libSize; a++)
    		for (b = 0; b < libSize; b++)
    			lib[a][b] = 0;
    	for (a = 0; a < libPointSize; a++)
    		for (b = 0; b < libPointSize; b++)
    			for (c = 0; c < libPointSize; c++)
    				for (d = 0; d < libPointSize; d++)
    					for (e = 0; e < libTimeSize; e++)
    						markLib[a][b][c][d][e] = 10000;
    That. The first smaller loop can be done with a call to memset. As for the second loop, it could be done using only one for loop... something like:
    Code:
    int x, *iarray = markLib;
    for(x = 0; x < libTimeSize * libPointSize * libP... * libPointSize; ++x) *iarray = 10000;
    Cuts down on variables, and code...

    The cast to (int) in areaValue is odd, and seems to be unneeded...
    You have global variables with the same name as local variables... (though declared after the functions that have them as locals... bad style in my book.) Why are the variables a, b, and time global? They're only used in that function...
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    I see a bunch of functions returning results which go unchecked.
    Perhaps you should start with them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. relation between bugs and compilation time
    By KIBO in forum Tech Board
    Replies: 10
    Last Post: 01-29-2009, 01:29 PM
  2. Bugs in Program Question
    By racerday182 in forum C++ Programming
    Replies: 14
    Last Post: 12-05-2008, 10:30 AM
  3. what are the bugs in this code..please help me.
    By me001 in forum C Programming
    Replies: 12
    Last Post: 09-23-2008, 11:52 AM
  4. Need help finding bugs
    By Shakti in forum Game Programming
    Replies: 16
    Last Post: 02-13-2005, 01:42 AM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21