Help!
I am using malloc to create an array on the heap... but I can't seem to get the compiler to recognize the call to "free"!
/********************************************
* Program Name: callFree.c
* Created By: Myriddin
* Creation Date: Friday, March 29, 2015
*
* Program Purpose: This program will be
* a place where I can practise with calling
* free!
*
*
* Modification History:
*
* Name: Date: Modification:
*
*
**********************************************/
include stdio.h
include stdlib.h
//The syntax of the include files stdio and stdlib had to be changed so they would appear in posting....
int main(int argc, char *argv[]) {
int intFlag = 1;
int n = 5;
//Version 1: Array is instantiated on the stack
//int arrayX[n];
//Version 2: Array is instantiated on the heap
int *arrayX = malloc(sizeof(int) * n);
//Load Array:
arrayX[0] = 23;
arrayX[1] = 42;
arrayX[2] = 4;
arrayX[3] = 16;
arrayX[4] = 8;
arrayX[5] = 15;
for(int i = 0; i < n; i++)
{
printf("Element: %d: %d\n", i, arrayX[i]);
}
//You need to call free on array that is in the heap!
if(intFlag == 1)
{
for(int i = 0; i < n; i++)
free(arrayX[i]);
free(arrayX);
}
return 0;
}
callFree.c:50:18: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'void *' [-Werror,-Wint-conversion]
free(arrayX[i]);
^~~~~~~~~
Thanks, Myriddin