Malloc + 2d arrays + C

Athanasios Karapantelakis karapant at csd.uoc.gr
Tue Mar 19 16:00:01 EET 2002


----- Original Message -----
From: "Giorgos Keramidas" <keramida at ceid.upatras.gr>
To: "Ioannis Sidiropoulos" <sidiropoylosi at yahoo.gr>
Cc: <linux-greek-users at lists.hellug.gr>
Sent: Tuesday, March 19, 2002 2:51 PM
Subject: Re: Malloc + 2d arrays + C


> On 2002-03-19 11:04, Ioannis Sidiropoulos wrote:
> >
> > Prospa8w na grapsw ena (to prwto mou) programma pou exei na kanei me
> > images. Exw loipon mia synarthsh pou pairnei enan pointer se 2d
> > array (to image) kai to epeksergazetai. Mesa s'ayth thn synarthsh kanw
> > malloc gia enan temp 2d array wste na ginei olh h epksergasia s'ayton
kai
> > prin epistrepsw ap'thn synarthsh na ton kanw copy sto actual image
(ksexasa
> > na pw oti einai char arrays):
> >
> > void process(int size, char **image) {
> >   int arraySize = 2*size+1; //arraySize = to mhkos ths pleyras toy#
> >                             // TETRAGWNOU array
>
> Se C h se C++ grafeis?  Ti einai to // ...?
> H C den exei sxolia se C++ stul.
>
Opa. H ANSI C den exei provlima me sxoleia pou arxizoun me "//"
>
> Giati kaneis ton `periergo' pollaplasiasmo me to 2 kai prostheteis 1?
> To `size' einai telika, h den einai to megethos tou pinaka?
>
> >   char **tmp = malloc(arraySize*sizeof(char *));
> >
> >   for (l;l<arraySize;l++) {
>
> Duo lathi:
> - undeclared variable `l'.
> - uninitialized variable `l' used as a loop sentinel.
>
> Ah, epishs.  Grafeis me athlio stul.
> Whitespace.  Use whitespace.  It's free and available in large quantities.
> To parapano einai POLU pio euanagnosto an to grapseis san:
>
> for (x = 0; x < arraySize; x++) {
> ...
> }
>
> >     tmp[i] = malloc(arraySize*sizeof(char));
> >  }
>
> Edo exeis kataferei kai ekanes allocate ena array megethous P x P,
> opou P = 2 * size + 1.  Akoma den katalabaino giati o periergos
> pollaplasiasmos me to 2 kai to + 1, alla telospantwn.
>
> >  // edw ginetai to processing ston tmp
> >  // kai twra 8a ton kanw copy ston image
> >  // kai 8a eley8erwsw th mnhmh
> >
> >  memmove (image, tmp, arraySize);
>
> Oriste?  Posa bytes einai auto?
> Koita ti leei to manpage ths memmove() edo pera...
>
> SYNOPSIS
>      #include <string.h>
>
>      void *
>      memmove(void *dst, const void *src, size_t len);
>
> DESCRIPTION
>      The memmove() function copies len bytes from string src to
>      string dst.  The two strings may overlap; the copy is always
>      done in a non-destructive manner.
>
> Diabase prosektika... "The memmove() function copies len bytes".
> Esena poios sou eipe oti ena array me X entries apo `char *' einai X
bytes?
>
> hades:~$ cat > sizeof.c
> #include <stdio.h>
>
> int main (void)
> {
> printf("%d\n", sizeof(char *));
> return 0;
> }
> hades:~$ cc -Wall sizeof.c
> hades:~$ ./a.out
> 4
>
> >  free(tmp);
>
> Kalh fash.  Ekanes free ton `tmp' pinaka, opote kapoios mporei na parei th
> mnhmh pou palia htan o `tmp' sto akribos epomeno malloc(), efoson einai
> pleon free memory :)  Mono pou ksexases pos den ekanes mono:
>
> tmp = malloc(...);
>
> alla ekanes kai ena malloc() gia KATHE stoixeio tou tmp array.  Pisteuo
pos
> mallon tha exeis ena mikro memory leak.
>
> > }
> >
> > Parolo pou kanei compile to programma me petaei Segm. Fault
> > otan to trexw, ti exw kanei la8os???
>
> Xoris na doume olo to programma?  Sobarologeis tora?
> Ki ego pou ksero ti sto gero-diaolo kaneis ekei pou egrapses:
>
> >  // edw ginetai to processing ston tmp
> >  // kai twra 8a ton kanw copy ston image
> >  // kai 8a eley8erwsw th mnhmh
>
> Show us the code, darling.  All of it...
>
> Giorgos Keramidas                       FreeBSD Documentation Project
> keramida@{freebsd.org,ceid.upatras.gr}  http://www.FreeBSD.org/docproj/
>
> --
> linux-greek-users mailing list -- http://lists.hellug.gr
>




More information about the Linux-greek-users mailing list