GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Nov-2009, 11:35
fastest fastest is offline
New Member
 
Join Date: Sep 2009
Location: Italy
Posts: 2
fastest is on a distinguished road

Dynamic allocation of matrix


Dear All,

Im experiencing some problems with the dynamic allocation of a matrix, in fact the problem is a little bit more complicated: I come to the details.

I want to read matrices from several files and perform some statistical analysis on those data.
So I created a list of files which should be red, and then for each of them I read the matrix, store in some way, and perform statistic. Now I have two problems:

1) Working on windows with MSVC I compile easily the cose and if I run it with only one file (without the free() command for the matrix) everything works well. The program instead crashes when it comes to freeing the matrix, so the problem should be somewhere there.

2) Under Linux (Ubuntu 9.10) with the same code I compile correctly but I cannot run the program because it crashes soon after it reads the matrix.Giving this error:
*** glibc detected *** ./amatr.out: free(): invalid next size (normal): 0x0c6a29c8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb77b3ff1]
/lib/tls/i686/cmov/libc.so.6[0xb77b56f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xb77b879d]
/lib/tls/i686/cmov/libc.so.6(fclose+0x14a)[0xb77a450a]
./amatr.out[0x8048bd1]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb775fb56]
./amatr.out[0x80487a1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:06 392510 /home/uzzen/Laura/prova/amatr.out
0804a000-0804b000 r--p 00001000 08:06 392510 /home/uzzen/Laura/prova/amatr.out
0804b000-0804c000 rw-p 00002000 08:06 392510 /home/uzzen/Laura/prova/amatr.out
089e7000-0c6ab000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b771a000-b7736000 r-xp 00000000 08:05 567 /lib/libgcc_s.so.1
b7736000-b7737000 r--p 0001b000 08:05 567 /lib/libgcc_s.so.1
b7737000-b7738000 rw-p 0001c000 08:05 567 /lib/libgcc_s.so.1
b7747000-b7749000 rw-p 00000000 00:00 0
b7749000-b7887000 r-xp 00000000 08:05 262412 /lib/tls/i686/cmov/libc-2.10.1.so
b7887000-b7889000 r--p 0013e000 08:05 262412 /lib/tls/i686/cmov/libc-2.10.1.so
b7889000-b788a000 rw-p 00140000 08:05 262412 /lib/tls/i686/cmov/libc-2.10.1.so
b788a000-b788d000 rw-p 00000000 00:00 0
b788d000-b78b1000 r-xp 00000000 08:05 262420 /lib/tls/i686/cmov/libm-2.10.1.so
b78b1000-b78b2000 r--p 00023000 08:05 262420 /lib/tls/i686/cmov/libm-2.10.1.so
b78b2000-b78b3000 rw-p 00024000 08:05 262420 /lib/tls/i686/cmov/libm-2.10.1.so
b78be000-b78c4000 rw-p 00000000 00:00 0
b78c4000-b78c5000 r-xp 00000000 00:00 0 [vdso]
b78c5000-b78e0000 r-xp 00000000 08:05 517 /lib/ld-2.10.1.so
b78e0000-b78e1000 r--p 0001a000 08:05 517 /lib/ld-2.10.1.so
b78e1000-b78e2000 rw-p 0001b000 08:05 517 /lib/ld-2.10.1.so
bf848000-bf85d000 rw-p 00000000 00:00 0 [stack]

I am working with matrices of about 2000 rows X 8000 columns.
The cose is as follows:
CPP / C++ / C Code:
	do
	{
		fscanf(list,"%s",in);
		if((fin = fopen(in,"r")) == NULL)	{
			printf("The file %s is not available for reading\n", infi);
			exit(0);	}
		for(i = 0; i < 6; i++)	
			{
			fgets(cestino,200,fin);
			fputs(cestino,stdout);
			}
		rewind(fin);
		//Lettura del file in formato grid//
		fscanf(fin,"%s %d",ncols,&colonne);
		fscanf(fin,"%s %d",nrows,&righe);
		
			for(i = 0; i < 5; i++)
			{ 
				fgets(cestino,200,fin);
			}
		
		///allocazione dinamica della matrice matrice///
		
		if((matrice=(float **)malloc(righe*sizeof( * float))) == NULL)
		{
			printf("memoria non pronta per *matrice\n");
			exit(0);
		}
		for(i = 1; i <= righe; i++)
		{
			if((matrice[i] = (float*)malloc(colonne*sizeof(float))) == NULL)
			{
				printf("memoria non pronta per **matrice");
				exit(0);
			}		
		}
		//lettura della matrice
		for(i = 1; i <= righe; i++)
		{ 

			for(j = 1;j <= colonne; j++)
			{ 
				fscanf(fin,"%f ",&matrice[i][j]);
			
			}
		}
		printf("Matrice %d x %d letta con successo\n", righe, colonne);
		
		if((fout = fopen("media_sqm.txt","w")) == NULL)	{
			printf("The file is not available for writing\nGoodbye\n");
			exit(0);	}
			
		if(dim==3)
			{ 
			numdati=0;
			k=0;
			nn1 = colonne;
			nn2 = righe;
			j=2;
			i=2;
			appoggio2 = 0;
			media = 0;
			while(i <= (nn2-1))
			{ 

				while(j <= (nn1))
				{ 
					appoggio2 = matrice[i][j]+matrice[i-1][j-1]+matrice[i][j-1]+matrice[i+1][j-1]+matrice[i-1][j]+matrice[i+1][j]+
					matrice[i-1][j+1]+matrice[i][j+1]+matrice[i+1][j+1];
					media = appoggio2/9;
					sqm = sqrt(((matrice[i][j]-media)*(matrice[i][j]-media)+(matrice[i-1][j-1]-media)*(matrice[i-1][j-1]-media)+
					(matrice[i][j-1]-media)*(matrice[i][j-1]-media)+(matrice[i+1][j-1]-media)*(matrice[i+1][j-1]-media)+
					(matrice[i-1][j]-media)*(matrice[i-1][j]-media)+(matrice[i+1][j]-media)*(matrice[i+1][j]-media)+
					(matrice[i-1][j+1]-media)*(matrice[i-1][j+1]-media)+(matrice[i][j+1]-media)*(matrice[i][j+1]-media)+
					(matrice[i+1][j+1]-media)*(matrice[i+1][j+1]-media))/9);
					fprintf(fout,"%4.1lf	%4.1lf\n",media,sqm);
					j=(j+3);
					
				}
				j=2;
				i=(i+3);
			}
			fclose(fin);
			fclose(fout);
			}
		else
			{ 
				numdati=0;
				k=0;
				nn1 = colonne;
				nn2 = righe;
				j=3;
				i=3;
				while(i <= (nn2-2))
				{ 

					while(j <= (nn1-1))
					{ 
						appoggio2=matrice[i][j]+matrice[i-1][j-1]+matrice[i][j-1]+matrice[i+1][j-1]+
						matrice[i-1][j]+matrice[i+1][j]+matrice[i-1][j+1]+matrice[i][j+1]+matrice[i+1][j+1]+
						matrice[i-2][j-2]+matrice[i-2][j-1]+matrice[i-2][j]+matrice[i-2][j+1]+matrice[i-2][j+2]+
						matrice[i-1][j-2]+matrice[i-1][j+2]+matrice[i][j-2]+matrice[i][j+2]+matrice[i+1][j-2]+
						matrice[i+1][j+2]+matrice[i+2][j-2]+matrice[i+2][j-1]+matrice[i+2][j]+matrice[i+2][j+1]+matrice[i+2][j+2];
						media=appoggio2/25;
						sqm=sqrt(((matrice[i][j]-media)*(matrice[i][j]-media)+(matrice[i-1][j-1]-media)*(matrice[i-1][j-1]-media)+
						(matrice[i][j-1]-media)*(matrice[i][j-1]-media)+(matrice[i+1][j-1]-media)*(matrice[i+1][j-1]-media)+
						(matrice[i-1][j]-media)*(matrice[i-1][j]-media)+(matrice[i+1][j]-media)*(matrice[i+1][j]-media)+
						(matrice[i-1][j+1]-media)*(matrice[i-1][j+1]-media)+(matrice[i][j+1]-media)*(matrice[i][j+1]-media)+
						(matrice[i+1][j+1]-media)*(matrice[i+1][j+1]-media)+(matrice[i-2][j-2]-media)*(matrice[i-2][j-2]-media)+
						(matrice[i-2][j-1]-media)*(matrice[i-2][j-1]-media)+(matrice[i-2][j]-media)*(matrice[i-2][j]-media)+
						(matrice[i-2][j+1]-media)*(matrice[i-2][j+1]-media)+(matrice[i-2][j+2]-media)*(matrice[i-2][j+2]-media)+
						(matrice[i-1][j-2]-media)*(matrice[i-1][j-2]-media)+(matrice[i-1][j+2]-media)*(matrice[i-1][j+2]-media)+
						(matrice[i][j-2]-media)*(matrice[i][j-2]-media)+(matrice[i][j+2]-media)*(matrice[i][j+2]-media)+
						(matrice[i+1][j-2]-media)*(matrice[i+1][j-2]-media)+(matrice[i+1][j+2]-media)*(matrice[i+1][j+2]-media)+
						(matrice[i+2][j-2]-media)*(matrice[i+2][j-2]-media)+(matrice[i+2][j-1]-media)*(matrice[i+2][j-1]-media)+
						(matrice[i+2][j]-media)*(matrice[i+2][j]-media)+(matrice[i+2][j+1]-media)*(matrice[i+2][j+1]-media)+
						(matrice[i+2][j+2]-media)*(matrice[i+2][j+2]-media))/25);
						fprintf(fout,"%4.1f	%4.1f\n",media,sqm);
						j=(j+5);
						
					}
					j=3;
					i=(i+5);
				}
				printf("Fino a qui tutto ok");
				fclose(fin);
				fclose(fout);
			}
		printf("Fino a qui tutto ok");	
		for(i = 1; i <= righe; i++)
			free(matrice[i]);
		free(matrice);		
	} while((feof(list)) == 0);

Any help is appreciated.
Thank you in advance.

Fastest
Last edited by LuciWiz : 05-Nov-2009 at 12:49. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 05-Nov-2009, 12:31
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Dynamic allocation of matrix


After just glancing at the code, I noticed one thing that stood out to me: All of your loops referencing the matrices start with one and include the number of matrices. In C, arrays are zero-based, not one-based:

CPP / C++ / C Code:
// create an array of 10 integers
int *array = (int*)malloc(10*sizeof(int));

// access the array zero-based
for(int i = 0; i < 10; ++i)
{
  array[i] = i * 2;
}
  #3  
Old 05-Nov-2009, 14:27
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Dynamic allocation of matrix


The problem is that you used tabs in your code. Try it with spaces instead.
Also , you should post a complete example. We can't even see how your data is declared!
  #4  
Old 06-Nov-2009, 03:27
fastest fastest is offline
New Member
 
Join Date: Sep 2009
Location: Italy
Posts: 2
fastest is on a distinguished road

Re: Dynamic allocation of matrix


Dear all,
as fakepoo suggested I tried to change to one based array to zero based array.
Incredibly it works now: under Linux environment it runs until the end.
Thank you also to Howard, even if I did not get exactly what he was meaning with
Quote:
"The problem is that you used tabs in your code. Try it with spaces instead."
Thank you for the help
Regards
Fastest
  #5  
Old 06-Nov-2009, 07:40
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Dynamic allocation of matrix


I meant read about "using tabs in the code you post here" in the Guidelines
(I was kidding about tabs breaking your program (or was I))
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic 2D Array / Matrix TransformedBG C++ Forum 4 14-Jan-2007 12:03
i need help in C++ PLZ its_me C++ Forum 3 04-Dec-2006 22:51
Help with OOP classes and Dynamic Allocation noob2c++ C++ Forum 3 18-Sep-2006 18:41
Dynamic memory allocation cpit C++ Forum 1 15-Sep-2006 12:55
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 07:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:07.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.