简体   繁体   English

变量值测试的分段错误

[英]segmentation fault on a test of value of variable

I don't understand why there is a segfault on : while (((*buf)!= ' ')) I would to store a content of a an array in N others array's (N number of thread), but when I store the content of buff I won't to cut a word.我不明白为什么有段错误: while (((*buf)!= ' ')) 我想将一个数组的内容存储在 N 个其他数组的(N 个线程)中,但是当我存储buff的内容我就不多说了。 I don't understand why there is a segmentation fault when I test the value of *buf??不明白为什么我测试*buf的值时会出现segmentation fault??

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

#include <ctype.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#define MAX_WORD_LENGHT 10

void *count_func(void *arg)
{

}
    int main (int argc, char **argv){

        pthread_t *tids;
        char * buf, * save;
        ssize_t cpt;
        int i, j, fd, count, size, decoup;

        if (argc <2 )
        {   
            printf("\n usage : %s [nom_du_fichier] \n ", argv[0]);
            exit(1);
        }

        fd = open(argv[1],O_RDONLY);

         //taille du fichier
        size = lseek (fd,0,SEEK_END);
        printf("\ntaille du fichier : %d octets\n",size);
        lseek(fd,0,SEEK_SET); // repositionement du pointeur au début du fichier.

        count = sysconf(_SC_NPROCESSORS_ONLN);
        count = (count < 1) ? 1 : count;
        char *tabs[count];

        buf =(char *)malloc(size);
        while ((cpt=read(fd,buf, size)) > 0){
            printf("lect en cours \n");
        }
        save=buf;
        while((*buf)!= '\0'){
        printf("carc: %c \n", *buf);
        buf++;
        }

        // répartion des données de buf sur N sous tableau: N correspandant aux nbr
        // de threads
        for (i=0; i< count; i++){
            tabs[i]= (char *) malloc ((size/count)+ MAX_WORD_LENGHT);
        }  
       buf= save; // on sauvgarde l'adresse du début du tableau.
        for (i=0; i<count; i++){
            for (j=0; j<=(size/count); j++, (*buf)!= '\0'){
                    if (j != (size/count)){
                       printf("j:%d \t", j);tabs[i][j] = *buf;        
                       buf++;
                    }else {
                    if (isspace(*buf)){
                        printf("cond 1\n");
                        break;
                    }else{
                       while (((*buf)!= ' ')){ tabs[i][j]= *buf; buf++; j++;}
                        printf("cont: %c \t ", *buf);
                        printf("cond 2\n");
                        break; 
                        }
                    }
                }
            }


        for (i=0; i< count; i++){
            for (j=0; j< (size/count)+1; j++){
            printf("value of tabs[%d][%d] is : %c \n", i, j, tabs[i][j]);
            }
        printf("tabs: %d done \n", i);
        }

        decoup = (size-1)/count;
        tids = (pthread_t *)malloc (sizeof(pthread_t) * count);

        if(tids == NULL)
        {
            printf("main: failed to allocate tids \n");
            exit(-1);
        }
        close(fd);
        buf=save;
        free(buf);
       // free(tabs[0]); free(tabs[1]);free(tabs[2]);free(tabs[3]);
       // free(tids);
        return 0;
    }

    // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4

This will go beyond the null terminating character of buf unless it finds a space character before.这将超出buf的空终止字符,除非它之前找到一个空格字符。

while (((*buf) != ' '))

Modify to this修改成这个

while (*buf && *buf != ' ')

If there is no null-terminating char in the buf or if there is but you want to read beyond that then check that you didn't go beyond the size of the buffer.如果 buf 中没有空终止字符,或者如果有但您想读取更多内容,请检查您是否没有超出缓冲区的大小。 Eg例如

while (j < size && *buf != ' ')

Also, lseek is not an efficient way to get the size of a file to allocate memory, as you have to go from the start of the file to the end and back to the start again to be able to read.此外, lseek不是获取文件大小以分配内存的有效方法,因为您必须从文件的开头到结尾再回到开头才能读取。 Have a look at fstat .看看fstat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM