不保留在C中的函数中操作的全局指针中的值

不保留在C中的函数中操作的全局指针中的值,c,pointers,heap-memory,dynamic-memory-allocation,C,Pointers,Heap Memory,Dynamic Memory Allocation,我有一个全局指针,声明为int*elements,并初始化为elements=(int*)calloc(noOfElements,sizeOf(int)) 我有一个函数,它将元素中的数据排序为 void InsertionSort(int start, int size) { int i,j,temp; for(i=start+1; i<size; i++) { temp = elements[i]; for(j=i-1; j>

我有一个全局指针,声明为
int*elements
,并初始化为
elements=(int*)calloc(noOfElements,sizeOf(int))

我有一个函数,它将元素中的数据排序为

void InsertionSort(int start, int size)
{
    int i,j,temp;
    for(i=start+1; i<size; i++)
    {
        temp = elements[i];
        for(j=i-1; j>=0&& elements[j]>temp; j--)
            elements[j+1] = elements[j];
        elements[j+1] = temp;
    }
}
新值={15,21,35,49,77,83,86,86,6303760,0}

我一直在使用GDB来跟踪这一点,但我不知道为什么它在函数执行后会改变

为了更清楚,我尝试使用

sections[i]=&elements[completion]

函数“InsertionSort”完成执行后

以下是完整程序的基本代码,为清晰起见,请提供注释

            // Global Variables
            int noOfThreads;
            int noOfElements;
            char sortAlgo;
            int * elements;
            int range;
            int lastRange;

            void FillArray(){
                int i;
                for(i = 0;i < noOfElements; i++){
                    // Limiting random value range from 0 to 99
                    elements[i] = rand()%100;
                }
            }

            int main(int argc, char *argv[]){
                int **sections;
                int completion;
                /*
                * Basic argument checking conditions removed 
                */
                elements = (int *)calloc(noOfElements,sizeof(int));
                noOfElements = atoi(argv[1]);
                noOfThreads = atoi(argv[2]);
                sortAlgo = argv[3][0];
                // Calculate the size of each section to be separated from the elements array to be operate individually based on number of threads requested
                range = noOfElements / noOfThreads;
                // Calculate the size of the last section to be separated from the elements array to be operate individually 
                lastRange = range + (noOfElements % noOfThreads);

                sections = (int** )malloc(noOfThreads * sizeof(int*));
                for (int i = 0; i < noOfThreads-1; i++ )
                    sections[i] = (int* )calloc(range,sizeof(int));
                sections[noOfThreads-1] = (int* )calloc(lastRange,sizeof(int));

                // Fill the array with random values
                FillArray();

                completion=0;
                for (int i = 0; i < noOfThreads - 1; i++ )
                {
                        InsertionSort(completion, range);
                        sections[i] = &elements[completion];
                        completion = completion + range;
                }
                InsertionSort(completion, lastRange);
                sections[noOfThreads - 1] = &elements[completion];
                Merge(sections,elements);

            }
//全局变量
int noOfThreads;
国际面条;
查·索塔尔戈;
int*元素;
整数范围;
int lastRange;
void FillArray(){
int i;
对于(i=0;i
您的问题似乎不太可能出现在您发布的代码中。你能发布一个完整的例子来复制你的问题吗?
noOfElements
的值是多少?传递给你的函数的
range
的值是多少?为什么你甚至有一个全局指针变量?这是自找麻烦。@stackptr为了简化代码。但我不认为这会导致我所面临的问题。我对指针、动态内存分配和调用堆栈的概念比较陌生。你不能将指针解引用到另一个作用域。如果指针是全局的,当它指向另一个作用域时,您可能会意外地取消对它的引用。
            // Global Variables
            int noOfThreads;
            int noOfElements;
            char sortAlgo;
            int * elements;
            int range;
            int lastRange;

            void FillArray(){
                int i;
                for(i = 0;i < noOfElements; i++){
                    // Limiting random value range from 0 to 99
                    elements[i] = rand()%100;
                }
            }

            int main(int argc, char *argv[]){
                int **sections;
                int completion;
                /*
                * Basic argument checking conditions removed 
                */
                elements = (int *)calloc(noOfElements,sizeof(int));
                noOfElements = atoi(argv[1]);
                noOfThreads = atoi(argv[2]);
                sortAlgo = argv[3][0];
                // Calculate the size of each section to be separated from the elements array to be operate individually based on number of threads requested
                range = noOfElements / noOfThreads;
                // Calculate the size of the last section to be separated from the elements array to be operate individually 
                lastRange = range + (noOfElements % noOfThreads);

                sections = (int** )malloc(noOfThreads * sizeof(int*));
                for (int i = 0; i < noOfThreads-1; i++ )
                    sections[i] = (int* )calloc(range,sizeof(int));
                sections[noOfThreads-1] = (int* )calloc(lastRange,sizeof(int));

                // Fill the array with random values
                FillArray();

                completion=0;
                for (int i = 0; i < noOfThreads - 1; i++ )
                {
                        InsertionSort(completion, range);
                        sections[i] = &elements[completion];
                        completion = completion + range;
                }
                InsertionSort(completion, lastRange);
                sections[noOfThreads - 1] = &elements[completion];
                Merge(sections,elements);

            }