434,720 Members | 2,244 Online
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

# Malloc and recursion

 P: n/a (At least I think that this is recursion) I've been getting stuck on this for far too long now. I suspect that this can't be done how I want it to. I'm thinking that I would need to store the mal in recurse() as an array, and then I would be able to free it. Thanks for your help. Matt /* 11/04/06 Write a function containing malloc(), which calls itself five times then returns. Write another function which frees all the memory. */ #include #include void recurse(int *); int main(void) { int a=0; recurse(&a); return 0; } void recurse(int *track) { int *mal = malloc(3*sizeof(int)); mal[0] = 0; mal[1] = 1; mal[2] = 2; (*track)++; if ((*track) < 5) recurse(track); } Apr 11 '06 #1
9 Replies

 P: n/a ballpointpenthief opined: (At least I think that this is recursion) I've been getting stuck on this for far too long now. I suspect that this can't be done how I want it to. I'm thinking that I would need to store the mal in recurse() as an array, and then I would be able to free it. Thanks for your help. Matt /* 11/04/06 Write a function containing malloc(), which calls itself five times then returns. Write another function which frees all the memory. */ #include #include void recurse(int *); int main(void) { int a=0; recurse(&a); return 0; } void recurse(int *track) { int *mal = malloc(3*sizeof(int)); mal[0] = 0; mal[1] = 1; mal[2] = 2; (*track)++; if ((*track) < 5) recurse(track); } You were absolutely on the right track. I think what you wanted is: /* 11/04/06 Write a function containing malloc(), which calls itself five times then returns. Write another function which frees all the memory. */ #include #include void recurse_malloc(int *); void recurse_free(int *); char *array[5]; int main(void) { int a=0; recurse_malloc(&a); recurse_free(&a); return 0; } void recurse_malloc(int *track) { array[*track] = malloc(1); *array[*track] = '0' + *track; printf("alloc: %d '%c'\n",*track,*array[*track]); (*track)++; if ((*track) < 5) recurse_malloc(track); } void recurse_free(int *track) { (*track)--; printf("free: %d '%c'\n",*track,*array[*track]); free(array[*track]); if ((*track) != 0) recurse_free(track); } -- "I once witnessed a long-winded, month-long flamewar over the use of mice vs. trackballs...It was very silly." (By Matt Welsh) Apr 11 '06 #2

 P: n/a > (At least I think that this is recursion) I've been getting stuck on this for far too long now. I suspect that this can't be done how I want it to. I'm thinking that I would need to store the mal in recurse() as an array, and then I would be able to free it. Well of course you should save it to free it, but the question is "Where". One possible solution would be to save the pointer at the begining of the allocated array, and then pass that new allocated array to the next function call, which saves it in its own new array. Finally, the last array address could be returned and the free function would go in reverse direction freeing the allocated arrays. This can be thought of as a LIFO list... -- Abdo Haji-Ali Programmer In|Framez Apr 11 '06 #3

 P: n/a On 11 Apr 2006 08:31:42 -0700 "ballpointpenthief" wrote: void recurse(int *track) { int *mal = malloc(3*sizeof(int)); mal[0] = 0; mal[1] = 1; mal[2] = 2; (*track)++; if ((*track) < 5) recurse(track); I don't see what the problem is calling free(mal) here, mal is a local variable and should not be overwritten by subsequent calls to recurse. } I might be wrong of course. Let me know if I am. -- Regards, Ed :: http://www.s5h.net :%s/\t/ /g :: proud unix system person :%s/Open Source/Free Software/g Apr 11 '06 #4

 P: n/a > I don't see what the problem is calling free(mal) here, mal is a local variable and should not be overwritten by subsequent calls to recurse. } I might be wrong of course. Let me know if I am. You are absolutely right, however the required program should call another "free" function to free the allocated arrays, instead of freeing them in the same function Abdo Haji-Ali Programmer In|Framez Apr 11 '06 #5

 P: n/a ballpointpenthief wrote: Write a function containing malloc(), which calls itself five times then returns. Write another function which frees all the memory. */ #include void example( int recurse ) { if (sizeof( recurse ) == 0) malloc( 17 ); if (example) example(0), example(0), example(0), example(0), example(0); } void freeAllMemoryAllocatedByExample() {} // alternative implementation: { exit( 0 ); } int main(void) { example( 1 ); freeAllMemoryAllocatedByExample(); return 0; } -- Chris "not a Tuvela" Dollin "To say that the human is thus and so is almost always to lie automatically." Apr 12 '06 #6

 P: n/a In article , Chris Dollin writes: void example( int recurse ) { if (sizeof( recurse ) == 0) malloc( 17 ); if (example) example(0), example(0), example(0), example(0), example(0); I fear example will all too often be true. -- Michael Wojcik mi************@microfocus.com Apr 12 '06 #7

 P: n/a I wrote: .. I'm thinking that I would need to store the mal in recurse() as an array, and then I would be able to free it. So then after reading the replies I tried this, and I think this must be the simplest way of doing what I need to use this for. Comments welcome. =========== /* 11/04/06 Write a function containing malloc(), which calls itself five times then returns. Write another function which frees all the memory. */ #include #include void recurse(int *); void recurse_free(int **, int *); int **mal; int main(void) { int a=0; mal = NULL; recurse(&a); recurse_free(mal, &a); return 0; } void recurse(int *track) { mal = realloc(mal, ((*track)+1)*sizeof(int *)); mal[*track] = malloc(3*sizeof(int)); mal[*track][0] = 0; mal[*track][1] = 1; mal[*track][2] = 2; (*track)++; if ((*track) < 5) recurse(track); } void recurse_free(int **to_free, int *track) { while (*track) { free(to_free[*track]); (*track--); } free(to_free); } Apr 12 '06 #8

 P: n/a ballpointpenthief wrote: I wrote: . I'm thinking that I would need to store the mal in recurse() as an array, and then I would be able to free it. So then after reading the replies I tried this, and I think this must be the simplest way of doing what I need to use this for. Comments welcome. Now that you've tried your hand at it, here's my solution: #include #include typedef struct data { struct data *ptr; } *dataptr; /* --------------- */ dataptr recursemake(dataptr p, int depth) { dataptr newp; printf("%d %p\n", depth, (void *)p); if (0 == depth) return p; else if (newp = malloc(sizeof *newp)) { newp->ptr = p; return recursemake(newp, depth - 1); } return newp; } /* recursemake */ /* --------------- */ dataptr recursefree(dataptr p) { dataptr t; if (p) { printf("%p %p\n", (void*)p, (void*)p->ptr); t = p; p = recursefree(p->ptr); free(t); } return p; } /* recursefree */ /* --------------- */ int main(void) { dataptr p; p = recursemake(NULL, 5); p = recursefree(p); return 0; } /* main */ -- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson More details at: Also see Apr 13 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.