Help me sortarisma (metrisi sixnotitas)

Giorgos Keramidas keramida at ceid.upatras.gr
Thu Jul 6 14:01:19 EEST 2006


On 2006-07-06 13:13, Dimitris Mexis <m65 at vivodinet.gr> wrote:
> Kalos, katalava. Tha asxolitho me tin lisi tou V13. Alla.
>
> 1) ta sxolia ston kodika, den ta vazo gia allous alla gia mena! Gia na
> thimamai ti exo kanei! polles fores den sou exei tixei na berdevese
> kai na xehnas?

Esy o idios, otan tha diabazeis to programma se ena mhna, epeidh exeis
ksexasei ti kanei to programma eisai, eks' orismou, "enas allos
programmatisths pou diabazei to idio programma".

Gi ayto ta sxolia prepei na einai safh, xrhsima kai perigrafika.

> 2) h main einai kati grammes perisoteres den ipirxe skopos na trexeis
> to programma. Gia afto den xreiazete na ta honeis. I 3 sinartiseis
> boroun na xrisimopoiithoun kalista me
>
> int main(int argc, char *argv[])
> {
> 	double matrix[10];
> 	cout << "before..." << endl;
> 	for ( int i = 0; i < 10; i++ ){
> 		matrix[i] = 10 - i;
> 		matrix[3] = 3;
> 		matrix[4] = 3;
> 		matrix[1] = 2;
> 		matrix[0] = 0;
> 		cout << matrix[i] << endl;
> 		}
> 	freq_array( matrix, ( sizeof( matrix )/sizeof( matrix[0] ) ) );
>
> 	exit(0);
> }
>
> alla den nomizo oti ena ebeiro mati tha xreiastei na trexi ton kodika
> e?  ;-)

Ena "empeiro mati" omws, afou dior8wsei ola ta warnings gia missing
headers kai alla la8h pou exei to programma sou mporei na 8elhsei na
treksei to programma bhma-pros-bhma se ena debugger.

> 3) To provlima einai oti nai isos den einai o kaliteros kodikas,
> SIGOURA, alla i "group by" opou kano delete kai null ta dedomena, THA
> eprepe na doulevi.

Oxi, giati einai grammenh me arketa xalia tropo.

Akoma kai me thn parapanw main() to programma bgazei edw:

  mexis.cpp: In function `int main(int, char**)':
  mexis.cpp:4: error: `cout' undeclared (first use this function)
  mexis.cpp:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
  mexis.cpp:4: error: `endl' undeclared (first use this function)
  mexis.cpp:13: error: `freq_array' undeclared (first use this function)
  mexis.cpp:15: error: `exit' undeclared (first use this function)
  mexis.cpp: At global scope:
  mexis.cpp:19: error: `size_t' has not been declared
  mexis.cpp:19: error: `size_t' has not been declared
  mexis.cpp:20: error: ISO C++ forbids declaration of `elements' with no type
  mexis.cpp:20: error: ISO C++ forbids declaration of `grpelements' with no type
  mexis.cpp: In function `void FreqArrayCount(float*&, float*&, int, int&)':
  mexis.cpp:21: error: `size_t' undeclared (first use this function)
  mexis.cpp:21: error: expected `;' before "i"
  mexis.cpp:23: error: `SortPrices' undeclared (first use this function)
  mexis.cpp:25: error: `i' undeclared (first use this function)
  mexis.cpp:26: error: `count' undeclared (first use this function)
  mexis.cpp:27: error: `k' undeclared (first use this function)
  mexis.cpp:28: error: `NULL' undeclared (first use this function)
  mexis.cpp:29: error: `fabs' undeclared (first use this function)
  mexis.cpp:29: error: `PRECISION' undeclared (first use this function)
  mexis.cpp:37: error: `GroupBy' undeclared (first use this function)
  mexis.cpp: At global scope:
  mexis.cpp:41: error: `size_t' has not been declared
  mexis.cpp:42: error: ISO C++ forbids declaration of `size' with no type
  mexis.cpp: In function `void SortPrices(float*&, int)':
  mexis.cpp:42: error: `void SortPrices(float*&, int)' used prior to declaration
  mexis.cpp: At global scope:
  mexis.cpp:69: error: `size_t' has not been declared
  mexis.cpp:69: error: `size_t' has not been declared
  mexis.cpp:70: error: ISO C++ forbids declaration of `elements' with no type
  mexis.cpp:70: error: ISO C++ forbids declaration of `grpelements' with no type
  mexis.cpp: In function `void GroupBy(float*&, float*&, int&, int&)':
  mexis.cpp:70: error: `void GroupBy(float*&, float*&, int&, int&)' used prior to declaration
  mexis.cpp:82: error: `fabs' undeclared (first use this function)
  mexis.cpp:82: error: `PRECISION' undeclared (first use this function)
  mexis.cpp:89: error: `NULL' undeclared (first use this function)
  mexis.cpp:92: error: `cout' undeclared (first use this function)
  mexis.cpp:92: error: `endl' undeclared (first use this function)
  mexis.cpp: At global scope:
  mexis.cpp:2: warning: unused parameter 'argc'
  mexis.cpp:2: warning: unused parameter 'argv'

Dior8wnontas ta la8h:

	* Pros8hkh twn headers pou leipoun <iostream>, <iomanip> kai <math.h>

	* Afairesh twn unused arguments argc kai argv apo th main()

	* Metonomasia ths freq_array() klhshs se FreqArrayCount()

	* Pros8hkh mias ShowMatrix() pou mporei na tupwsei ton pinaka se
	  opoiodhpote stadio.

	* Pros8hkh mias `matrixlen' metablhths anti gia xrhsh ths `magikhs'
	  sta8eras 10 sth main() kai opou allou anaferetai to megethos tou
	  pinaka.

	* Afairesh twn pointer-references, afou aploi pointers mporoun na
	  kanoun mia xara thn idia douleia.

	* Orismos tou 'PRECISION' pou einai undefined, se 0.001

	* Allagh olwn twn loop indexes apo `int' se `size_t', afou einai
	  diaforetikoi typoi (signed to ena, unsigned to allo)

Ta opoia opws blepeis den einai KATHOLOU liga...

Emfanizetai to pio sobaro bug pou exei to programma ws twra:

: giorgos at gothmog:/home/giorgos/tmp/mexis$ ./mexis
: Amesws meta thn arxikopoihsh: [ 0, 2, 8, 3, 3, 5, 4, 3, 2, 1,]
: Prin thn SortPrices: [ 0, 2, 8, 3, 3, 5, 4, 3, 2, 1,]
: Meta thn SortPrices: [ 0, 1, 2, 2, 3, 3, 3, 4, 5, 8,]
: Prin thn GroupBy: [ 0, 1, 2, 2, 3, 3, 3, 4, 5, 8,]
: Segmentation fault: 11 (core dumped)
: giorgos at gothmog:/home/giorgos/tmp/mexis$ gdb mexis mexis.core
: GNU gdb 6.1.1 [FreeBSD]
: Copyright 2004 Free Software Foundation, Inc.
: GDB is free software, covered by the GNU General Public License, and you are
: welcome to change it and/or distribute copies of it under certain conditions.
: Type "show copying" to see the conditions.
: There is absolutely no warranty for GDB.  Type "show warranty" for details.
: This GDB was configured as "i386-marcel-freebsd"...
: Core was generated by `mexis'.
: Program terminated with signal 11, Segmentation fault.
: Reading symbols from /usr/lib/libstdc++.so.5...done.
: Loaded symbols for /usr/lib/libstdc++.so.5
: Reading symbols from /lib/libm.so.4...done.
: Loaded symbols for /lib/libm.so.4
: Reading symbols from /lib/libc.so.7...done.
: Loaded symbols for /lib/libc.so.7
: Reading symbols from /libexec/ld-elf.so.1...done.
: Loaded symbols for /libexec/ld-elf.so.1
: #0  0x281b87d2 in idalloc (ptr=0xbfbfe890) at /home/build/src/lib/libc/stdlib/malloc.c:2879
: 2879    /home/build/src/lib/libc/stdlib/malloc.c: No such file or directory.
:         in /home/build/src/lib/libc/stdlib/malloc.c
: (gdb) bt
: #0  0x281b87d2 in idalloc (ptr=0xbfbfe890) at /home/build/src/lib/libc/stdlib/malloc.c:2879
: #1  0x281baf3e in free (ptr=0xbfbfe890) at /home/build/src/lib/libc/stdlib/malloc.c:3577
: #2  0x2811e8c1 in operator delete (ptr=0x0) at /home/build/src/gnu/lib/libstdc++/../../../contrib/libstdc++/libsupc++/del_op.cc:40
: #3  0x2811b9ef in operator delete[] (ptr=0xbfbfe890)
:     at /home/build/src/gnu/lib/libstdc++/../../../contrib/libstdc++/libsupc++/del_opv.cc:36
: #4  0x08048a9a in main () at mexis.cpp:125
: Current language:  auto; currently c
: (gdb) list
: 125         delete[] matrix;
: 126         delete[] freq;
: 127         matrix = NULL;
: 128         freq = NULL;
: 129
: 130         cout << "New elements :" << c << endl;
: 131
: 132         matrix = new float[c];
: 133         freq = new float[c];
: 134
: (gdb)

Kaneis delete ena array pou den exei ginei allocate me new pote.

As poume pws ayto einai bug ths main() pou ksepetakses etsi sta grhgora apo to
tsantaki tou Sport Billy, ki as allaksoume tous static pinakes se allocated
pinakes.  Ayto den pauei na einai blakeia ths GroupBy(), h opoia den kserei an
ontws einai heap-allocated oi pinakes, opote de tha prepe na to ypo8etei tyfla.

Telos pantwn.

Allazontas to:

    int
    main(void)
    {
        float matrix[10];

se:

    #define  MAGIC_MATRIX_LEN        10

    int
    main(void)
    {
        float *matrix = new float[MAGIC_MATRIX_LEN];
        float *matrix2 = new float[MAGIC_MATRIX_LEN];
        size_t matrixlen = MAGIC_MATRIX_LEN;
        size_t matrix2len = MAGIC_MATRIX_LEN;

Kia pros8etwntas tis katallhles ShowMatrix() klhseis sthn GroupBy() to
apotelesma einai:

: $ ./mexis 
: Amesws meta thn arxikopoihsh: [ 0, 2, 8, 3, 3, 5, 4, 3, 2, 1,]
: Prin thn SortPrices: [ 0, 2, 8, 3, 3, 5, 4, 3, 2, 1,]
: Meta thn SortPrices: [ 0, 1, 2, 2, 3, 3, 3, 4, 5, 8,]
: Prin thn GroupBy: [ 0, 1, 2, 2, 3, 3, 3, 4, 5, 8,]
: 
: Group loop 1, iteration 0
: Group loop 1, tmpMatrix : [ -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]
: Group loop 1, tmpFreq   : [ -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]
: 
: Group loop 1, iteration 1
: Group loop 1, tmpMatrix : [ 0, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]
: Group loop 1, tmpFreq   : [ -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]
: 
: Group loop 1, iteration 2
: Group loop 1, tmpMatrix : [ 0, 1, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]
: Group loop 1, tmpFreq   : [ -2.87352e-16, 1, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16, -2.87352e-16,]

Sthn arxh tou iteration 1 blepeis to bug?

De tha prepe to tmpFreq[0] na einai 1, afou to stoixeio 0 emfanizetai
mia fora ston pinaka tmpMatrix[]?

Synexise to debugging me to attachment...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: mexis.cpp
Type: text/x-c++src
Size: 3979 bytes
Desc: not available
URL: <http://lists.hellug.gr/pipermail/linux-greek-users/attachments/20060706/09f0f151/attachment.cpp>


More information about the Linux-greek-users mailing list