c++ - how to free malloc outside of function -
can't resolve problem - compiler allways tells me have troubles free(pointer) function. i'm not sure working of pointers debugging has shown works well. free function could't free memory.
#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 malloc-array int* pointer; //pointer storing of malloc-address int total; //variable sumupnumbers function pointer = null; //point on 0 //inform user before getting number him std::cout << "please give size of array:" << std::endl; fflush(stdout); //free output window //get number size of array scanf("%d", &arraysize); //call readnumbers function , store first address of //the malloc-array in pointer pointer = readnumbers(arraysize); //call sumupnumbers function , store number in total total = sumupnumbers(pointer, arraysize); fflush(stdout); //free output window //show number total printf("\n total of array:%d", total); //call free function making memory of //the malloc-array free again free(pointer); fflush(stdin); //free keyboard buffer getchar(); //wait feedback user return 0; //return 0 machine in case if works } //this function has pointer extension because want work //array outside of function. give function size of array //we want build. function builds array , fills numbers //and gives first address of array. int* readnumbers(int size) { int* array; //pointer creating of malloc-array int i; //counter //pointer storing of first address of array int* helppointer; array = null; //set pointers helppointer = null; // on 0 //create array array = (int *) malloc(size * sizeof(int)); //check value of array sure have created //the array without errors if(array != null) { //store first address of malloc-pointer helppointer = array; //give value parts of array for(i=0; i<=size; i++) { //inform user printf("\n give %d. nummber of array:\n", i+1); fflush(stdout); //free output window //read value scanf("%d", array+i); } return helppointer; //return first address } //if went wrong creating of array, do: else { //tell user, computer does't have enough memory std::cout << "there no place saving data in mamory"; return 0; //return failure } } //the input of function pointer address of malloc-array //from readnumbers , size of array. function adds numbers //from array , gives result of additation back. int sumupnumbers(int* sumpointer, int size) { int sum; //variable storing of total value int i; //counter sum = 0; //set sum on 0 before work //count values array for(i=0; i<=size; i++) { //count 1 number after sum = sum + *(sumpointer+i); } return sum; //return total value }
the limits of for
loops wrong. writing 1 position on end of array, might corrupt memory later program fails. change for
loops to:
for(i=0; i<size; i++)
Comments
Post a Comment