C++ 如何在函数外部释放malloc

C++ 如何在函数外部释放malloc,c++,function,pointers,malloc,C++,Function,Pointers,Malloc,无法解决此问题-我的编译器总是告诉我自由(指针)函数有一些问题。所以我不确定指针的工作情况,但调试表明,实际上一切都很好。只有free函数无法释放内存 #include <stdio.h> //Bibliothek für input/output. #include <stdlib.h> //For malloc #include <math.h> //Bi

无法解决此问题-我的编译器总是告诉我自由(指针)函数有一些问题。所以我不确定指针的工作情况,但调试表明,实际上一切都很好。只有free函数无法释放内存

    #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
}
#包括//Bibliothek für输入/输出。
#包括//用于malloc
#包括//Bibliothek für MatcheMatcheMatche Operationen。
在C++中包含//BiopoToek fur in /输出。
#包括//Bibliothek für布尔值
//原型
整数*读取数(整数大小);
int sumupNumber(int*sumpointer,int size);
//主要功能
int main()
{
int arraySize;//malloc数组的大小
int*pointer;//用于存储malloc地址的指针
int total;//sumUpNumbers函数的变量
pointer=NULL;//零上的点
//在从用户处获得号码之前通知用户

std::coutfor
循环的
限制是错误的。您正在写入数组末尾的一个位置,这可能会损坏内存,导致以后程序失败。将for
循环的
更改为:

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

for(i=0;i在
readNumbers
函数中,您有:

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

您在sumUpNumbers函数中也遇到同样的问题。但这很可能只会导致不正确的求和,尽管这在技术上是未定义的行为。

您的代码几乎没有问题:

  • fflush(stdin)
    是未定义行为的生成器
  • 两个不正确的计数器:如果大小为
    size
    ,则必须为(i=0;i
  • 如果
    数组
    ,则
    int*readNumbers(int size)
    返回
    int
    而不是
    int*
  • C和C++的混合,没有明显的原因,使用<代码> CIN < /代码>和<代码> CUT
  • <>除了写了三个明显的错误(1)和(2)和(3)之外,你还强迫自己使用C++编译器(4)来编译一些东西,其中99%个是简单的C代码。为什么?
    如果您用“<代码> SCANFE())/<代码>和<代码> Prtff()> /Cudio>调用,您就可以取消C++,因此可以使用C编译器。在这种情况下,也要修改<代码> MalOC 调用,以符合<代码> C<代码>代码>标准:

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

    然后你会得到100%的C代码,它更易于阅读、研究和调试。

    fflush(stdin);
    调用未定义的行为,并可能导致程序崩溃。你确定这不是实际问题吗?只需删除该行。
    (i=0;I2相当肯定,编译器没有告诉您有麻烦。当然,它比C++更具体。为什么在C++中使用Maloc?我相当肯定它不是编译器,而是OS中的运行时异常……为什么使用<代码> Malc C < /C>和<代码> Fabue/Cuff>。我的建议是OP,它是简化代码的,我建议我的OP有可能简化代码,使它成为100%个C,而不是使用C++编译器来编写代码,它是1% C++ C++ @ LPS。iguous.(-:@user3078414如果您将第4版添加到列表中,我相信它将是完整的。readNumbers()中有返回0。在C中更改为返回NULL时,它将增加可移植性。
    
    array = malloc(size * sizeof(int)); //no result casting!