neon said:
does by calling free() from the caller, deallocate the heap used by
variable newString in function StringCopy? if not, how wil I deallocate
the heap used by the StringCopy?
There's no requirement that the memory for allocated memory is kept in a
heap structure, but yes, calling free() on a pointer to block of memory
you got from malloc() frees that memory, regardless of the scope of the
variable you first assigned that pointer to. Well, as long as you
haven't realloc()'ed it in the mean time, in which case you need to use
the new value; or have already free()d it: you shouldn't do it twice.
But that's not what's going on here. You're passing the right thing to
malloc(); but not to free(). Let's rearrange your code a bit, so it's
easier to see what's going on:
char* StringCopy(const char* string)
[ First, minor, bug: "string" is an identifier that's reserved for use
by the implementation. All identifiers starting with str and a lower-
case letter are said:
{
char *newString;
newString = malloc(sizeof(char)*len);
[ Second bug: _what_ len? strlen(string) is wrong, because you want one
more byte to hold the terminating '\0'. Oh, and sizeof(char) is
guaranteed to be 1.]
[ Third bug, relevant to serious programs, not to tests: always check
that malloc() didn't return a null pointer. ]
strcpy(newString, string);
return(newString);
}
Ok, so up to here, apart from that length, it's OK. You're taking a
string; allocating memory for a copy of it; copying the string into the
new memory; and passing the pointer out. Correctly. If the calling
function stores the return value of StringCopy() somewhere, and later
calls free() on that value, that is a correct use of malloc() and
free(). (Note also that StringCopy() does _nothing_ to either the value
or the contents of string.) However...
int main(void){
char string[80];
[ I'm assuming you put some content into string, here. ]
[ I'm also assuming that you actually meant StringCopy - copy&paste,
don't retype, your code; typos are easy to make. ]
....you are _not_ assigning the return value of StringCopy() to anything.
Therefore, that pointer value is now lost; and since it was the only
pointer to your block of allocated memory, so is that block. (Note that,
as above, string itself has not been touched.)
Since string is an array in main(), and not a pointer to a block of
allocated memory, you can't pass it to free().
Here's a corrected version of your code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char* StringCopy(const char* str)
{
char *newString;
newString = malloc(strlen(str)+1);
if (newString)
strcpy(newString, string);
return(newString); /* Yes, even when it's null. */
}
int main(void)
{
char oldstr[80]="This is a test.";
char *newstr;
newstr=StringCopy(string);
if (newstr) /* This is why we could return a null pointer from
StringCopy(): we're checking it here. */
printf("Lo and behold: %s; %s!\n", oldstr, newstr);
else
puts("StringCopy() could not get the memory it needed.");
free(newstr); /* This is legal even when newstr==NULL. */
/* You can't use newstr after this, until you give it a new value.
oldstr, OTOH, is still fine. */
return 0;
}
Richard