如何在函数之外释放malloc

时间:2016-05-25 08:56:11

标签: c++ function pointers malloc

无法解决这个问题 - 我的编译器总是告诉我,我有一些免费(指针)功能的麻烦。所以我不确定我的指针的工作情况,但是调试表明实际上一切都运行良好。只有免费功能才能释放内存。

    #include <stdio.h>              //Bibliothek für input/output.
    #include <stdlib.h>             //For malloc
    #include <math.h>               //Bibliothek für matchematische Operationen.
    #include <iostream>             //Bibliothek für in/output in C++.
    #include <stdbool.h>            //Bibliothek für boolean

    //Prototypes
int* readNumbers(int size);
int sumUpNumbers(int* sumpointer, int size);

//Main function
int main()
{
    int arraySize;  //Size of the malloc-array
    int* pointer;   //pointer for storing of the malloc-address
    int total;      //variable for the sumUpNumbers function
    pointer = NULL; //point on zero

    //inform the user before getting a number from him
    std::cout << "Please give the size of array:" << std::endl;
    fflush(stdout); //free the output window
    //get a number for the size of array
    scanf("%d", &arraySize);

    //call the readNumbers function and store the first address of
    //the malloc-array in pointer
    pointer = readNumbers(arraySize);

    //call the sumUpNumbers function and store the number in total
    total = sumUpNumbers(pointer, arraySize);

    fflush(stdout); //free the output window
    //show the number from total
    printf("\n total of the array:%d", total);

    //call the free function for making the memory of
    //the malloc-array free again
    free(pointer);

    fflush(stdin);  //free the keyboard buffer
    getchar();      //wait for a feedback from user
    return 0;       //return 0 to the machine in case if everything works well
}


//This function has a pointer extension because we want to work with the
//array outside of this function. We give the function a size of the array
//we want to build. The function builds an array and fills it with numbers
//and than gives us back the first address of the array.
int* readNumbers(int size)
{

    int* array;         //pointer for creating of malloc-array
    int i;              //counter

    //pointer for storing of the first address of the array
    int* helpPointer;
    array = NULL;       //set the pointers
    helpPointer = NULL; //              on zero

    //create the array
    array = (int *) malloc(size * sizeof(int));

    //check the value of the array to be sure that we have created
    //the array without errors
    if(array != NULL)
    {
        //store the first address of the malloc-pointer
        helpPointer = array;
        //give some value to all the parts of array
        for(i=0; i<=size; i++)
        {
            //inform the user
            printf("\n give the %d. nummber of the array:\n", i+1);
            fflush(stdout); //free the output window
            //read the value
            scanf("%d", array+i);
        }

        return helpPointer; //return the first address
    }
    //if something went wrong by creating of the array, do:
    else
    {
        //tell the user, what we computer does't have enough memory
        std::cout << "There is no place for saving the data in mamory";
        return 0;   //return with failure
    }

}

//The input of this function is a pointer with the address of the malloc-array
//from the readNumbers and the size of this array. The function adds all the numbers
//from the array and gives us the result of the additation back.
int sumUpNumbers(int* sumpointer, int size)
{
    int sum;    //variable for storing of total value
    int i;      //counter
    sum = 0;    //set the sum on zero before work with it

    //count all the values from the array
    for(i=0; i<=size; i++)
    {
        //count one number after another
        sum = sum + *(sumpointer+i);
    }
    return sum; //return the total value
}

3 个答案:

答案 0 :(得分:2)

for循环的限制是错误的。您正在写入阵列末尾的一个位置,这可能会破坏内存,以便以后程序失败。将for循环更改为:

for(i=0; i<size; i++)

答案 1 :(得分:2)

readNumbers函数中:

for(i=0; i<=size; i++)

但数组只有size个元素,所以只需将<=更改为<

for(i=0; i < size; i++)

sumUpNumbers功能存在同样的问题。但这很可能只会导致不正确的总和,尽管它在技术上是未定义的行为。

答案 2 :(得分:1)

您的代码几乎没有问题:

  1. fflush(stdin)是未定义行为的生成器。
  2. 两个不正确的计数器:如果大小为size,则必须计算for(i = 0; i < size; i++)
  3. 如果int* readNumbers(int size)int,则int*会返回array而非NULL
  4. C和C ++的奇怪混合,没有明显的理由使用cincout
  5. 除了编写三个明显的错误(1)和(2)和(3)之外,你还要自己使用C ++编译器(4)来编译某些东西,其中99%是普通的C代码。为什么呢?

    如果您使用适当的cincout调用替换scanf()printf(),则可以摆脱C ++。所以你可以使用C编译器。在这种情况下,请务必修改malloc调用以符合C标准:

    array = malloc(size * sizeof(int)); //no result casting!
    

    然后你得到100%的C代码,更容易阅读,学习和调试。