c - 变量值测试中的段错误

标签 c

我不明白为什么出现段错误: 而 (((*buf)!= ' ')) 我想将一个数组的内容存储在N个其他数组(N个线程)中,但是当我存储buff的内容时我不会删掉一个字。 我不明白为什么在测试*buf的值时会出现段错误??

#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

最佳答案

这将超出 buf 的空终止字符,除非它之前找到空格字符。

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

修改为这里

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

如果 buf 中没有空终止字符,或者有但您想读取超出该范围的内容,请检查是否超出了缓冲区的大小。例如

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

此外,lseek 并不是获取文件大小以分配内存的有效方法,因为您必须从文件的开头到结尾,然后再次返回开头才能分配内存。能够阅读。看看fstat .

关于c - 变量值测试中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809416/

相关文章:

c - 在 C 中访问 WINAPI 方法

c - 将图像放置在固定布局 GTK2 中

关于运算符优先级的澄清

c - 字符串文字 : Where do they go?

c - 这个结构/union 声明有什么问题?

c - add = (char *) wed.. 这在 C 中是什么意思?

c - 为什么我在 C 中没有得到 tan(PI/2) = infinity

c - 如何将 UInt64 数组转换为 UInt16 数组以执行多精度乘法?

c - Libev:如何安排尽快调用回调

c - 释放二维数组 - 双重释放或损坏