简体   繁体   English

EXC_BAD_ACCESS关于基数排序的链接列表中的指针

[英]EXC_BAD_ACCESS on pointer in linked list for radix sort

I'm trying to come up with a rudimentary radix sort (I've never actually seen one, so I'm sorry if mine is awful), but I am getting an EXC_BAD_ACCESS error on the line link = *(link.pointer); 我正在尝试提出一个基本的基数排序(我实际上从未见过,所以很抱歉我的糟糕),但是我在行link = *(link.pointer);上收到EXC_BAD_ACCESS错误link = *(link.pointer); . My C skills aren't great, so hopefully someone can teach me what I'm doing wrong. 我的C技能不是很好,所以希望有人可以教我我做错了什么。

I'm using XCode and ARC is enabled. 我正在使用XCode,并且已启用ARC。

Here is the code: 这是代码:

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

#define ARRAY_COUNT 10
#define MAX_VALUE   1000000
#define MODULO 10.0f

typedef enum
{
    false,
    true
} bool;

typedef struct linkedListStruct
{
    int value;
    struct linkedListStruct *pointer;
} LinkedList;

void radixSort(int *array);
bool arraySorted(int *array);
int * intArray(int minValue, int maxValue);

int main(int argc, const char * argv[])
{
    int *sortingArray = intArray(0, MAX_VALUE);

    radixSort(sortingArray);

    printf("Array %s sorted", arraySorted(sortingArray) ? "" : "not");

    return 0;
}

void radixSort(int *array)
{
    int numberOfIterations = (int)ceilf(log(MAX_VALUE)/log(MODULO));
    for(int n = 0; n < numberOfIterations; n++)
    {
        LinkedList *linkedListPointers[(int)MODULO] = {0};
        int i = ARRAY_COUNT;
        while(i--)
        {
            int location = (int)floor((array[i] % (int)powf(MODULO, n + 1))/powf(MODULO, n));
            LinkedList link = { array[i], NULL };
            link.pointer = linkedListPointers[location];
            linkedListPointers[location] = &link;
        }
        int location = 0;
        for(int pointerSelection = 0; pointerSelection < MODULO; pointerSelection++)
        {
            if(linkedListPointers[pointerSelection])
            {
                LinkedList link = { 0, linkedListPointers[pointerSelection] };
                linkedListPointers[pointerSelection] = NULL;
                while(link.pointer)
                {
                    link = *(link.pointer);
                    array[location++] = link.value;
                }
            }
        }
    }
}

bool arraySorted(int *array)
{
    int i = ARRAY_COUNT;
    while(--i)if(array[i - 1] > array[i])break;
    return !i;
}

int * intArray(int minValue, int maxValue)
{
    int difference = maxValue - minValue;
    int *array = (int *)malloc(sizeof(int) * ARRAY_COUNT);
    int i;
    for(i = 0; i < ARRAY_COUNT; i++)
    {
        array[i] = rand()%difference + minValue;
    }
    return array;
}

Also, if someone wants to suggest improvements to my sort, that would also be appreciated. 另外,如果有人想提出改进建议,也将不胜感激。

The problem came from how I was allocating the linked list. 问题出在我如何分配链表。 I changed 我变了

LinkedList link = { array[i], NULL };
link.pointer = linkedListPointers[location];

to

LinkedList *link = malloc(sizeof(LinkedList));
link->value = array[i];
link->pointer = linkedListPointers[location];

In the first example, the pointer to link remained the same through each loop iteration (I wasn't aware it would do that), so I needed to make the pointer point to a newly allocated memory chunk. 在第一个示例中, link的指针在每次循环迭代中都保持不变(我不知道这样做会如此),因此我需要使指针指向新分配的内存块。

EDIT: 编辑:

Changing that also had me change from 改变那也让我从

while(link.pointer)
{
    link = *(link.pointer);
    array[location++] = link.value;
}

to

while(linkPointer)
{
    link = *linkPointer;
    array[location++] = link.value;
    linkPointer = link.pointer;
}

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

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