Malloc + 2d arrays + C
cris
stud1157 at di.uoa.gr
Tue Mar 19 19:52:01 EET 2002
On Tuesday 19 March 2002 14:51, Giorgos Keramidas wrote:
> 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.
>
> 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 *));
> >
Xwris na isxurizomai oti gnwrizw C auto pou mou kanei entupwsi einai
i parapanw grammi
to 'sizeof(char *) ' ti timi akribws exei????
> > 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/
More information about the Linux-greek-users
mailing list