For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2008 > how to free memory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to free memory
oswald.harry@gmail.com

2008-01-30, 4:34 am

hi
i wrote a directoryparse function that will return an array of
all .jpg or .jpeg filenames for another parse1(..) to work on. after
this i need to free all allocated memory..in this case I am not quite
sure how i should free the memory ..i wd appreciate if someone can
help me here



int ends_with(const char *haystack, const char *needle){

const char *found = strstr(haystack, needle);
return (found && (found[strlen(needle)] == 0));

}

int selectjpeg(const struct dirent *d){
return ends_with(d->d_name, ".jpg") || ends_with(d->d_name,
".jpeg");

}

void parse1(int nmcnt,char** thenames){
int i;
for (i=0;i<nmcnt;i++){
printf("newnames[%d]is %s\n",i,*thenames);
thenames++;
}
}

int parsedirectory(char * dirname){
struct dirent **filelist = NULL;
char * directory =dirname;
int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, selectjpeg, alphasort);
if(fcount < 0) {
perror(directory);
return -1;
}
printf("total jpeg imgs:%d\n",fcount);

char** imgnamelist=malloc(fcount*sizeof(*imgnam
elist));
for(i=0;i<fcount;i++){
imgnamelist[i]=malloc( (strlen((*filelist)->d_name)+1)
*sizeof(**imgnamelist));

strcpy(imgnamelist[i],(*filelist)->d_name );
filelist++;
}
/* i pass it to a parse fn */
parse1(fcount,imgnamelist);

/* should i be doing the free() here? how? */

return fcount;
}

int main(int argc,char* argv[]) {
parsedirectory(argv[1]) ;
return 0;
}

Barry Margolin

2008-01-30, 4:34 am

In article
<84d6a45f-e060-4b1d-8d0a-5fa99c454abe@c23g2000hsa.googlegroups.com>,
oswald.harry@gmail.com wrote:

> hi
> i wrote a directoryparse function that will return an array of
> all .jpg or .jpeg filenames for another parse1(..) to work on. after
> this i need to free all allocated memory..in this case I am not quite
> sure how i should free the memory ..i wd appreciate if someone can
> help me here
>
>
>
> int ends_with(const char *haystack, const char *needle){
>
> const char *found = strstr(haystack, needle);
> return (found && (found[strlen(needle)] == 0));
>
> }
>
> int selectjpeg(const struct dirent *d){
> return ends_with(d->d_name, ".jpg") || ends_with(d->d_name,
> ".jpeg");
>
> }
>
> void parse1(int nmcnt,char** thenames){
> int i;
> for (i=0;i<nmcnt;i++){
> printf("newnames[%d]is %s\n",i,*thenames);
> thenames++;
> }
> }
>
> int parsedirectory(char * dirname){
> struct dirent **filelist = NULL;
> char * directory =dirname;
> int fcount = -1;
> int i = 0;
> fcount = scandir(directory, &filelist, selectjpeg, alphasort);
> if(fcount < 0) {
> perror(directory);
> return -1;
> }
> printf("total jpeg imgs:%d\n",fcount);
>
> char** imgnamelist=malloc(fcount*sizeof(*imgnam
elist));
> for(i=0;i<fcount;i++){
> imgnamelist[i]=malloc( (strlen((*filelist)->d_name)+1)
> *sizeof(**imgnamelist));
>
> strcpy(imgnamelist[i],(*filelist)->d_name );
> filelist++;
> }
> /* i pass it to a parse fn */
> parse1(fcount,imgnamelist);
>
> /* should i be doing the free() here? how? */


With the same kind of loop that you used to allocate it:

for(i=0; i<fcount; i++) {
free(imgnamelist[i]);
}
free(imgnamelist);

>
> return fcount;
> }
>
> int main(int argc,char* argv[]) {
> parsedirectory(argv[1]) ;
> return 0;
> }


--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com