Diafores metaksy gcc 3.4 kai 3.3
Giorgos Keramidas
keramida at ceid.upatras.gr
Sat Nov 20 15:53:22 EET 2004
On 2004-11-20 10:54, Karaoulis Marios <marios at lemnos.geo.auth.gr> wrote:
> Eimai enas mallon metrios pros kakos programmatisths. Xrhsimopoio thn
> c++ gia kapoious algori8mopus pou grafo. Exo tsatistei me olh auth thn
> katastash pou epikratei sthn c++.
Hremia. Apla kapoio lathos kaneis :-P
> H logikh einai h ekshs
>
> #include <memorh.h>
> #include <iostream>
> #include <new>
> #include <excpeption>
> #include "marutil.h"
Mhpws to excpeption prepei na einai 'exception'?
> int main()
> {
> //pinaka a 20 x 10 diadtasewn. Perna kai to onoma tou pinaka gia naksero
> //an kapoios pinakas xtyphsei na ksero pou
>
> float **a=float_matrix(20,10,a);
> }
Bad idea!
Pernas thn timh pou exei kapoia stigmh, prin to function call, o pointer `a'.
Amesos meta to float_matrix() call teleiosei, h timh ayth einai praktika
invalid -- toulaxiston oson afora oti eixes kata nou otan thn apo8hkeyes
eswterika sthn float_matrix.
Estw oti prin to assignment:
float **a = float_matrix(20, 10, a);
to pointer `a' eixe timh 0 (kai kala NULL pointer, epeidh den to exeis
arxikopoihsei akoma). Ayth einai h timh pou tha parei h float_matrix.
Malista ayth h timh tha einai entelos axrhsth epeidh thn pernas mesw enos
char[] transformation tou pointer, pou den exei mallon kamia apolutws aksia
pleon -- exei xasei thn plhroforia oti einai float vector.
Ystera, h float_matrix() kanei ta dika ths, allocate kapoio block mnhmhs, kai
sou epistrefei kapoio pointer se ayth th 8esh mnhmhs. Fysika, ayth h 8esh
mnhmhs den exei kamia sxesh me thn prohgoymenh timh tou `a'. Opote dyo tina
mporei na ginoun:
a. H float_matrix krataei thn prohgoymenh timh pou eixe to `a', opote
tora o pointer ths deixnei ston koraka.
b. H float_matrix den krataei thn timh pou eixe to `a' prin to
allocation alla thn timh pou exei META. Opote den xreiazetai na ths pernas me
parametro thn palia timh, afou apla th grafei sta palia ths ta papoutsia.
Ta pragmata 8a mporouse na einai ligo pio diaforetika, an h float_matrix()
epairne ena "reference" h ena "pointer" sthn metablhth pou kanei allocate.
Xoris exception handling, gia na einai pio mikros kai katanohtos o kwdikas,
ennow kati san ayto:
// A vector of pointers to 2-dimensional float matrixes.
// This keeps a crude list of the areas allocated through float_matrix.
static float ****fpguard;
static int fpguardsz;
float **
float_matrix(int rows, int cols, float ***fp)
{
float **tmp;
int k;
assert(fp != NULL);
tmp = new float **[rows];
for (k = 0; k < rows; k++)
tmp[k] = new float *[cols];
// Save a pointer to the vector used to store our result.
// Then store a reference to the alloced area in `fp'.
marios::fpguard(fp);
return (*fp = tmp);
}
> #include<iostream>
> #include <stdlib.h>
> #include<stdio.h>
> #include<exception>
> #include<new>
> #include<memory.h>
> //#include "marutil.h"
> using namespace std;
Den einai kalo na xrhsimopoieis iostreams kai stdio.h sto idio programma. To
kathena apo auta mporei na kanei to diko tou bufferring, na xeirizetai ta idia
file descriptors me to diko tou ksexoristo tropo kai na sou kseskisei entelws
kathe ypothesi pou isos tha mporouses na kaneis gia to host systhma tou
programmatos.
Just don't.
Otan grafeis C++ h tha einai C++ all the way, h den tha grafeis se C++ :P
BTW, to indentation tou programmatos bghke epioikws xalia. Den ksero an
ftaiei o mailer sou h ontws einai etsi. Einai xali omws. Anagkasthka kai to
ekana indent prin prospathiso kan na to diabaso.
: /*------------------------------------------------------------------------------
: Function To Allocate A 3D FLOAT Matrix mXnXz dimensions
: --------------------------------------------------------------------------------*/
:
: float ***
: float_3d(int m, int n, int z, char array[])
: {
: int i, j;
: float ***tmp_mtx;
:
: try {
: tmp_mtx = new float **[m];
: } catch(bad_alloc exception) {
: cout << "Error Allocating " << array << " In Dimension 1" << ".Error message is :" << exception.what();
: exit(1);
: }
: try {
: for (i = 0; i < m; i++) {
: tmp_mtx[i] = new float *[n];
:
: try {
: for (j = 0; j < n; j++) {
: tmp_mtx[i][j] = new float[z];
: }
: } catch(bad_alloc exception) {
: cout << "Problem In " << array << " Dimension 3" << ".Error message is :" << exception.what();
: exit(1);
: }
: }
: } catch(bad_alloc exception) {
: cout << "Problem In " << array << " Dimension 2" << ".Error message is :" << exception.what();
: exit(1);
: }
:
: return tmp_mtx;
: }
Olo ayto to terastio pragma gia na mporeseis na kaneis allocate ena 3-diastato
pinaka? Eleos :P
Epishs, to exception handling sou einai ligo mysthrio, afou den einai ki h
kalyterh idea olwn twn epoxwn ena exception handler na kanei xyma exit(1).
Olo ayto ego mallon tha to egrafa (xoris to axrhsto array[] argument):
float ***
float_3d(int rows, int cols, int depth)
{
try {
float ***p;
int j, k;
p = new float **[rows];
for (j = 0; j < rows; j++)
p[j] = new float *[cols];
for (j = 0; j < rows; j++)
for (k = 0; k < depth; k++)
p[j][k] = new float[depth];
} catch (e exception) {
return (NULL);
}
}
Skepsou poso tha se brizei kapoios pou tha kanei link me ti biblio8hkh sou
otan anakalupsei oti epeidh apetyxe ena allocation ekane exit(1) olo tou to
programma kai den prolabe p.x. na grapsei kapoia pragmata pou h8ele sto disko.
: int ***
: int_3d(int m, int n, int z, char array[])
: {
Antistoixa sxolia me parapano.
: float **
: float_matrix(int m, int n, char array[])
: {
Mia apo ta idia.
: int **
: int_matrix(int m, int n, char array[])
: {
Epishs.
: /*------------------------------------------------------------------------------
: Function To Allocate A FLOAT Vector m dimensions
: --------------------------------------------------------------------------------*/
:
: float *
: float_vector(int m, char array[])
: {
To sxolio den exei sxesh me to ti kanei o kwdikas. Ektos ki an apla einai
ligo lathos h xrhsh ths lekshs 'dimension' ekei pou den prepei. H synarthsh
amesos parapato, h float_vector() kanei swsta ayto pou leei to onoma ths,
kanei allocate ena float vector.
Aytos einai ki enas apo tous logous pou sxolia tetoiou styl einai axrhsta kai
prepei na paraleipontai. Eite einai entelws peritta, afou den prosthetoun
sthn katanohsh tou kwdika, eite einai entelws lathos kai blaptoun perissotero
apo oti boh8oun :P
: ///////////////////////////////////////////////////////////////////////////////////////////////
: /////////////////////////////////DEALOCATE /////////////////////////////////////////////////////
: ///////////////////////////////////////////////////////////////////////////////////////////////
Eleos.
O kwdikas den prepei na moiazei me ypopshfio entry se diagwnismo ASCII ART.
More information about the Linux-greek-users
mailing list