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

Discussion in 'C Programming' started by M-One, Jul 6, 2003.

  1. M-One

    M-One Guest

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

    TIA!
     
    M-One, Jul 6, 2003
    #1
    1. Advertising

  2. M-One

    Al Bowers Guest

    Al Bowers, Jul 6, 2003
    #2
    1. Advertising

  3. M-One

    Mike Wahler Guest

    M-One <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Jul 6, 2003
    #3
  4. M-One

    M-One Guest

    >> 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?


    Yes; thanks for the example.
     
    M-One, Jul 6, 2003
    #4
  5. M-One

    M-One Guest

    M-One, Jul 6, 2003
    #5
  6. M-One

    Mike Wahler Guest

    Re: [OT] Calloc of "unsigned char my_bytes[256][256]"

    Al Bowers <> wrote in message
    news:ygMNa.4653$...

    > Al Bowers
    > Tampa, Fl USA


    Hey Al, how does Lou's hair look? :)

    -Mike (Seattle, WA)
     
    Mike Wahler, Jul 7, 2003
    #6
  7. M-One

    Giuseppe Guest

    On Sun, 06 Jul 2003 14:26:30 GMT, Al Bowers <>
    wrote:
    >M-One wrote:
    >> > Question 6.16 discuss this topic.
    >> > http://www.eskimo.com/~scs/C-faq/q6.16.html

    [...]
    #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);
    }
     
    Giuseppe, Jul 7, 2003
    #7
  8. w (Giuseppe) wrote (07 Jul 2003) in
    news: / 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.




    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America
     
    Martin Ambuhl, Jul 7, 2003
    #8
  9. On Mon, 07 Jul 2003 16:17:23 GMT, in comp.lang.c ,
    w (Giuseppe) wrote:

    >On Sun, 06 Jul 2003 14:26:30 GMT, Al Bowers <>
    >wrote:
    >>M-One wrote:
    >>> > Question 6.16 discuss this topic.
    >>> > http://www.eskimo.com/~scs/C-faq/q6.16.html

    >[...]
    >#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.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jul 7, 2003
    #9
  10. M-One

    Randy Howard Guest

    In article <>, w
    says...
    > #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*

    --
    Randy Howard
    remove the obvious bits from my address to reply.
     
    Randy Howard, Jul 7, 2003
    #10
  11. M-One

    Zoran Cutura Guest

    Giuseppe <> wrote:
    > On Sun, 06 Jul 2003 14:26:30 GMT, Al Bowers <>
    > wrote:
    >>M-One wrote:
    >>> > Question 6.16 discuss this topic.
    >>> > http://www.eskimo.com/~scs/C-faq/q6.16.html

    > [...]


    > #define R return


    > return 0;


    What kind of game are you playing? "How inconsiquent can you get?"
    You're close to win the prize.
    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Jul 8, 2003
    #11
  12. M-One

    Giuseppe Guest

    #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);
    }
     
    Giuseppe, Jul 8, 2003
    #12
  13. M-One

    Giuseppe Guest

    On Tue, 08 Jul 2003 07:49:02 GMT, w (Giuseppe)
    wrote:
    >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; }
    > 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 ); *p=0; }
    > R 0;
    > }
    > }
    > (*p)->nrows = nrows ;
    > (*p)->nco = ncolumns;
    > return i; /* Ritorna il numero di colonne allocate; */
    >}
     
    Giuseppe, Jul 8, 2003
    #13
  14. M-One

    pete Guest

    Giuseppe wrote:
    >
    > #include <stdio.h>
    > #include <stdlib.h>


    Your program crashes on my machine.

    --
    pete
     
    pete, Jul 8, 2003
    #14
  15. M-One

    Giuseppe Guest

    #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'
     
    Giuseppe, Jul 8, 2003
    #15
  16. M-One

    Richard Bos Guest

    pete <> wrote:

    > Giuseppe wrote:
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>

    >
    > Your program crashes on my machine.


    You're surprised? ;-)

    Richard
     
    Richard Bos, Jul 8, 2003
    #16
  17. M-One

    Tom St Denis Guest

    Giuseppe wrote:
    > #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
     
    Tom St Denis, Jul 8, 2003
    #17
  18. M-One

    Giuseppe Guest

    On Tue, 08 Jul 2003 12:45:23 GMT, w (Giuseppe)
    wrote:

    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
     
    Giuseppe, Jul 8, 2003
    #18
  19. M-One

    Giuseppe Guest

    #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à"
     
    Giuseppe, Jul 8, 2003
    #19
  20. M-One

    Giuseppe Guest

    #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.
     
    Giuseppe, Jul 8, 2003
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Tarasevich
    Replies:
    0
    Views:
    476
    Andrey Tarasevich
    Sep 2, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    497
    Kevin Goodsell
    Sep 2, 2003
  3. luca
    Replies:
    5
    Views:
    27,041
    Default User
    Mar 2, 2004
  4. JKop

    calloc.... Why?

    JKop, Jul 18, 2004, in forum: C++
    Replies:
    14
    Views:
    790
    tom_usenet
    Jul 20, 2004
  5. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,084
    Jun Woong
    Jun 26, 2003
Loading...

Share This Page