Friday, 15 January 2010

c - Realloc with matrix and automatization -


I was wondering why a 4 * or whatever reDel works for the 1D array if I used the oldpointer = realloc (

Oldpointer, newsize);

But when I try with a 2D char * array then it fails. Seeing here and there, I think sometimes people use the NewPointer = Rialoch (old indicator, Newsweek), but if this is the only use, then it will not be useful for me, because the size of the matrix columns and rows Change the code used in a loop (I

I have tried it,

  void main () {Int max = 5, i, j; For the four ** matrix; matrix = malloc (max * sizeof (four)); (i = 0; i & lt; h (Matrix, max * 2 * size (four)); strcpy (matrix [4], "we \ 0"; matrix [i] = molok (max * size (four));} matrix = real Error in 'printf' ("% s", matrix [4]);}  

`/. ': Realloc (): invalid next size: 0x00000000015c4010 *** Canceled

The problem is that you can not have your double pointer points because you have not allocated enough space.

  matrix = molk (max * size (four)); / * Which is actually * matrix = miles (max); * /  

must be

  matrix = maulok (max * size (four *)); / * ^ Maximum pointers, then a poitner * /  

is the size, if you want realloc () , then you can do it like this < / P>

  zero * indicator; Pointer = Rialoc (matrix, 2 * max * size (four *)); If (poitner == NULL) handleFailure_OrExit_ButDoNot_Use_The_Realloced_Poitner (); Matrix = pointer;  

Check the return value of the Always function if it returns a return, for example malloc () / Calloc () / realloc () " and normally no custom implementation ", return NULL .


No comments:

Post a Comment