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

M

Mark McIntyre

On Tue, 08 Jul 2003 16:15:41 GMT, in comp.lang.c ,
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define P printf
#define R return
#define F for

okay, thats enough., You're plonked. Now fsck off.
 
P

pete

Richard said:
This code is sufficient evidence that the person writing it is not an
experienced C programmer.
Any advice he gives should therefore be treated
with suspicion.

Those who wish to write such code for kicks have the IOCCC.
Those who wish
it to be taken seriously are deluding themselves.

I wish he would at least try to take himself seriously,
and recognize that he doesn't even like these macros himself:

printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
printf("my.array1[255+100][255+100] = \'%c\'\n"
return i; /* Ritorna il numero di colonne allocate; */
return i; /* Ritorna il numero di colonne allocate; */
for( i=0; i<nrows; i++ )
for( i=0; i<nrows; i++ )
for(i=0; i < p->nrows; i++)
for(i=0; i < (*p)->nrows; i++)
 
A

Al Bowers

Giuseppe said:
/* 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; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.
 
G

Giuseppe

Giuseppe said:
/* 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 */
return i; /* Ritorna il numero di colonne allocate; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.


I have seen it in my last message and
i= i>=(*p)->nrows ? i: (*p)->nrows; is right?
Thank you
 
A

Al Bowers

Giuseppe said:
Giuseppe said:
/* 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 */
return i; /* Ritorna il numero di colonne allocate; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.



I have seen it in my last message and
i= i>=(*p)->nrows ? i: (*p)->nrows; is right?


No.
Take for example that you use the function to allocated an array,
arr[256][256].
Then you come back with this function a reallocate the array,
arr[4][256].
The statement: temp= realloc( (*p)->a, nrows * sizeof *temp )
will reduce the allocation(assuming it is successful), but at
this point you have lost the pointers arr[4],arr[5],.....arr[255]
which pointed to previous allocated memory. So, now, how are you
going to free these allocations?

One way to solve this is redesign the function to allocated
a new array of the new size. Then copy over the data from
the old array that will fit in the new array. Then delete the
old array.

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

/* Create array of new size */
if((temp = malloc(nrows * sizeof *temp)) == NULL) return 0;
for(i = 0; i < nrows; i++)
if((temp = malloc(ncolumns * sizeof **temp)) == NULL)
{
flag = 0;
break;
}
if(flag == 0) /* Allocation error in new array, free and return */
{
for( ; i != 0; i--) free(temp[i-1]);
free(temp);
return 0;
}
/* copy contents of old array to new */
for(i = 0;i < (p->nrows<nrows?p->nrows:nrows) ; i++)
for(n = 0; n < (p->ncolumns<ncolumns?p->ncolumns:ncolumns);n++)
temp[n] = p->array1[n];

freeARRAY(p);
p->array1 = temp;
p->ncolumns = ncolumns;
p->nrows = nrows;
return 1;
}


void freeARRAY(ARRAY *p)
{
size_t i;

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



--
 
G

Giuseppe

if( nrows < (*p)->nrows )
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 */
return i; /* Ritorna il numero di colonne allocate; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.



I have seen it in my last message and
i= i>=(*p)->nrows ? i: (*p)->nrows; is right?


No.
Take for example that you use the function to allocated an array,
arr[256][256].
Then you come back with this function a reallocate the array,
arr[4][256].
The statement: temp= realloc( (*p)->a, nrows * sizeof *temp )
will reduce the allocation(assuming it is successful), but at
this point you have lost the pointers arr[4],arr[5],.....arr[255]


yes, and it could be easy free them in this way
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );

but the game could be too much difficult for me.
(but it seems to work ok)
which pointed to previous allocated memory. So, now, how are you
going to free these allocations?

One way to solve this is redesign the function to allocated
a new array of the new size. Then copy over the data from
the old array that will fit in the new array. Then delete the
old array.

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

/* Create array of new size */
if((temp = malloc(nrows * sizeof *temp)) == NULL) return 0;
for(i = 0; i < nrows; i++)
if((temp = malloc(ncolumns * sizeof **temp)) == NULL)
{
flag = 0;
break;
}
if(flag == 0) /* Allocation error in new array, free and return */
{
for( ; i != 0; i--) free(temp[i-1]);
free(temp);
return 0;
}
/* copy contents of old array to new */
for(i = 0;i < (p->nrows<nrows?p->nrows:nrows) ; i++)
for(n = 0; n < (p->ncolumns<ncolumns?p->ncolumns:ncolumns);n++)
temp[n] = p->array1[n];

freeARRAY(p);
p->array1 = temp;
p->ncolumns = ncolumns;
p->nrows = nrows;
return 1;
}


void freeARRAY(ARRAY *p)
{
size_t i;

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



--
--
Al Bowers
Tampa, Fl USA
mailto: (e-mail address removed) (remove the x)
http://www.geocities.com/abowers822/
 
G

Giuseppe

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

#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);

for(h=0; h<50; ++h)
{unsigned len=rand()%20+1;
if(creaARRAY(&r, len, len) * creaARRAY(&a, len, len)*
creaARRAY(&b, len, len)!= 0 )
{for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
a->a[j]= rand()%100;
for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
b->a[j]= rand()%100;

if(somma(r, a, b))
{stampa(a); printf("+ \n");
stampa(b); printf("==\n");
stampa(r);
}
}
if(rszARRAY(&r, NROWS+100, NCOLUMNS+100))
{(*r).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*r).a[255+100][255+100]);
}
if(rszARRAY(&a, 3, 3))
{(*a).a[2][2] = 'c';
printf("my.array1[2][2] = \'%c\'\n", (*a).a[2][2]);
}
if(rszARRAY(&a, NROWS+100, NCOLUMNS+100))
{(*a).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*a).a[255+100][255+100]);
}
if(rszARRAY(&a, 1, 1))
{(*a).a[0][0] = 'c';
printf("my.array1[0][0] = \'%c\'\n", (*a).a[0][0]);
}
if(rszARRAY(&a, 1, 1))
(*a).a[0][0] = 'c';
//if(rszARRAY(&a, 0, 1));
//if(rszARRAY(&a, 1, 0));
//if(rszARRAY(&a, 0, 0)); non funzionano
if(rszARRAY(&a, 100, 199))
(*a).a[99][99] = 'c';
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
return 0;
}

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

if(!a) return ;
for( i=0; i< a->nrows; ++i )
{printf("\n");
for( j=0; j< a->nco ; ++j)
printf("%3u ", (unsigned) a->a[j]);
}
printf("\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)
return 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
return 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
return 0;
for( i=0; i< r->nrows; ++i)
for( j=0; j< r->nco; ++j)
{h = a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
return 0;
r->a[j]=h;
}
return r;
}

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

if(p==0) return 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
return 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;}
return 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
{for( --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;}
return 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return 1;
}

/* Da usarsi a matrice già creata con creaARRAY (e dimensione>0) */
/* Se riesce a fare il resize ritorna 1 altrimenti **p={0, 0, 0} */
/* *da liberare subito dopo* con l'opportuno freeARRAY */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{size_t i;
unsigned char **temp, *tmp;

if( p==0 || *p==0 )
return 0;
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
{i = nrows<(*p)->nrows ? nrows: (*p)->nrows ;
goto label;/* It will ok? */
}
(*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;
label:
if(i)
{for(--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;
return 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
return 1;
}

/* matrice parzialmente dinamica */
void freeARRAY(ARRAY* p)
{size_t i;

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

/* matrice totalmente dinamica */
void freeARRAY_d(ARRAY** p)
{size_t i;

if( p==0 || (*p)==0 ) return ;
for(i=0; i < (*p)->nrows; i++)
free((*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}
 
G

Giuseppe

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

#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);

for(h=0; h<50; ++h)
{unsigned len=rand()%20+1;
if(creaARRAY(&r, len, len) * creaARRAY(&a, len, len)*
creaARRAY(&b, len, len)!= 0 )
{for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
a->a[j]= rand()%100;
for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
b->a[j]= rand()%100;

if(somma(r, a, b))
{stampa(a); printf("+ \n");
stampa(b); printf("==\n");
stampa(r);
}
}
if(rszARRAY(&r, NROWS+100, NCOLUMNS+100))
{(*r).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*r).a[255+100][255+100]);
}
if(rszARRAY(&a, 3, 3))
{(*a).a[2][2] = 'c';
printf("my.array1[2][2] = \'%c\'\n", (*a).a[2][2]);
}
if(rszARRAY(&a, NROWS+100, NCOLUMNS+100))
{(*a).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*a).a[255+100][255+100]);
}
if(rszARRAY(&a, 1, 1))
{(*a).a[0][0] = 'c';
printf("my.array1[0][0] = \'%c\'\n", (*a).a[0][0]);
}
if(rszARRAY(&a, 1, 1))
(*a).a[0][0] = 'c';
//if(rszARRAY(&a, 0, 1));
//if(rszARRAY(&a, 1, 0));
//if(rszARRAY(&a, 0, 0)); non funzionano
if(rszARRAY(&a, 100, 199))
(*a).a[99][99] = 'c';
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
return 0;
}

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

if(!a) return ;
for( i=0; i< a->nrows; ++i )
{printf("\n");
for( j=0; j< a->nco ; ++j)
printf("%3u ", (unsigned) a->a[j]);
}
printf("\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)
return 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
return 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
return 0;
for( i=0; i< r->nrows; ++i)
for( j=0; j< r->nco; ++j)
{h = a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
return 0;
r->a[j]=h;
}
return r;
}

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

if(p==0) return 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
return 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;}
return 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
{for( --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;}
return 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return 1;
}

/* rszARRAY()*/
/* Da usarsi a matrice già creata con creaARRAY(). Se riesce a */
/* fare il resize ritorna 1 altrimenti 0 ed **p={0, 0, 0} */
/* *da liberare subito dopo* con l'opportuno freeARRAY */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{size_t i;
unsigned char **temp, *tmp;

if( p==0 || *p==0 )
return 0;
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
{i = nrows<(*p)->nrows ? nrows: (*p)->nrows ;
goto label;
}
(*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= nrows < (*p)->nrows ? nrows :
(i>=(*p)->nrows ? i: (*p)->nrows);
label:
if(i)
{for(--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;
return 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
return 1;
}

/* matrice parzialmente dinamica */
void freeARRAY(ARRAY* p)
{size_t i;

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

/* matrice totalmente dinamica */
void freeARRAY_d(ARRAY** p)
{size_t i;

if( p==0 || (*p)==0 ) return ;
for(i=0; i < (*p)->nrows; i++)
free((*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}
 
A

Al Bowers

Giuseppe said:
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );

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 */
return i; /* Ritorna il numero di colonne allocate; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.


I have seen it in my last message and
i= i>=(*p)->nrows ? i: (*p)->nrows; is right?


No.
Take for example that you use the function to allocated an array,
arr[256][256].
Then you come back with this function a reallocate the array,
arr[4][256].
The statement: temp= realloc( (*p)->a, nrows * sizeof *temp )
will reduce the allocation(assuming it is successful), but at
this point you have lost the pointers arr[4],arr[5],.....arr[255]



yes, and it could be easy free them in this way
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );


That will solve the memory leak problem.
but the game could be too much difficult for me.
(but it seems to work ok)

It may work for you but not me. Consider the result should you
have a deallocation/allocation failure. For example,
you resize an old array, arr[256][4], to arr[3][256].
I am less concerned about the deallocation of the rows from 256 to
3 as the allocations of the columns from 4 to 256. There is
the possiblity of allocation failure. It this occurs, the function
aborts allocation of the new array and the old array is deallocated.
So the result is now you have nothing. I would prefer that this
function preserve the integrity of the old array should allocation
of the new array fail.
 
G

Giuseppe

Giuseppe said:
Giuseppe wrote:
Giuseppe wrote:
/* 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( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );

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 */
return i; /* Ritorna il numero di colonne allocate; */
}


Same problem as before. You have not considered what whould happen
if you use this function to decrease nrow from a previous allocation.
A hint; lost pointers to previous allocations, ie. memory leak.


I have seen it in my last message and
i= i>=(*p)->nrows ? i: (*p)->nrows; is right?

No.
Take for example that you use the function to allocated an array,
arr[256][256].
Then you come back with this function a reallocate the array,
arr[4][256].
The statement: temp= realloc( (*p)->a, nrows * sizeof *temp )
will reduce the allocation(assuming it is successful), but at
this point you have lost the pointers arr[4],arr[5],.....arr[255]



yes, and it could be easy free them in this way
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );


That will solve the memory leak problem.
but the game could be too much difficult for me.
(but it seems to work ok)

It may work for you but not me. Consider the result should you
have a deallocation/allocation failure. For example,
you resize an old array, arr[256][4], to arr[3][256].
I am less concerned about the deallocation of the rows from 256 to
3 as the allocations of the columns from 4 to 256. There is
the possiblity of allocation failure. It this occurs, the function
aborts allocation of the new array and the old array is deallocated.
So the result is now you have nothing. I would prefer that this
function preserve the integrity of the old array should allocation
of the new array fail.


So this could be better?
__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NROWS 256
#define NCOLUMNS 256

int mai=0;

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

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, len;
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;

srand((unsigned)time(0));
if(rszARRAY(&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);

for(h=0; h<10; ++h)
{len=rand()%20+1;
if(rszARRAY(&r, len, len) * rszARRAY(&a, len, len) *
rszARRAY(&b, len, len)== 0 )
goto lab;

for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
a->a[j]= rand()%100;
for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
b->a[j]= rand()%100;

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

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

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

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

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

if(rszARRAY(&a, 1, 1))
(*a).a[0][0] = 'c';

printf("\n");
mai=1;
if(rszARRAY(&a, 1, 2000000000))
(*a).a[0][0] = 'c';
else printf(" c allocazione fallita\n");
printf("DOPO\n");
mai=1;
if(rszARRAY(&a, -1, 1))
(*a).a[0][0] = 'c';
else printf("r allocazione fallita\n");
if(rszARRAY(&a, -1, 1))
(*a).a[0][0] = 'c';
else printf("r allocazione fallita\n");
mai=1;
if(rszARRAY(&a, 1, 2000000000))
(*a).a[0][0] = 'c';
else printf(" c allocazione fallita\n");
printf("DOPO\n");
if(rszARRAY(&a, 1, 2000000000))
(*a).a[0][0] = 'c';
else printf(" c allocazione fallita\n");
printf("DOPO\n");

if(rszARRAY(&a, 100, 199))
(*a).a[99][99] = 'c';
else printf("allocazione fallita\n");

if(rszARRAY(&a, 1, 2000000000))
(*a).a[0][0] = 'c';
else printf(" c allocazione fallita\n");
lab:
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
return 0;
}

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

if(!a) return ;
for( i=0; i< a->nrows; ++i )
{printf("\n");
for( j=0; j< a->nco ; ++j)
printf("%3u ", (unsigned) a->a[j]);
}
printf("\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)
return 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
return 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
return 0;
for( i=0; i< r->nrows; ++i)
for( j=0; j< r->nco; ++j)
{h = a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
return 0;
r->a[j]=h;
}
return r;
}

void freeARRAY(ARRAY* p)
{size_t i;

if( p==0 ) return ;
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 ) return ;
for(i=0; i < (*p)->nrows; i++)
free((*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}


/* rszARRAY()*/
/* Serve per inizializzare la matrice e per fare resize. */
/* Se riesce a fare il resize ritorna 1 altrimenti ritorna */
/* 0 ed **p non viene toccato (rimane allocato se allocato).*/
/* Deve essere nrows>0 e nco>0, mentre **p=&y ove ARRAY *y=0*/
/* (allocazione totalmente dinamica);oppure ARRAY my={0,0,0}*/
/* e ARRAY *y=&my (allocazione parzialmente dinamica) */
/* oppure y già allocato con rszARRAY() prima. */

int rszARRAY(ARRAY** p, size_t nrows, size_t nco)
{size_t i, j, u, v;
unsigned char **a;

if( p==0 ) return 0;

if( ( a=malloc( nrows * sizeof *a) )== NULL )
return 0;
for(i=0; i<nrows; ++i)
{a= malloc(nco * sizeof **a);
if(a==0)
{
label:
if(i)
{for(--i; i!=0; --i)
free( a );
free(a[0]);
}
free(a);
return 0;
}
}

if(*p==0)
{if(( *p=malloc(sizeof **p) )==0 )
goto label;
(*p)->a = a ;
(*p)->nrows = nrows;
(*p)->nco = nco ;
return 1;
}
/* Why we have to copy? */
u= nrows <= (*p)->nrows ? nrows : (*p)->nrows;
v= nco <= (*p)->nco ? nco : (*p)->nco ;

for(i=0; i<u; ++i )
for(j=0; j<v; ++j)
a[j]= (*p)->a[j];
for(i=u; i<nrows; ++i )
for(j=v; j<nco; ++j )
a[j]= 0;
/* end question */

freeARRAY(*p);
(*p)->a = a ;
(*p)->nrows = nrows;
(*p)->nco = nco ; /* nuove righe e colonne */
return 1;
}
 
D

Default User

Mark McIntyre wrote:
ps there's no such word as evidentially,... :)


One entry found for evidential.
Main Entry: ev·i·den·tial
Pronunciation: "e-v&-'den(t)-sh&l
Function: adjective
Date: 1641
: EVIDENTIARY 1
- ev·i·den·tial·ly adverb



Brian Rodenborn
 
D

Default User

Mark said:
In an american dictionary. And it doesn't mean what you're using it to
mean anyway.... Sheesh, americans...:)


Yeah right. Rule number one for usenet, never correct someone's grammar
or spelling because it isn't polite. Rule number two, really don't do it
if you don't know what you are talking about.




Brian Rodenborn
 
M

Mark McIntyre

That type of face saving attempt,

Who's saving face? It doesn't mean what he meant it to mean, if you
see what I mean :)
is known as the "No True Scotsman" fallacy.

are you spillin' my pint, or chewin' a brick, laddie? Oootside, noo,
youse. And yer mammy better hae her sowin kit handy fer when ye gets
hame...
:)
 
P

Programmer Dude

(Sheeze,... only in clc.... )

Mark said:
Incorrect.

It doesn't mean what he meant it to mean, if you see what I mean

Incorrect. It means exactly what I meant it to mean.
 
A

Alan Balmer

(Sheeze,... only in clc.... )



Incorrect. It means exactly what I meant it to mean.
Since your usage of the word doesn't seem to correspond with the
dictionary meaning, maybe you could share what you mean it to mean.

Originally, I thought you had mis-typed "evidently", but apparently
you had something else in mind.
 
A

Arthur J. O'Dwyer

Incorrect. It means exactly what I meant it to mean.

"When I use a word," Humpty Dumpty said, in rather a scornful tone,
"it means just what I choose it to mean - neither more nor less."
"The question is," said Alice, "whether you can make words mean so many
different things."
"The question is," said Humpty Dumpty, "which is to be master - that's
all."
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top