| | |
How to free up memory used for queues?
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
I have a first-in-first-out queue implementation such that when it is first initialized, a malloc is called to set aside memory for items that will be placed on the queue. When I am done with the queue, how do I free up the memory?
My code for the queue is as follows:
My code for the queue is as follows:
C Syntax (Toggle Plain Text)
typedef Edge Item; void QUEUEinit(int); int QUEUEempty(void); void QUEUEput(Item); Item QUEUEget(void); static Item *q; static int N,head,tail; void QUEUEinit(int maxN) {q = malloc((maxN+1)*sizeof(Item)); N = maxN+1; head=N; tail=0;} int QUEUEempty() { return head % N == tail;} void QUEUEput(Item item) { q[tail++] = item; tail = tail % N;} Item QUEUEget() { head = head % N; return q[head++];}
As defined in stdlib.h and malloc.h will do the trick. Assure only pointers that were created with malloc, calloc or realloc are passed to free (), otherwise other calls to get memory may have unpredictable results.
C Syntax (Toggle Plain Text)
free (q);
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
I did try the free(q), but was surprised when the following thing happened:
when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
•
•
•
•
Originally Posted by kloony
I did try the free(q), but was surprised when the following thing happened:
when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
“The essential notion of a socialist society is force.”
— Milton Friedman
— Milton Friedman
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
The free() function is still puzzling me in some sense. I have a simple code below which creates a structure called graph. The code compiles without any problem and when it is executed, the output prints out 2 "10"'s, one before the free(), another one after. Did the memory set aside for G really got freed?
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> main() { typedef struct graph *Graph; struct graph{int V; int E;}; int V=10; int E=10; Graph G=malloc(sizeof *G); G->V=V; G->E=E; printf("%d\n", G->E); free(*G); printf("%d\n", G->E); return 0;}
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
Apologies for my previous code....the free function should be
I mistyped it. Any help would be greatly appreciated.
C Syntax (Toggle Plain Text)
free(G);
I mistyped it. Any help would be greatly appreciated.
Hello,
When free() is called, a block of memory previously allocated by a call to malloc, calloc or realloc is freed: Further documentation:
C Reference: malloc()
C Reference: free()
- Stack Overflow
When free() is called, a block of memory previously allocated by a call to malloc, calloc or realloc is freed:
Graph G=malloc(sizeof *G); /* Memory is dynamically allocated */ if (G != NULL) { /* G successfully points to an empty block of memory */ /* Do task here */ }else if (G == NULL) { return 0; /* Memory allocation failed */ } free(G); /* Dynamically allocated memory is freed */
C Reference: malloc()
C Reference: free()
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
Thanks, that's why I thought that after I "free(G)", the code
should not be able to return me a value of 10. when it does, it surprises me, which makes me wonder whether the memory has really been freed.
C Syntax (Toggle Plain Text)
printf("%d\n", G->E);
should not be able to return me a value of 10. when it does, it surprises me, which makes me wonder whether the memory has really been freed.
Hello,
Also, be sure to set your variable to NULL after you free the memory. Example: This can ensure a few things. Firstly, it ensures our pointer doesn't point to our freed address. Secondly, it secures your code from crashing if you call free() twice on the same pointer. For instance: Where in fact: Hope this helps.
- Stack Overflow
Also, be sure to set your variable to NULL after you free the memory. Example:
free(G); /* Dynamically allocated memory is freed */ G = NULL; /* This ensures that we point our pointer to NULL */
free(G); /* Dynamically allocated memory is freed */ free(G); /* Will cause a crash. */
free(G); /* Dynamically allocated memory is freed */ G = NULL; /* This ensures that we point our pointer to NULL */ free(G); /* Won't cause a crash because G points to NULL. */
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Other Threads in the C Forum
- Previous Thread: Adding to linked list from external file
- Next Thread: can someone help...please with this prb
Views: 6623 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch bit build c++ c/c++ calling char character code coke command conversion convert copy database decimal directory dude dynamic ebook error exec factorial fflush fgets file floatingpointvalidation fork function functions getline givemetehcodez grade graphics haiku help|help|help|help homework i/o include input insert int integer intmain() keyboard lazy libcurl line linked linkedlist linux list lists loop malloc matrix memory mysql no-effort output overwrite parallel path permutations pointer pointers problem process program programming read readfile recursion recursive recv reverse scanf socketprograming sockets spoonfeeding stdin string strings strtok structures student system testing turbo-c turboc unix user variable win32 windows _getdelim






