|
-
Mar 16th, 2008, 11:35 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] C: Not sure about use of free()
In my C program, I've allocated memory using this:
C Code:
printf("Sorting.\nSort how many numbers? ");
scanf("%ld", &n); //get input for number of numbers
int *pIntArray; //declare pointer, which will point to first element
pIntArray = (int *) malloc (sizeof(int) * n); //allocate enough memory for 'n' ints, and point pIntArray at the first one
I know I'm meant to free the memory once I'm done with it, but when I call free(), do I only need to call it once, using pIntArray as the argument? Or do I need to loop through pIntArray to pIntArray + n - 1 and free every one of my 'elements'? Oh and after free()ing, should I set pIntArray to NULL?
On a side note, if I don't free() the memory, creating a memory leak, when is the memory eventually returned to the pool? When the program ends? When the computer is shut down? Some other time?
cheers,
metal
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|