分段错误(核心转储)用C字节读写器 [英] Segmentation fault (core dumped) in C byte reader

查看:189
本文介绍了分段错误(核心转储)用C字节读写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的介绍对系统操作类,它具有两个不同的部分,首先是一个简单的程序来读取可执行文件,逐字节,并输出至少4个字符长进入字符串赋值。这是串程序(命令),你可以在UNIX中使用的一个简单的模型。

I have an assignment for my intro to system operations class that has two distinct parts, the first being a simple program to read an executable, byte by byte, and output the strings entered that are at least 4 characters long. It is a simple modeling of the strings program (command) you can use in UNIX.

我有一个分段错误(核心转储)错误,我送入这三个独立的样本可执行文件。我明白,这基本上意味着我试图访问我没有访问(通过大言不惭,该计划拥有分配的内存块,或通过其他方式)一些内存地址。不幸的是,我不明白为什么这个项目在做。

I'm having a segmentation fault (core dumped) error for three separate sample executables that I feed into it. I understand that this essentially means I'm trying to access some memory address that I do not have access to (either by overreaching the allocated block that the program owns, or by some other means). Unfortunately, I don't understand why this program is doing it.

我认为问题出在我的链表实现 - 我用它来存储可读的字符,然后检查是否链表中有4个条目时,非可读取的作物了。如果这样做,我打印。然后我清除链接列表并重新开始。

I think the problem lies in the my linked list implementation - I use it to store the characters that are readable, and then check to see if the linked list has 4 entries when a non-readable character crops up. If it does, I print it. Then I clear the linked list and start again.

我穿越由字节文件的字节,我觉得这个方案的逻辑是健全的。然而,我的指针,地址和malloc的完全理解并非作为声音。我有一种预感段错误就是因为这个知识缺乏的发生。

I'm traversing the file byte by byte, and I feel that the logic of this program is sound. However, my complete understanding of pointers, addresses, and malloc is not as sound. I have a hunch the segmentation fault is occurring because of this lack of knowledge.

有人能看看下面的code,并找出我做错了什么?最重要的是,你能解释一下我误用什么概念,为什么?我担心程序运作的方式应该是的,又担心我缺乏了解。在code低于 - 谢谢

Could someone look at the code below, and find out what I'm doing wrong? Most importantly, could you explain what concept I am misusing, and why? I'm worried about the program functioning the way it should, yes, but also worried about my lack of understanding. The code is below - thank you.

#include <stdio.h>
#include <stdlib.h>

struct node{
    char ANSII;
    struct node *next_node;
};

void clear_list(struct node *first_node);
void print(struct node *first_node);
int counter(struct node *first_node);
void append(char temp, struct node *first_node);


int main(int argc, char **argv){
    FILE *f = NULL;
    struct node header;
    char temp;

    if(argc != 2){ /* argv[0] = name of the program, argv[1] = file to open */
        printf("usage: %s filename:", argv[0]);
    }

    f = fopen(argv[1], "rb");

    if(f == 0){ /* check for successful read */
         printf("Could not open file.\n");
    }

    while(!feof(f)){
        fread(&temp, sizeof(1), 1, f);
        if(temp >= 32 && temp <= 128){ /* If it falls between the bounds of printable     characters. */
            append(temp, &header); //Builds the string
        }else{
            if(counter(&header) > 3){
                print(&header);
            }
            clear_list(&header);
        }
    }
    return 0;
}
void clear_list(struct node *first_node){
    struct node *conductor;
    while(first_node != NULL){
        conductor = first_node;
        while(conductor->next_node != NULL){
            conductor = conductor->next_node;
        }
        free(conductor);
    }
}
void print(struct node *first_node){
    struct node *conductor = first_node;
    while(conductor != 0){
        printf("%s", conductor->ANSII);
        conductor = conductor->next_node;
    }
    printf("\n");
}
int counter(struct node *first_node){
    struct node *conductor = first_node;
    int counter = 0;
    while(conductor != 0){
        conductor = conductor->next_node;
        counter++;
    }
    return counter;
}
void append(char temp, struct node *first_node){
    struct node *conductor = first_node;
    while(conductor->next_node != 0){
        conductor = conductor->next_node;
    }
    conductor->next_node = malloc(sizeof(conductor->next_node));
    if(conductor->next_node == 0){
        printf("Memory allocation failed!");
        return;
    }
    conductor = conductor->next_node;
    conductor->ANSII = temp;
}

我试过到目前为止实施的答案,现在,而不是分段错误我收到:

I tried implementing the answers so far, and now instead of a segmentation fault I'm getting:

*** glibc detected *** ./mystrings: double free or corruption (fasttop): 0x0000000000601250   ***
======= Backtrace: =========
/lib64/libc.so.6[0x3886a75916]
./mystrings[0x400798]
./mystrings[0x40072f]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x3886a1ecdd]
./mystrings[0x4005b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:1b 1921384528                         /afs/pitt.edu/home/n/a/nap54/private/cs449/project2/mystrings
00600000-00601000 rw-p 00000000 00:1b 1921384528                              /afs/pitt.edu/home/n/a/nap54/private/cs449/project2/mystrings
00601000-00622000 rw-p 00000000 00:00 0                                  [heap]
3886600000-3886620000 r-xp 00000000 fd:00 180                            /lib64/ld-2.12.so
388681f000-3886820000 r--p 0001f000 fd:00 180                            /lib64/ld-2.12.so
3886820000-3886821000 rw-p 00020000 fd:00 180                            /lib64/ld-2.12.so
3886821000-3886822000 rw-p 00000000 00:00 0
3886a00000-3886b89000 r-xp 00000000 fd:00 183                            /lib64/libc-2.12.so
3886b89000-3886d89000 ---p 00189000 fd:00 183                            /lib64/libc-2.12.so
3886d89000-3886d8d000 r--p 00189000 fd:00 183                            /lib64/libc-2.12.so
3886d8d000-3886d8e000 rw-p 0018d000 fd:00 183                            /lib64/libc-   2.12.so
3886d8e000-3886d93000 rw-p 00000000 00:00 0
388d200000-388d216000 r-xp 00000000 fd:00 6639                           /lib64/libgcc_s-4.4.6-20120305.so.1
388d216000-388d415000 ---p 00016000 fd:00 6639                           /lib64/libgcc_s-  4.4.6-20120305.so.1
388d415000-388d416000 rw-p 00015000 fd:00 6639                           /lib64/libgcc_s-    4.4.6-20120305.so.1
7ffff7fd5000-7ffff7fd8000 rw-p 00000000 00:00 0
7ffff7ffb000-7ffff7ffe000 rw-p 00000000 00:00 0
7ffff7ffe000-7ffff7fff000 r-xp 00000000 00:00 0                          [vdso]
7ffffffea000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

现在,我完全丧失。可能有人棚(更多?)的见解?感谢您的帮助家伙...

Now I'm completely lost. Could someone shed (more?) insight? Thanks for your help guys...

推荐答案

在声明变量,你不初始化成员。这意味着那些值将是不确定的和随机的。然后当你调用追加 next_node 成员很可能不是 NULL 引领你解引用一个未定义的指针,具有不确定的行为。

When you declare the variable header, you do not initialize the members. This means that the values of those will be undefined and random. Then when you call append the next_node member is most likely not NULL leading you do dereference an undefined pointer and have undefined behavior.

初​​始化至少 next_node 成员标题结构

Initialize at least the next_node member of the header structure in main.

这篇关于分段错误(核心转储)用C字节读写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆