2016-05-25 7 views
-3

この問題は解決できません - 私のコンパイラはフリー(ポインタ)関数にいくつか問題があると私に言います。だから私は私のポインタの作業についてはわかりませんが、デバッグは実際にはうまくいくことを示しています。自由な関数だけがメモリを解放できませんでした。関数の外で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

'fflush(stdin);'未定義の動作を呼び出し、プログラムがクラッシュする可能性があります。これは実際の問題ではないと確信していますか?その行を削除するだけです。 – Lundin

+3

'for(i = 0; i <= size; i ++)'が割り当てられたメモリを超えています。注:「コンパイルエラー」はありません。あなたが言ったよりも1つ多くのエントリを求めていることに気付かなかったのですか? –

+2

コンパイラがあなたに**いくつかの問題を抱えているとは言いません**。確かにそれはそれよりも具体的です。また、なぜC++でmallocを使用するのですか? – user2079303

答えて

2

forループの制限が間違っています。配列の終わりに1つの位置に書き込んでいると、メモリが破損してプログラムが失敗する可能性があります。

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

が、配列だけsize要素の長さで、これだけ<<=を変更します:

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

あなたが持っているreadNumbers機能で

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

:へforループを変更しますsumUpNumbers関数にも同じ問題があります。しかし、これは技術的に未定義の振る舞いであるにもかかわらず、不正確な合計をもたらす可能性が最も高い。

1

あなたのコードでは、いくつかの問題があります。

  1. fflush(stdin)は未定義の動作の発電機です。
  2. 2つの間違ったカウンター:サイズはsizeある場合arrayNULLであれば、あなたはfor(i = 0; i < size; i++)
  3. あなたint* readNumbers(int size)リターンintの代わりint*をカウントしなければなりません。 cinとは別に

coutを使用するCおよびC++がない明白な理由のための

  • 奇妙な混合が書かれた3つの明白な過ち(1)及び(2)と(3)、あなたはまた、使用することを自分自身をプッシュします何かをコンパイルするためのC++コンパイラ(4)、その99%はプレーンなCコードです。どうして?

    あなたが適切なscanf()printf()呼び出しでcincoutを交換する場合は、あなたがC++を取り除きます。したがって、Cコンパイラを使用することができます。その場合もC標準に準拠するために、mallocコールを修正してください:

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

    次にあなたは、勉強やデバッグを読みやすくなり、100%のCコードを取得。

  • +0

    OPは[tag:C++]コードをコンパイルしていますので、 'malloc'の戻り値のキャストは必須です。 – LPs

    +0

    私の答えがはっきりしていることを願っています。私は** <1%C++ **、@ LPのコードにC++コンパイラを使わなくても、コードを単純化して100%**プレーンなC **にする可能性についてOPにアドバイスしました。なんらかの理由で、それはあいまいなCとC++の両方のタグが付けられています。 ( - : – user3078414

    +0

    @ user3078414あなたがあなたのリストに問題4を追加するとそれは完全に信じられるでしょう readNumbers()にreturn 0があります。CでNULLを返すように変更された場合、 は移植性を向上させます –

    関連する問題