为什么全局变量在不同方法中表现不同?

aik*_*v95 3 c arrays string variables segmentation-fault

目标:

  1. 给出字符串(字典)的全局数组,只要在函数中可以计算大小load().
  2. 用功能在屏幕上打印字典print().

我的方法:

创建指向字符串的全局指针,创建字符串数组load()并将本地数组指定给全局指针.

问题:

如果我尝试在内部打印全局数组(以及本地数据)load(),一切都很好,但是在打印的情况下print(),segfault会出现在数组末尾的某处.GDB和valgrind输出对我来说似乎很神秘.我放弃.怎么了?

来源和字典在这里.

码:

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

// length of the longest word in dictionary
#define LENGTH 45
// dictionary file
#define DICTIONARY "large"

// prototypes
void load(const char* dictionary);
void print(void);

// global dictionary size
int dict_size = 0;
// global dictionary
char **global_dict;

int main(void)
{
    load(DICTIONARY);
    print();
    return 0;  
}

/**
 * Loads dictionary into memory.
 */
void load(const char* dictionary)
{
    // open dictionary file
    FILE *dict_file = fopen(dictionary, "r");    

    // compute size of dictionary
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // look for '\n' (one '\n' means one word)
        if (c == '\n')
        {
            dict_size++;
        }
    }

    // return to beginning of file
    fseek(dict_file, 0, SEEK_SET);

    // local array
    char *dict[dict_size];

    // variables for reading
    int word_length = 0;
    int dict_index = 0;
    char word[LENGTH + 1];   

    // iteration over characters
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // allow only letters
        if (c != '\n')
        {
            // append character to word
            word[word_length] = c;
            word_length++;
        }
        // if c = \n and some letters're already in the word
        else if (word_length > 0)
        {
            // terminate current word
            word[word_length] = '\0';

            //write word to local dictionary
            dict[dict_index] = malloc(word_length + 1);
            strcpy(dict[dict_index], word);
            dict_index++;

            // prepare for next word
            word_length = 0;
        }
    }

    // make local dictioinary global
    global_dict = dict;
}

/**
 * Prints dictionary.
 */
void print(void)
{
    for (int i = 0; i < dict_size; i++)
        printf("%s %p\n", global_dict[i], global_dict[i]);
}
Run Code Online (Sandbox Code Playgroud)

Iha*_*imi 6

答案很简单,您将指针指向一个本地变量,load()并在load()返回时释放它,因此它无效print(),导致未定义的行为.

你甚至评论过它

// local array <-- this is your comment not mine
char *dict[dict_size];
Run Code Online (Sandbox Code Playgroud)

您有两种选择:

  1. 不要使用全局变量,与全局变量一起使用的模式没有任何好处,相反,它非常危险.您可以从函数返回动态分配的poitner load(),然后将其传递给print().

  2. 使用分配指针数组malloc().

    global_dict = malloc(dict_size * sizeof(*global_dict));
    
    Run Code Online (Sandbox Code Playgroud)

为什么我不喜欢全局变量?

  • 因为您可以在程序中执行您在程序中所做的操作,甚至无需接收编译器的警告.

但是当然,当你获得经验时,你不会做这种事情,所以它更多是你的错,而不是全局变量,但是看到程序员还在学习,使用全局变量来解决共享数据的问题是很常见的.在函数中,这是参数的用途.

所以使用全局变量+不知道如何正确处理它们是不好的,而是学习函数参数,你将解决所有需要全局变量通过程序中的不同函数传递数据而不使用全局变量的问题.

这是你自己的代码,我删除了global_dict变量,并在里面使用了动态内存分配load(),我也执行了一些错误检查malloc(),你应该改进那部分,如果你想要代码是健壮的,其余的是自我解释


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

// length of the longest word in dictionary
#define LENGTH 45
// dictionary file
#define DICTIONARY "large"

// prototypes
char **load(const char *dictionary);
void print(char **);

int main(void)
{
    char **dictionary;

    dictionary = load(DICTIONARY);
    if (dictionary == NULL)
        return -1;
    print(dictionary);

    /* Don't forget to free resources, 
       you might need to do it while
       the program is still running,
       leaked resources might quickly 
       become a problem.
     */
    for (int i = 0 ; dictionary[i] != NULL ; ++i)
        free(dictionary[i]);
    free(dictionary);

    return 0;  
}
/**
 * Loads dictionary into memory.
 */
char **load(const char *dictionary)
{
    // open dictionary file
    FILE  *dict_file;    
    size_t dict_size;
    char **dict;
    char   word[LENGTH + 1];
    size_t word_length;
    size_t dict_index;
    dict_file = fopen(dictionary, "r");
    if (dict_file == NULL) /* you should be able to notify this */
        return NULL; /* failure to open file */
    // compute size of dictionary
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // look for '\n' (one '\n' means one word)
        if (c == '\n')
        {
            dict_size++;
        }
    }
    // return to beginning of file
    fseek(dict_file, 0, SEEK_SET);

    // local array
    dict = malloc((1 + dict_size) * sizeof(*dict));
    /*                    ^ add a sentinel to avoid storing the number of words */
    if (dict == NULL)
        return NULL;

    // variables for reading
    word_length = 0;
    dict_index = 0;
    // iteration over characters
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // allow only letters
        if (c != '\n')
        {
            // append character to word
            word[word_length] = c;
            word_length++;
        }
        // if c = \n and some letters're already in the word
        else if (word_length > 0)
        {
            // terminate current word
            word[word_length] = '\0';

            //write word to local dictionary
            dict[dict_index] = malloc(word_length + 1);
            if (dict[dict_index] != NULL)
            {
                strcpy(dict[dict_index], word);
                dict_index++;
            }
            // prepare for next word
            word_length = 0;
        }
    }
    dict[dict_index] = NULL;
    /* We put a sentinel here so that we can find the last word ... */
    return dict;
}

/**
 * Prints dictionary.
 */
void print(char **dict)
{
    for (int i = 0 ; dict[i] != NULL ; i++)
        printf("%s %p\n", dict[i], (void *) dict[i]);
}
Run Code Online (Sandbox Code Playgroud)

  • 不,当你用`malloc()`创建一个指针时,它会一直有效,直到你在它上面调用`free()`,这样你就可以在程序生效的同时获得整个程序的数据. (2认同)