访问char数组外部函数但是在内部时没有分段错误

时间:2013-06-24 10:12:55

标签: c segmentation-fault readfile

所以基本上就是代码生成我的segmentation fault

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

    #define CORRUPT_KEY_FILE    "s64581-source-key-corrupt.bin" /* ContainsKey and IV */

    #define DEBUG               1

    /**
     * Reads content of a given file and writes it into a buffer
     * 
     * @param filename Path of the file to be read
     * @param buffer Pointer to the buffer to write into
     * @param length Variable to store the filelength into
     * 
     * @return EXIT_FAILURE if an error occurred opening th File, otherwise EXIT_SUCCESS
     */
    int readFiletoBuffer(const char* filename, unsigned char* buffer, int* length) {

        int i;

        if(DEBUG) printf("\n\nFunction readFileToBuffer(..) called\n");

        FILE *file = fopen(filename, "rb");
        if(!file) {
            printf("Error opening file %s", filename);
            return EXIT_FAILURE;
        }

        fseek(file, 0 , SEEK_END);
        *length = ftell(file);
        rewind(file);

        if(DEBUG) printf("\tLength of File %s: %d\n",filename ,*length);

        buffer = (unsigned char*) malloc(*length+1);

        fread(buffer, *length, 1, file);
        fclose(file);

        for(i=0; i<*length; i++) printf("%d:\t%X\n",14, buffer[14]);

        return EXIT_SUCCESS;
    }

    int main(int argc, char **argv)
    {
        unsigned char *tmp_char;
        int tmp_length, i;
        tmp_char=NULL;

        readFiletoBuffer(CORRUPT_KEY_FILE, tmp_char, &tmp_length);

        for(i=0; i<tmp_length; i++) printf("%d:\t%X\n",i, tmp_char[i]);

        return 0;
    }

注意两个for循环打印缓冲区的内容。函数内部的一个工作正常,main - 函数中的一个产生segmentations fault。为什么会这样,如何在函数readFiletoBuffer中分配所需的内存时解决这个问题?

任何帮助表示赞赏! :d

2 个答案:

答案 0 :(得分:1)

问题是,当您使用参数readFiletoBuffer调用函数tmp_char时,将复制此参数!调用此函数后,tmp_char为NULL!

您需要使用指向此参数的指针进行更改,如下所示:

tmp_char=NULL;
readFiletoBuffer(CORRUPT_KEY_FILE, &tmp_char, &tmp_length);

所以你的功能看起来像这样:

int readFiletoBuffer(const char* filename, unsigned char** buffer, int* length)

答案 1 :(得分:1)

从函数中获取所需的char数组,将char **数组作为参数传递。 这样就会返回你的char *指针。

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

#define CORRUPT_KEY_FILE    "s64581-source-key-corrupt.bin" /* ContainsKey and IV */

#define DEBUG               1

/**
 * Reads content of a given file and writes it into a buffer
 * 
 * @param filename Path of the file to be read
 * @param buffer Pointer to the buffer to write into
 * @param length Variable to store the filelength into
 * 
 * @return EXIT_FAILURE if an error occurred opening th File, otherwise EXIT_SUCCESS
 */
int readFiletoBuffer(const char* filename, unsigned char** buffer, int* length) {

    int i;

    if(DEBUG) printf("\n\nFunction readFileToBuffer(..) called\n");

    FILE *file = fopen(filename, "rb");
    if(!file) {
        printf("Error opening file %s", filename);
        return EXIT_FAILURE;
    }

    fseek(file, 0 , SEEK_END);
    *length = ftell(file);
    rewind(file);

    if(DEBUG) printf("\tLength of File %s: %d\n",filename ,*length);

    *buffer = (unsigned char*) malloc(*length+1);

    fread(*buffer, *length, 1, file);
    fclose(file);

    for(i=0; i<*length; i++) printf("%d:\t%X\n",14, buffer[14]);

    return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
    unsigned char *tmp_char;
    int tmp_length, i;
    tmp_char=NULL;

    readFiletoBuffer(CORRUPT_KEY_FILE, &tmp_char, &tmp_length);

    for(i=0; i<tmp_length; i++) printf("%d:\t%X\n",i, tmp_char[i]);

    return 0;
}

这是我的建议。