C - 将数组本地复制到全局 - 分段错误

时间:2016-02-11 18:33:13

标签: c arrays

我希望有一个全局数组,可以将其复制到本地数组中,以便进行每个菜单选择'。您可以在选择1中指定数组的大小,当您将本地数组复制到全局数组时,我觉得这会混淆全局数组大小。当试图完成这个'复制'在'菜单1'您将收到"分段故障(核心转储)"错误,如果您的金额为'是高(10,000 +)。

同样在退出程序时,您会收到"分段故障(核心转储)"代码139错误。

所以我的问题是如何复制一个阵列,我仍然可以拥有一个全局数组'而不是遇到这些错误。我包含了一些 sizeof array printf来检查大小,因为正如我上面所说,我认为在复制之前需要做一些事情来改变数组的大小。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

int main()
{

    int choice = 0; // menu variable
    int global_amount = 0; //random number generator var's
    int globalArray[0];

    do
    {       
        system("clear");
        printf("Menu\n");
        printf("1: Random Number Generator\n");
        printf("2: Bubble Sort\n");
        printf("3: Exit\n");
        printf("\nNote: Only Random Number Generator Writes To Global Array!\n");
        printf("\nMenu Choice: ");
        scanf ("%d", &choice);

        if (choice == 1)
        {
            system("clear");
            printf("Random Number Generator\n");
            printf("---------\n");          

            printf("Enter the amount of numbers you want (range will be from 0-99): ");
            scanf ("%d", &global_amount);

            int highest = 99;

            // initilize array with size "amount" specified above
            int randArray[global_amount]; 

            int i, randomNumber;

            for(i=0;i<global_amount;i++)
            {
                randomNumber = rand() % highest;
                randArray[i] = randomNumber;
            }

            printf("\nArray Filled With Random Numbers 0 through %d\n\n", highest);

            printf("\nSize of arrays before copy\n");
            printf("randArray - %zu\n", sizeof(randArray));
            printf("globalArray - %zu\n", sizeof(globalArray));

            // copy local array to global array
            for(i=0;i<global_amount;i++)
            {
                globalArray[i] = randArray[i];
            }

            printf("\nSize of arrays after copy\n");
            printf("randArray - %zu\n", sizeof(randArray));
            printf("globalArray - %zu\n", sizeof(globalArray));

            // prints out your array
            printf("\nYour array has these values: \n");
            for(i=0;i<global_amount;i++)
            {
                printf("%d ", globalArray[i]);
            }
            printf("\n");

            printf("\nPress enter to continue...\n");
            char var;scanf ("%c", &var);scanf ("%c", &var);
        }

        else if (choice == 2)
        {
            system("clear");
            printf("Bubble Sort\n");
            printf("---------\n");          

            int i;
            int bubbleArray[global_amount]; // local array

            // copy global array to local array
            for(i=0;i<global_amount;i++)
            {
                bubbleArray[i] = globalArray[i];
            }

            // display array to be sorted
            printf("Your array had these values: \n");
            for(i=0;i<global_amount;i++)
            {
                printf("%d ", bubbleArray[i]);
            }

            // bubble sort code
            int c, d, swap;

            for (c = 0 ; c < ( global_amount - 1 ); c++)
            {
                for (d = 0 ; d < global_amount - c - 1; d++)
                {
                    if (bubbleArray[d] > bubbleArray[d+1]) /* For decreasing order use < */
                    {
                        swap       = bubbleArray[d];
                        bubbleArray[d]   = bubbleArray[d+1];
                        bubbleArray[d+1] = swap;
                    }
                }
            }

            printf("\n\nBubble Sort Completed\n");

            // display sorted array
            printf("\n");
            printf("Sorted array list in ascending order:\n");
            for ( c = 0 ; c < global_amount ; c++ )
            {
                printf("%d ", bubbleArray[c]);
            }
            printf("\n");

            printf("\nPress enter to continue...\n");
            char var;scanf ("%c", &var);scanf ("%c", &var);
        }

        else if (choice == 3)
        {
            system("clear");
            return 0;
        }

        else
        {
            system("clear");
            printf("That is not a valid entry.\n");
            printf("Press enter to continue...\n");
            char var;scanf ("%c", &var);scanf ("%c", &var);
        }
    }
    while (choice !=3);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

你的“global_array”的大小为零,所以它基本上是一个堆栈上的地址,当数字被复制到它时,它会覆盖其他堆栈内容。当您通过堆栈段的边缘时,您会得到Segmentation fault

你的randArray是一个C99 VLA(可变长度数组),它也存在于堆栈中,但它有适当的大小。

注意,术语“全局”通常用于具有文件范围且不在堆栈中的变量。

答案 1 :(得分:1)

C中的数组不会自动调整大小。定义alert()不是如何做到的。

您需要为int globalArray[0];动态分配空间,如下所示:

globalArray

在NULL指针上调用int *globalArray = NULL; // initialize to NULL so you can safely clean up ... // if globalArray was allocated before, free it free(globalArray); // allocate space for global_amount ints globalArray = malloc(sizeof(int)*global_amount); // copy local array to global array for(i=0;i<global_amount;i++) { globalArray[i] = randArray[i]; } ... else if (choice == 3) { // clean up when you're done free(globalArray); system("clear"); return 0; } 是一个无操作,这就是我们将其初始化为NULL的原因。

相关问题