string - C programming - matrix-based function not working -


in program i'm writing, have function, reads input , stores each line in entry of array of characters. n number given in input, , represents number of lines read.

char **linelist(int n) {     int i, j;     char **list;     list = calloc(n, sizeof(char *));     (j = 0; j < n; j++){         list[j] = calloc(max_str, sizeof(char));     }     (i = 0; < n; i++){         fgets(list[i], (max_str), stdin);     }     return list; } 

this function seems working correctly, output of command supposed give input output matches supposed output in tests.

then, have following function, takes array returned aforementioned 1 , further splits lines words, generating two-dimentional matrix, has n lines, corresponding number of lines in input, , therefore, number in list, , arbitrary number of rows, words of each line being store in each entry.

char ***createmat(int n, char **list) {     char ***matrix;     int i, x, j, k, l, y, z;     char *buffer;     const char separators[] = {' ','\t',',',';','.','?','!','"','\n'};     printf("f0\n");     buffer = calloc(max_str, sizeof(char));     y = 0;      matrix = calloc(n, sizeof(char**));     for(z = 0; z < n; z++) {         matrix[z] = calloc(n, sizeof(char*)); // line 100         for(i = 0; < n; i++) {             matrix[z][i] = calloc(max_str, sizeof(char));         }     }     printf("f1\n");     for(x = 0; x < n; x++){         for(j = 0, l = 0; list[x][j] != '\0'; j++, l++){             buffer[l] = tolower(list[x][j]);             for(k = 0; k < 9; k++){                 if(list[x][j] == separators[k]){                     if(l != 0){                         buffer[l] = '\0';                         printf("f2\n");                         stringcpy(buffer,matrix[x][y++]);  //line 114                         printf("f3\n");                     }                     l = -1;                 }             }         }         matrix[x][y+1] = "\n";     }     printf("f4\n");     return matrix; } 

this function, starts allocating memory matrix, process raises no errors in valgrind. problem seems come part that's supposed separate , store words. based on tests, seems work when each line has 1 word, doesn't when line has multiple words. printf statements put in debugging purposes , running following input in terminal:

3  //what functions receive int n - number of lines sky pink , yellow , not uncute 

gives following output:

f0 f1 f2 f3 f2 f3 f2 f3 f2 segmentation fault (core dumped) 

as running in valgrind returns following:

==7599== invalid read of size 8 ==7599==    @ 0x400d01: createmat (proj.c:114) ==7599==    0x4015a7: main (proj.c:241) ==7599==  address 0x5201428 0 bytes after block of size 72 alloc'd ==7599==    @ 0x4c2abb4: calloc (vg_replace_malloc.c:593) ==7599==    0x400bbc: createmat (proj.c:100) ==7599==    0x4015a7: main (proj.c:241) ==7599==  ==7599== invalid read of size 1 ==7599==    @ 0x40087d: stringcpy (proj.c:12) ==7599==    0x400d16: createmat (proj.c:114) ==7599==    0x4015a7: main (proj.c:241) ==7599==  address 0x0 not stack'd, malloc'd or (recently) free'd 

and stringcpyfunction following:

void stringcpy(char *s, char *t) {     while ((*s++ = *t++) != '\0'); //line 12 } 

i've been trying figure out what's wrong forever, no avail, being have few experience in c. appreciated. thank you.

one possible problem variable y never appears reset zero. appears if should reset 0 when reaching end of line.

one other possibility if there more n words in given line, result in writing past end of allocated memory.

edit based on addition of source stringcpy, appears have parameters backwards in call. think should be:

stringcpy(matrix[x][y++],buffer);  //line 114 

note following line causes memory leak (the assignment of constant string "\n" causes pointer allocated memory in spot lost:

    matrix[x][y+1] = "\n"; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -