Malloc + 2d arrays + C
Λαμπής Μιχαήλ
mlambis at oneway.gr
Wed Mar 20 13:12:01 EET 2002
Στις Τρι 19 Μαρ 2002 13:04, ο Ioannis Sidiropoulos έγραψε:
> Xairetw!
Geia
> void process(int size, char **image) {
>
> int arraySize = 2*size+1; //arraySize = to mhkos ths pleyras toy#
>
> // TETRAGWNOU array
>
> char **tmp = malloc(arraySize*sizeof(char *));
>
> for (l;l<arraySize;l++) {
>
> tmp[i] = malloc(arraySize*sizeof(char))
>
> }
Mexri edw ok mou fainetai
>
> // edw ginetai to processing ston tmp
>
> // kai twra 8a ton kanw copy ston image
>
> // kai 8a eley8erwsw th mnhmh
>
> memmove (image, tmp, arraySize);
Ayto prepei na omologisw den to katalaba. O pinakas tmp exei megethos
arraySize^2 etsi? Giati prospatheis na antigrapseis mono arraySize stoixeia?
Bebaia outws i allws den nomizw oti mporeis me mia klisi sti memmove na
antigrapseis olon ton tmp giati ta malloc pou exeis kanei den sou
eksasfalizoun synexh kommatia mnhmhs. Dhladh enw se enan kanoniko pinaka pou
exeis dhlwsei p.x. me int a[3][3] ta a[0][2] a[1][0] einai se synexomenes
theseis mnimis (swsta?) enw anamesa sto tmp[0][arraySize-1] tmp[1][0] mporei
na mesolabei otidhpote.
Giati de dokimazeis to:
for(i=0;i<arraySize;i++)
memmove(image[i],tmp[i],arraySize);
dhladh na ton antigrapseis grammi-grammi?
>
Edw pisteyw thes ena:
for(i=0;i<arraySize;i++)
free(tmp[i]);
> free(tmp);
>
> }
>
> Parolo pou kanei compile to programma me petaei Segm. Fault
>
> otan to trexw, ti exw kanei la8os???
Isws kaneis kati lathos sto ypoloipo programma?
Mixalhs Lamphs aka migan
More information about the Linux-greek-users
mailing list