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: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #11
Mar 9th, 2005
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);)

  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: How to free up memory used for queues?

 
0
  #12
Mar 9th, 2005
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
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to free up memory used for queues?

 
0
  #13
Mar 9th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #14
Mar 9th, 2005
Great!
Thanks Stack Overflow and Narue!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #15
Mar 9th, 2005
Oh sorry but one more question:

If memory is allocated in the main function (via malloc), say to G, if I pass G into a function, can the memory be freed inside the function via free(G) or do I have to free(G) outside the function?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: How to free up memory used for queues?

 
0
  #16
Mar 9th, 2005
Hello,

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;
}
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
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #17
Mar 9th, 2005
Really thank you for your efforts.
1) could you tell me what it means by "function scope"?
2) I was actually expecting to see the same thing (printout) for "Before call" and "Function scope (before NULL) " in the function test, but it turned out otherwise.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: How to free up memory used for queues?

 
0
  #18
Mar 9th, 2005
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:
#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;
}
In this particular case, and for testing purposes only, this is what my output looked like:
  1. Before call: 0xa0501b8
  2. Function scope: 0xa0501b8
  3. Function scope - After NULL: 0x0
  4. After call: 0x0
As seen, the address of ptr is the same when in the scope of main, and dereferenced in the scope of test().


- 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #19
Mar 9th, 2005
Yes, i got the same printout too (ie. the first two are the same while the last two are zeros), only that the values are different but that's ok since we are using differnet computers.
Guess I got it now. Really appreciate your help.
Have a good day!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC