简体   繁体   English

C:Malloc分段错误

[英]C: Malloc Segmentation Fault

I am getting a segmentation fault when using malloc. 使用malloc时出现分段错误。 When I uncomment the global COPY & LIST variables and comment out the malloc & free calls, the program runs as expected. 当我取消注释全局COPY&LIST变量并注释掉malloc和free调用时,程序将按预期运行。

Am I mis-using malloc or free? 我在滥用malloc还是免费? If so, what is the proper usage of malloc & free? 如果是这样,malloc和free的正确用法是什么?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>

#define MAX_LENGTH 1000000
//int LIST[MAX_LENGTH];
//int COPY[MAX_LENGTH];

/**
 * [main description]
 * Main function reads generated data and then measures run time (in seconds) of each 
 * sorting algorithm. Data is re-shuffled to its original state for each sort.
 * @param  argc
 * @param  argv
 * @return      [returns 0 on successful run]
 * O(n^2)
 */
int main(int argc, char const *argv[])
{
    void read(int*, int*);
    void refresh(int*, int*);
    void selectionSort(long, int*);
    void bubbleSort(long, int*);
    void insertionSort(long, int*);
    time_t start, finish;
    long length = 1000;
    int *LIST = (int*) malloc(length * sizeof(int));
    int *COPY = (int*) malloc(length * sizeof(int));
    read(LIST, COPY);
    for (length = 1000; length <= MAX_LENGTH; length=length+33300) {
        //code omitted
        refresh(LIST, COPY);
        //code omitted
        refresh(LIST, COPY);
        //code omitted
        refresh(LIST, COPY);
        LIST = realloc(LIST, length * sizeof(int));
        COPY = realloc(COPY, length * sizeof(int));
    }
    free(LIST);
    free(COPY);

    return 0;
}
/**
 * [read description]
 * Reads data from stdin, and populates @LIST. 
 * Also populates @COPY, a copy of @LIST. 
 */
void read(int* LIST, int* COPY) {
    long i;
    for (i = 0; i < MAX_LENGTH; i++)
    {
        scanf("%d", &LIST[i]);
        COPY[i] = LIST[i];
    }
}

/**
 * [refresh description]
 * Copies the contents of parameter from into parameter to. 
 */
void refresh(int *LIST, int *COPY) {
    int i;
    for (i = 0; i < MAX_LENGTH; i++) {
        LIST[i] = COPY[i];
    }
}

You're trampling way out of bounds with the refresh() function. 您正在使用refresh()函数超出范围。 The version I'm looking at is: 我正在查看的版本是:

void refresh(int *LIST, int *COPY) {
    int i;
    for (i = 0; i < MAX_LENGTH; i++) {
        LIST[i] = COPY[i];
    }
}

You should pass in the number of items to copy and not use MAX_LENGTH . 您应该传递要复制的项目数,而不要使用MAX_LENGTH

void refresh(int n_items, int *LIST, int *COPY) {
    int i;
    for (i = 0; i < n_items; i++) {
        LIST[i] = COPY[i];
    }
}

On a minor style note, you should usually reserve names in upper case for macros (with the known exceptions of FILE from <stdio.h> and DIR from <dirent.h> on POSIX systems; they are not usually macros). 在次要的样式说明上,通常应为宏保留大写名称(在POSIX系统上,已知的例外是<stdio.h>FILE<dirent.h> DIR ;它们通常不是宏)。

You have a bug in your malloc call. 您的malloc调用中有一个错误。 The lines below: 下面的行:

int *LIST=(int*) malloc(sizeof(int*) * length);
int *COPY=(int*) malloc(sizeof(int*) * length);

should be: 应该:

int *LIST=(int*) malloc(sizeof(int) * length);
int *COPY=(int*) malloc(sizeof(int) * length);

You are doing something similar with your realloc call. 您正在执行与realloc调用类似的操作。 I'm not 100% what your intent is with the realloc calls, but it should probably be something like: 我不是100%打算使用realloc调用,但是可能应该是这样的:

LIST = (int*)realloc(LIST, length * sizeof(int));

Alternatively, you could just define something to represent the size of a single element, since you're always working with int types throughout your code. 另外,由于在整个代码中始终使用int类型,因此您可以定义一些内容来表示单个元素的大小。

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

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