Malloc + 2d arrays + C

Άγγελος Οικονομόπουλος lydwigvernon at yahoo.co.uk
Tue Mar 19 14:50:01 EET 2002


On Tuesday 19 March 2002 13:04, Ioannis Sidiropoulos wrote:
> Xairetw!
>
> 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):
>

int mallocs=0, frees=0;
> void process(int size, char **image) {
>
>   int arraySize = 2*size+1; //arraySize = to mhkos ths pleyras toy#
/* asxeto: to size=(arraySize-1)/2 ti einai? */
>
>                             // TETRAGWNOU array
>
>   char **tmp = malloc(arraySize*sizeof(char *));

mallocs++;

>
>   for (l;l<arraySize;l++) {
>
>     tmp[i] = malloc(arraySize*sizeof(char))
>
>   }

mallocs += arraySize; /* toulaxiston auto fantazomai pws h8eles na grapseis */

>
>   // edw ginetai to processing ston tmp
>
>   // kai twra 8a ton kanw copy ston image
>
>   // kai 8a eley8erwsw th mnhmh
>
> memmove (image, tmp, arraySize);
>
> free(tmp);
frees++;

if(mallocs>frees)
	goto memory_leak;

Giati petaei segv? Steile kai ton upoloipo kwdika.
>
> }
>
> Parolo pou kanei compile to programma me petaei Segm. Fault
>
> otan to trexw, ti exw kanei la8os???
>
> TIA
>
> Giannhs
>
>
>
> ---------------------------------
> Do You Yahoo!?
> Αποκτήστε την δωρεάν σας@yahoo.gr διεύθυνση  στο Yahoo! Mail.



More information about the Linux-greek-users mailing list