I got my code to work, but need some feedback if I am free up the memory correctly? Is there a way to make sure that I have clear up the memory? I just wanted to get in the good habit since I am pretty new at this. Thanks for the feed back:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *readfile(const char *filename);
int main(int argc, char *argv[]) {
char *pms;
int size = 1;
pms = readfile(argv[1]);
printf("%s", pms++);
return (0);
}
char *readfile(const char *filename){
static char c;
char *out;
FILE *myfile;
int size;
out = (char *)malloc(24130);
int x=0;
myfile = fopen(filename, "r");
while ((c=fgetc(myfile)) != EOF){
out[x]=c;
x++; }
//resize the memory here
size = strlen(out)+1;
if(!realloc(out, size))
{
printf("Unable to allocate memory");
return (0);
}
return(out);
free(out);
}