Calloc of "unsigned char my_bytes[256][256]"

M

M-One

See subject: how do I calloc (and free the memory, if that's not
free(my_bytes);) this?

TIA!
 
M

Mike Wahler

M-One said:
See subject: how do I calloc (and free the memory, if that's not
free(my_bytes);) this?

Have you tried looking up the documentation for calloc
and free?

#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned char *my_bytes = calloc(256 * 256, sizeof *my_bytes);
if(my_bytes == NULL)
{
fprintf(stderr, "Cannot allocate memory");
return EXIT_FAILURE;
}

/* whatever */

free(my_bytes);
return EXIT_SUCCESS;
}

-Mike
 
M

M-One

See subject: how do I calloc (and free the memory, if that's not
> Have you tried looking up the documentation for calloc
> and free?

Yes; thanks for the example.
 
G

Giuseppe

[...]
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256


typedef struct ARRAY{
unsigned char **array1;
size_t nrows;
size_t ncolumns;
}ARRAY;


int resizeARRAY(ARRAY *p, size_t nrows, size_t ncolumns);
void freeARRAY(ARRAY *p);


int main(void)
{ ARRAY my={NULL, 0, 0};

if(resizeARRAY(&my, NROWS, NCOLUMNS))
{my.array1[255][255] = 'c';
printf("my.array1[255][555] = \'%c\'\n", my.array1[255][255]);
}

freeARRAY(&my);
return 0;
}


int resizeARRAY(ARRAY* p, size_t nrows, size_t ncolumns)
{ size_t i;
unsigned char **temp;

if( (temp=realloc( p->array1, nrows * sizeof(*temp) )) == NULL )
R 0;
p->array1=temp;
for( i=0; i<nrows; i++ )
{p->array1=malloc( ncolumns * sizeof( *(p->array1) ) );
if(p->array1 == NULL)
{F( --i; i!=0; --i)
free( p->array1 );
free( p->array1[0] );
free( p->array1 );
R 0;
}
}
p->nrows = nrows ;
p->ncolumns = ncolumns;
return i; /* Ritorna il numero di righe allocate; */
}


void freeARRAY(ARRAY* p)
{ size_t i;

for(i=0; i < p->nrows; i++)
free( p->array1 );
free( p->array1 );
p->array1 = 0;
p->ncolumns = 0;
p->nrows = 0;
}


void freeARRAY1(ARRAY* p)
{ size_t i;

for(i=0; i < p->nrows; i++)
free( p->array1 );
free( p->array1 );
free(p);
}
 
M

Martin Ambuhl

(e-mail address removed) (Giuseppe) wrote (07 Jul 2003) in
/ comp.lang.c:
[...]
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

He's back. And as stupid as ever.
 
M

Mark McIntyre

On Mon, 07 Jul 2003 16:17:23 GMT, in comp.lang.c ,
[...]
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

Why do you persist in this obfuscatory stupidity? Do you think it
makes you look clever? Do you think its useful for others? Top Tip: it
makes you look a prat and everyone is ignoring you.
typedef struct ARRAY{
unsigned char **array1;
size_t nrows;
size_t ncolumns;
}ARRAY;

By common convention in C, uppercase is reserved for macros,
Microsoft's gratuitous ignoring of that convention notwithstanding.
 
R

Randy Howard

#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256


typedef struct ARRAY{
unsigned char **array1;
size_t nrows;
size_t ncolumns;
}ARRAY;


Okay, that's one time too many, I've given up. I don't normally
ever do this, but ...

*plonk*
 
G

Giuseppe

#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256

typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;

int resizeARRAY(ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY(ARRAY *p);
void freeARRAY_1(ARRAY *p);
ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b);
void stampa(ARRAY* a);

int main(void)
{
ARRAY *r=0, *a=0, *b=0;

if(resizeARRAY(&r, 3, 3) * resizeARRAY(&a, 3, 3)*
resizeARRAY(&b, 3, 3)!= 0 )
{
a->a[0][0]= 9; a->a[0][1]=15; a->a[0][2]=20;
a->a[1][0]=10; a->a[1][1]=14; a->a[1][2]=20;
a->a[2][0]=10; a->a[2][1]=15; a->a[2][2]=19;

b->a[0][0]=1 ; b->a[0][1]=1 ; b->a[0][2]=2 ;
b->a[1][0]=1 ; b->a[1][1]=1 ; b->a[1][2]=2 ;
b->a[2][0]=1 ; b->a[2][1]=1 ; b->a[2][2]=2 ;
if(somma(r, a, b));
{stampa(a);P("+\n"); stampa(b);P("==\n"); stampa(r);}

}
freeARRAY_1(r); freeARRAY_1(a); freeARRAY_1(b);
R 0;
}

void stampa(ARRAY* a)
{ size_t i, j;

if(!a) R ;
F( i=0; i<a->nrows; ++i )
{P("\n");
F( j=0; j<a->nco ; ++j)
P("%3u ", (unsigned) a->a[j]);
}
P("\n");
}

ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b)
{
size_t i, j;
unsigned char h;

if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
R 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
R 0;
F( i=0; i< r->nrows; ++i)
F( j=0; j< r->nco; ++j)
{h= a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
R 0;
r->a[j]=h;
}
R r;
}

int resizeARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i, j=0;
unsigned char **temp;

if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
R 0;
j=1;
}
if( (temp=realloc( (*p)->a, nrows * sizeof(*temp) )) == NULL )
{if(j) free( *p );
R 0;
}
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a );
if(j) free( *p );
R 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return i; /* Ritorna il numero di colonne allocate; */
}

void freeARRAY(ARRAY* p)
{ size_t i;

if(!p) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
p->a = 0;
p->nco = 0;
p->nrows = 0;
}

void freeARRAY_1(ARRAY* p)
{ size_t i;

if(!p) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
free(p);
}
 
G

Giuseppe

int resizeARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i, j=0;
unsigned char **temp;

if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
R 0;
j=1;
}
if( (temp=realloc( (*p)->a, nrows * sizeof(*temp) )) == NULL )
{if(j) free( *p );
*p=0;

if(j){free(*p); *p=0; }
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a );
if(j) {free( *p ); *p=0; }
R 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return i; /* Ritorna il numero di colonne allocate; */
}
 
G

Giuseppe

#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256

typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;

int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns);
int rszARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY (ARRAY* p);
void freeARRAY_d(ARRAY* p);
ARRAY* somma (ARRAY* r, ARRAY* a, ARRAY* b);
void stampa (ARRAY* a);

int main(void)
{
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;

if(creaARRAY(&mmy, NROWS, NCOLUMNS))
{my.a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
}

if(rszARRAY(&mmy, NROWS+100, NCOLUMNS+100))
{my.a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
my.a[255+100][255+100]);
}
freeARRAY(&my);

if(creaARRAY(&r, 3, 3) * creaARRAY(&a, 3, 3)*
creaARRAY(&b, 3, 3)!= 0 )
{
a->a[0][0]= 9; a->a[0][1]=15; a->a[0][2]=20;
a->a[1][0]=10; a->a[1][1]=14; a->a[1][2]=20;
a->a[2][0]=10; a->a[2][1]=15; a->a[2][2]=19;

b->a[0][0]=1 ; b->a[0][1]=1 ; b->a[0][2]=2 ;
b->a[1][0]=1 ; b->a[1][1]=1 ; b->a[1][2]=2 ;
b->a[2][0]=1 ; b->a[2][1]=1 ; b->a[2][2]=2 ;
if(somma(r, a, b));
{stampa(a);P("+\n"); stampa(b);P("==\n"); stampa(r);}

}

if(rszARRAY(&r, NROWS, NCOLUMNS))
{(*r).a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", (*r).a[255][255]);
}

freeARRAY_d(r); freeARRAY_d(a); freeARRAY_d(b);
R 0;
}

void stampa(ARRAY* a)
{ size_t i, j;

if(!a || a->a==0) R ;
F( i=0; i<a->nrows; ++i )
{P("\n");
F( j=0; j<a->nco ; ++j)
P("%3u ", (unsigned) a->a[j]);
}
P("\n");
}

ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b)
{
size_t i, j;
unsigned char h;

if(r==0||a==0||b==0||r->a==0||a->a==0||b->a==0)
R 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
R 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
R 0;
F( i=0; i< r->nrows; ++i)
F( j=0; j< r->nco; ++j)
{h = a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
R 0;
r->a[j]=h;
}
R r;
}

int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i, j=0;

if(p==0) R 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
R 0;
j=1;
}
if( ( (*p)->a = malloc(nrows * sizeof( *((*p)->a)) ) ) == NULL )
{if(j) {free( *p ); *p=0;}
else {(*p)->nrows=0; (*p)->nco=0;}
R 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
if(j) {free( *p ); *p=0; }
else {(*p)->nrows=0; (*p)->nco=0;}
R 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return i; /* Ritorna il numero di colonne allocate; */
}

/* Da usarsi a matrice già creata */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i;
unsigned char **temp, *tmp;

if( p==0 || *p==0 )
R 0;
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
R 0;
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{if( i >= (*p)->nrows )
(*p)->a=0; /* non c'è un puntatore risultato di malloc */
if(( tmp=realloc((*p)->a, ncolumns * sizeof *tmp ) )==NULL)
{
if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
(*p)->nrows = 0;
(*p)->nco = 0;
R 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
return i; /* Ritorna il numero di colonne allocate; */
}

void freeARRAY(ARRAY* p)
{ size_t i;

if(!p) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
p->a = 0;
p->nco = 0;
p->nrows = 0;
}

void freeARRAY_d(ARRAY* p)
{ size_t i;

if(!p) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
free(p);
}

______________
C:\NG1>mat1
my.array1[255][255] = 'c'
my.array1[255+100][255+100] = 'c'

9 15 20
10 14 20
10 15 19
+

1 1 2
1 1 2
1 1 2
==

10 16 22
11 15 22
11 16 21
my.array1[255][255] = 'c'
 
T

Tom St Denis

Giuseppe said:
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define R return
#define F for

You're missing

#define I if
#define II int
#define V void
#define S *
#define T typedef

:)

Tom
 
G

Giuseppe

Due errori
#include <stdio.h>
Ho fatto una compilazione con gcc e in questa linea risulta un
carattere non normale-return alla fine
#include <stdlib.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256

typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;

int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns);
int rszARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY (ARRAY* p);
void freeARRAY_d(ARRAY* p);
ARRAY* somma (ARRAY* r, ARRAY* a, ARRAY* b);
void stampa (ARRAY* a);

int main(void)
{
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;

if(creaARRAY(&mmy, NROWS, NCOLUMNS))
{my.a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
}

if(rszARRAY(&mmy, NROWS+100, NCOLUMNS+100))
{my.a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
my.a[255+100][255+100]);
}
freeARRAY(&my);

if(creaARRAY(&r, 3, 3) * creaARRAY(&a, 3, 3)*
creaARRAY(&b, 3, 3)!= 0 )
{
a->a[0][0]= 9; a->a[0][1]=15; a->a[0][2]=20;
a->a[1][0]=10; a->a[1][1]=14; a->a[1][2]=20;
a->a[2][0]=10; a->a[2][1]=15; a->a[2][2]=19;

b->a[0][0]=1 ; b->a[0][1]=1 ; b->a[0][2]=2 ;
b->a[1][0]=1 ; b->a[1][1]=1 ; b->a[1][2]=2 ;
b->a[2][0]=1 ; b->a[2][1]=1 ; b->a[2][2]=2 ;
if(somma(r, a, b));
qui c'è un ; di troppo
{stampa(a);P("+\n"); stampa(b);P("==\n"); stampa(r);}

Dubito che qualcuno possa trovare un errore degno di nota (sono
fortunato?)
Saluti
 
G

Giuseppe

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256

typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;

int creaARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
int rszARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY (ARRAY* p);
void freeARRAY_d(ARRAY** p);
ARRAY* somma (ARRAY* r, ARRAY* a, ARRAY* b);
void stampa (ARRAY* a);

int main(void)
{ size_t i, j, h;
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;

srand((unsigned)time(0));
if(creaARRAY(&mmy, NROWS, NCOLUMNS))
{my.a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
}

if(rszARRAY(&mmy, NROWS+100, NCOLUMNS+100))
{my.a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
my.a[255+100][255+100]);
}
freeARRAY(&my);

F(h=0; h<1000; ++h)
{if(creaARRAY(&r, 3, 3) * creaARRAY(&a, 3, 3)*
creaARRAY(&b, 3, 3)!= 0 )
{F(i=0; i<3; ++i)
F(j=0; j<3; ++j)
a->a[j]= rand()%100;
F(i=0; i<3; ++i)
F(j=0; j<3; ++j)
b->a[j]= rand()%100;

if(somma(r, a, b))
{stampa(a);P("+\n"); stampa(b);P("==\n"); stampa(r);}

}
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
R 0;
}

void stampa(ARRAY* a)
{ size_t i, j;

if(!a) R ;
F( i=0; i<a->nrows; ++i )
{P("\n");
F( j=0; j<a->nco ; ++j)
P("%3u ", (unsigned) a->a[j]);
}
P("\n");
}

ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b)
{
size_t i, j;
unsigned char h;

if(r==0||a==0||b==0|| r->a==0||a->a==0||b->a==0)
R 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
R 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
R 0;
F( i=0; i< r->nrows; ++i)
F( j=0; j< r->nco; ++j)
{h=a->a[j]+b->a[j];
if( h< a->a[j] || h< b->a[j] )
R 0;
r->a[j]=h;
}
R r;
}

int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i, j=0;

if(p==0) R 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
R 0;
j=1;
}
if( ( (*p)->a = malloc(nrows * sizeof( *((*p)->a)) ) ) == NULL )
{if(j) {free( *p ); *p=0;}
else {(*p)->nrows=0; (*p)->nco=0;}
R 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a ); /* Libera il massimo che può */
if(j) {free( *p ); *p=0; }
else {(*p)->a=0; (*p)->nrows=0; (*p)->nco=0;}
R 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return i; /* Ritorna il numero di colonne allocate; */
}

/* Da usarsi a matrice già creata */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i;
unsigned char **temp, *tmp;

if( p==0 || *p==0 )
R 0;
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
R 0;
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{
if( i >= (*p)->nrows )
(*p)->a=0; /* non c'è un puntatore risultato di malloc */
if(( tmp=realloc( (*p)->a, ncolumns * sizeof *tmp ) )==NULL)
{
if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a );/* libera il massimo che può */
(*p)->a = 0;
(*p)->nrows = 0;
(*p)->nco = 0;
R 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
return i; /* Ritorna il numero di colonne allocate; */
}

void freeARRAY(ARRAY* p)
{ size_t i;

if( p==0 ) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
p->a = 0;
p->nco = 0;
p->nrows = 0;
}

void freeARRAY_d(ARRAY** p)
{ size_t i;

if( p==0 || *p==0 ) R;
for(i=0; i < (*p)->nrows; i++)
free( (*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}
_______
"Chi cerca troverà"
 
G

Giuseppe

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define P printf
#define R return
#define F for

#define NROWS 256
#define NCOLUMNS 256

typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;

int creaARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
int rszARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY (ARRAY* p);
void freeARRAY_d(ARRAY** p);
ARRAY* somma (ARRAY* r, ARRAY* a, ARRAY* b);
void stampa (ARRAY* a);

int main(void)
{ size_t i, j, h;
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;

srand((unsigned)time(0));
if(creaARRAY(&mmy, NROWS, NCOLUMNS))
{my.a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
}

if(rszARRAY(&mmy, NROWS+100, NCOLUMNS+100))
{my.a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
my.a[255+100][255+100]);
}
freeARRAY(&my);

F(h=0; h<1000; ++h)
{if(creaARRAY(&r, 3, 3) * creaARRAY(&a, 3, 3)*
creaARRAY(&b, 3, 3)!= 0 )
{F(i=0; i<3; ++i)
F(j=0; j<3; ++j)
a->a[j]= rand()%100;
F(i=0; i<3; ++i)
F(j=0; j<3; ++j)
b->a[j]= rand()%100;

if(somma(r, a, b))
{stampa(a);P("+\n"); stampa(b);P("==\n"); stampa(r);}

}
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
R 0;
}

void stampa(ARRAY* a)
{ size_t i, j;

if(!a) R ;
F( i=0; i< a->nrows; ++i )
{P("\n");
F( j=0; j< a->nco ; ++j)
P("%3u ", (unsigned) a->a[j]);
}
P("\n");
}

ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b)
{
size_t i, j;
unsigned char h;

if(r==0||a==0||b==0|| r->a==0||a->a==0||b->a==0)
R 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
R 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
R 0;
F( i=0; i< r->nrows; ++i)
F( j=0; j< r->nco; ++j)
{h=a->a[j]+b->a[j];
if( h< a->a[j] || h< b->a[j] )
R 0;
r->a[j]=h;
}
R r;
}

int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i, j=0;

if(p==0) R 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
R 0;
j=1;
}
if(((*p)->a = malloc(nrows * sizeof( *((*p)->a)))) == NULL)
{if(j) {free( *p ); *p=0;}
else {(*p)->nrows=0; (*p)->nco=0;}
R 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a ); /* Libera il massimo che può */
if(j) {free( *p ); *p=0; }
else {(*p)->a=0; (*p)->nrows=0; (*p)->nco=0;}
R 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
R i; /* Ritorna il numero di colonne allocate; */
}

/* Da usarsi a matrice già creata */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{ size_t i;
unsigned char **temp, *tmp;

if( p==0 || *p==0 )
R 0;
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
R 0;
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{if( i >= (*p)->nrows )
(*p)->a=0; /* non c'è un puntatore risultato di malloc */
if(( tmp=realloc( (*p)->a, ncolumns * sizeof *tmp ) )== NULL)
{i= i>=(*p)->nrows ? i: (*p)->nrows;
if(i)
F( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
free( (*p)->a );/* libera il massimo che può */
(*p)->a = 0;
(*p)->nrows = 0;
(*p)->nco = 0;
R 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
R i; /* Ritorna il numero di colonne allocate; */
}

void freeARRAY(ARRAY* p)
{ size_t i;

if( p==0 ) R;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
p->a = 0;
p->nco = 0;
p->nrows = 0;
}

void freeARRAY_d(ARRAY** p)
{ size_t i;

if( p==0 || (*p)==0 ) R;
for(i=0; i < (*p)->nrows; i++)
free( (*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}
___________
Chi cerca trova ma questa volta (spero) sarà difficile trovare.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top