943,383 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7237
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 9th, 2005
0

Re: How to free up memory used for queues?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 9th, 2005
0

Re: How to free up memory used for queues?

Great!
Thanks Stack Overflow and Narue!
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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.
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 9th, 2005
0

Re: How to free up memory used for queues?

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!
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Adding to linked list from external file
Next Thread in C Forum Timeline: can someone help...please with this prb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC