c - How to clean the Array after write some information -


i first declare pointer array

int q,k; char ***scharray; scharray = malloc(sizeof(char**)*1000);     for(q = 0; q < 1000; q++) {         scharray[q] = malloc(sizeof(char*)*100);         for(k = 0; k < 1000; k++) {             scharray[q][k] = malloc(sizeof(char)*100);         }     }  char buf[80]={0}; 

then read data pipe put in buf , copy pointer array

strcpy(scharray[commandnum][0], buf);  

after that, want clean content of pointer array. use following code.

scharray[commandnum][0]=null; 

after that, cannot write data scharray[commandnum][0] code

strcpy(scharray[commandnum][0], buf); 

is there mistake in code above?

i think want set first character of allocated string '\0'. you'd using

scharray[commandnum][0][0] = '\0'; 

your current code replaces (and leaks) pointer buffer allocated malloc(sizeof(char)*100)

as aside, , before pulls me on it, note sizeof(char) guaranteed 1 malloc can simplified malloc(100).


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -