| | |
How to free up memory used for queues?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
Thanks, it does help me understand a little better. So does it mean that even though the second time I call to print (after free(G);)
the program was able to give me 10 means that "10" is still stored at that memory but that memory is now free to be used by something else? so if I had done some other things later on in my program, and the memory storing "10" is used for something else, then I will not be able to retrieve the "10" later?
C Syntax (Toggle Plain Text)
printf("%d\n", G->E);
the program was able to give me 10 means that "10" is still stored at that memory but that memory is now free to be used by something else? so if I had done some other things later on in my program, and the memory storing "10" is used for something else, then I will not be able to retrieve the "10" later?
Hello,
When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller remains unchanged, because C's pass-by-value semantics mean that called functions never permanently change the values of their arguments.
A pointer value which has been freed is, strictly speaking, invalid, and any use of it, even if is not dereferenced can theoretically lead to trouble, though as a quality of implementation issue, most implementations will probably not go out of their way to generate exceptions for innocuous uses of invalid pointers.
References: ANSI Sec. 4.10.3
ISO Sec. 7.10.3
Rationale Sec. 3.2.2.3
- Stack Overflow
When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller remains unchanged, because C's pass-by-value semantics mean that called functions never permanently change the values of their arguments.
A pointer value which has been freed is, strictly speaking, invalid, and any use of it, even if is not dereferenced can theoretically lead to trouble, though as a quality of implementation issue, most implementations will probably not go out of their way to generate exceptions for innocuous uses of invalid pointers.
References: ANSI Sec. 4.10.3
ISO Sec. 7.10.3
Rationale Sec. 3.2.2.3
- 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
>so if I had done some other things later on in my program
Once freed, the memory is no longer yours. That means that in theory, another process could allocate the memory immediately after you're done freeing it and overwrite the values you had stored there before printf is called in your program. So your print isn't guaranteed to work, and on top of that, you've invoked undefined behavior because you don't own the memory you're accessing.
Once freed, the memory is no longer yours. That means that in theory, another process could allocate the memory immediately after you're done freeing it and overwrite the values you had stored there before printf is called in your program. So your print isn't guaranteed to work, and on top of that, you've invoked undefined behavior because you don't own the memory you're accessing.
I'm here to prove you wrong.
Hello,
No problem. Let me answer this question by example: If you run this code, you will see that I passed the variables address to test(). With the address, I can modify and free the memory from within that scope. You will see that the free() and NULL calls were successful by the time we return to main().
- Stack Overflow
No problem. Let me answer this question by example:
#include <stdio.h> #include <stdlib.h> void test(char **p) { /* run test; view memory address */ printf("Function scope: %0x%08x\n", (unsigned int)*p); /* free memory */ free(*p); /* set to NULL */ *p = NULL; printf("Function scope - After NULL: %0x%08x\n", (unsigned int)*p); } int main() { char *ptr = malloc(25); /* allocation may have failed */ if (ptr == NULL) return 0; /* memory address before */ printf("Before call: %0x%08x\n", (unsigned int)ptr); /* send memory address */ test(&ptr); /* run some tests */ printf("After call: %0x%08x\n", (unsigned int)ptr); return 0; }
- 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
Hello,
1) What I mean't by function scope is simple. All it knows is what is happening within the function, and it doesn't have a clue as to what is happening in the 'int main' part of the program. This kind of scope is called 'local scope'. Whatever happens in the 'int main' is only visible to that section of the program. And on the other hand, anything that happens in the function 'test' is only visible to that part of the program, which in both cases is between the opening and closing curly braces.
2) That strikes me as well. Let me re-post my example using the %p type condition instead of the %0x%08x: In this particular case, and for testing purposes only, this is what my output looked like: As seen, the address of ptr is the same when in the scope of main, and dereferenced in the scope of test().
- Stack Overflow
1) What I mean't by function scope is simple. All it knows is what is happening within the function, and it doesn't have a clue as to what is happening in the 'int main' part of the program. This kind of scope is called 'local scope'. Whatever happens in the 'int main' is only visible to that section of the program. And on the other hand, anything that happens in the function 'test' is only visible to that part of the program, which in both cases is between the opening and closing curly braces.
2) That strikes me as well. Let me re-post my example using the %p type condition instead of the %0x%08x:
#include <stdio.h> #include <stdlib.h> void test(char **p) { /* run test; view memory address */ printf("Function scope: %p\n", (void *)*p); /* free memory */ free(*p); /* set to NULL */ *p = NULL; printf("Function scope - After NULL: %p\n", (void *)*p); } int main() { char *ptr = malloc(25); /* allocation may have failed */ if (ptr == NULL) return 0; printf("Before call: %p\n", (void *)ptr); /* send memory address */ test(&ptr); /* run some tests */ printf("After call: %p\n", (void *)ptr); return 0; }
C Syntax (Toggle Plain Text)
Before call: 0xa0501b8 Function scope: 0xa0501b8 Function scope - After NULL: 0x0 After call: 0x0
- 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
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






