开发者

getting seg fault on malloc'ing for a string array. please help [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
#include<stdio.h>  
------------------  
#include<stdlib.h>  
--------------------  
#include<string.h      
--------------------  

enum { buf = BUFSIZ };

char line[buf], **tab = NULL;

 int cur_buf, count_lineMax = -1, count_line = -1,
    i, j, k, l;

int main (in开发者_运维技巧t argc, char **argv) {

        FILE *file1;
        file1 = fopen(argv[1], "r");

        cur_buf = buf;

        /*printf("%d\n", cur_buf);*/


                while(fgets(line, cur_buf, file1) != NULL) {

                        i = strlen(line);

                        for(j=0;j<i;j++) {
                                if(line[j] == '\n') {
                                        count_lineMax++;
                                }
                                /*printf("%c", line[j]);*/
                        }

                }
                /*printf("%d\n", count_lineMax);*/

                rewind(file1);

                tab = malloc(count_lineMax);
                memset(tab, 0, count_lineMax);


                for(k=0;k<count_lineMax;k++) {
                        tab[k] = malloc(cur_buf+1);
                        memset(tab[k], 0, cur_buf+1);
                }

                        while(fgets(line, cur_buf, file1) != NULL) {
                                i = strlen(line);

                                for(j=0;j<i;j++) {
                                        if(line[j] == '\n') {
                                                count_line++;
                                        }
                                }
                        tab[count_line] = line;
                        printf("%s", tab[count_line]);
                        }
        return 0;
        for(l=0;l<count_lineMax;l++) {
                free(tab[l]);
        }

        free(tab);
        fclose(file1);
}
## Heading ##


You haven't allocated enough memory for your lines.

char **tab;

tab = malloc(count_lineMax);
memset(tab, 0, count_lineMax);

count_lineMax is the number of lines that are in the file and you are trying to allocate memory to store that many lines. You only allocate count_lineMax bytes of memory but that's not enough for count_lineMax pointers. Those lines should read:

tab = malloc(count_lineMax * sizeof(*tab));
memset(tab, 0, count_lineMax * sizeof(*tab));

After that, you attempt to allocate more memory to store count_lineMax pointers and you overflow your memory corrupting the heap.

Furthermore, the second time you read in your file, you are writing to your file buffer and storing that buffer in your tab array. You're leaking the memory you had previously allocated and you attempt to free() this buffer which is wrong. So any of those problems are causing the segmentation fault.

while(fgets(line, cur_buf, file1) != NULL) { /* read into `line` */
    /* ... */
    tab[count_line] = line; /* DEATH! */
}
/* ... */
for(l = 0; l < count_lineMax; l++) {
    free(tab[l]); /* DOUBLE DEATH! *?
}

You need to either read your lines directly into your allocated buffers correctly:

count_line = 0;
while(fgets(&tab[count_line], cur_buf, file1) != NULL) { /* read into `line` */
    /* ... */
    count_line++;
}

Or copy the contents of the line buffer into your allocated ones. That way you're not leaking your memory and freeing the wrong ones.

count_line = 0;
while(fgets(line, cur_buf, file1) != NULL) { /* read into `line` */
    /* ... */
    strcpy(tab[count_line], line);
    count_line++;
}

There are other issues with the code but I won't go over them as these are higher priority.


Also, you never check that count_line is in range so tab[count_line] could be invalid when you assign to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜