question, malloc in a function 
Author Message
 question, malloc in a function

Hey all-

If I allocate space to a local variable with malloc() in a function,
and return() from the function without free() -ing the malloc() -ed memory,
does the memory return to the heap as the variable passes out of scope?

For example:

#include<stdio.h>
#include<string.h>

char *substr(char *, int, int);

main(int argc, char *argv[])
{
char thing[]="abcdefg";
printf("\n%s",substr(thing,1,3));

Quote:
}

char *substr(char *source, int start, int length)
{
char *dest;
dest=malloc((length+1)*sizeof(char));
strcpy(dest, "");
strncpy(dest,&source[start-1],length);
dest[length]='\0';
return(dest);

Quote:
}

The above code returns a substring of a given string.  It works, I am just
wondering whether overuse will do bad things.

Thanks for any insights,

respectfully,



Mon, 02 Nov 1998 03:00:00 GMT  
 question, malloc in a function


Quote:

>Hey all-

>If I allocate space to a local variable with malloc() in a function,
>and return() from the function without free() -ing the malloc() -ed memory,
>does the memory return to the heap as the variable passes out of scope?

No.

Quote:
>For example:

>#include<stdio.h>
>#include<string.h>

>char *substr(char *, int, int);

>main(int argc, char *argv[])
>{
>char thing[]="abcdefg";
>printf("\n%s",substr(thing,1,3));
>}

>char *substr(char *source, int start, int length)
>{
>char *dest;
>dest=malloc((length+1)*sizeof(char));
>strcpy(dest, "");
>strncpy(dest,&source[start-1],length);
>dest[length]='\0';
>return(dest);
>}

>The above code returns a substring of a given string.  It works, I am just
>wondering whether overuse will do bad things.

Yes, using it will do bad things (specifically, a memory leak), but
even if the behaviour were as suggested in your first paragraph, it
would still do bad things because you'd be returning a pointer to
some memory which had been returned to the heap.

You could make it the responsibility of the caller to free off the
memory, or use a static buffer in the sub-routine, or an array of
static buffers used cyclically.

John
--
John Winters.  Wallingford, Oxon, England.



Wed, 04 Nov 1998 03:00:00 GMT  
 question, malloc in a function

Quote:

> Hey all-

> If I allocate space to a local variable with malloc() in a function,
> and return() from the function without free() -ing the malloc() -ed memory,
> does the memory return to the heap as the variable passes out of scope?

> For example:

> #include<stdio.h>
> #include<string.h>

> char *substr(char *, int, int);

> main(int argc, char *argv[])
> {
> char thing[]="abcdefg";
> printf("\n%s",substr(thing,1,3));
> }

> char *substr(char *source, int start, int length)
> {
> char *dest;
> dest=malloc((length+1)*sizeof(char));
> strcpy(dest, "");
> strncpy(dest,&source[start-1],length);
> dest[length]='\0';
> return(dest);
> }

> The above code returns a substring of a given string.  It works, I am just
> wondering whether overuse will do bad things.

It will eat memory if the user does not free() the string returned
when done with it.

Memory allocated with mallc() is freed only when you explicitly do so
or at program termination (the standard does not actually guarantee
the latter, but any reasonable operating system will do it for you).

Michael M Rubenstein



Wed, 04 Nov 1998 03:00:00 GMT  
 question, malloc in a function



Quote:
>Hey all-

>If I allocate space to a local variable with malloc() in a function,

Be clear that you are talking about 2 objects/varioables here. Firstly
there is the pointer which may or may not be a local variable, and secondly
there is the object you created with malloc which is *not* a local
variable.

Quote:
>and return() from the function without free() -ing the malloc() -ed memory,
>does the memory return to the heap as the variable passes out of scope?

If you have an automatic (local) pointer that is destroyed when it
passes out of scope. The malloc'd variable itself has no scope since it
has no identifier/name. It has a 'duration' i.e. it lasts until it is
freed. If you destroy the pointer without first freeing the malloc'd
variable it remians allocated but you have no way of getting at it. This is
called a memory leak.

Quote:
>For example:

>#include<stdio.h>
>#include<string.h>

>char *substr(char *, int, int);

>main(int argc, char *argv[])
>{
>char thing[]="abcdefg";
>printf("\n%s",substr(thing,1,3));
>}

>char *substr(char *source, int start, int length)
>{
>char *dest;
>dest=malloc((length+1)*sizeof(char));
>strcpy(dest, "");

This strcpy is redundant. If it was needed it would be better to write
it as:

 dest[0] = '\0';

Quote:
>strncpy(dest,&source[start-1],length);
>dest[length]='\0';

Consider replacing these three lines with:

 dest[0] = '\0';
 strncat(dest,&source[start-1],length);

Quote:
>return(dest);
>}

>The above code returns a substring of a given string.  It works, I am just
>wondering whether overuse will do bad things.

If you call substr repeatedly without freeing the variable that malloc
creates then you will slowly eat through your available memory. You could
in this case free it in the caller after you had used it. However with this
sort of code it is normally better to pass an extra argument of a pointer
to a buffer created in the caller.

--
-----------------------------------------


-----------------------------------------



Wed, 04 Nov 1998 03:00:00 GMT  
 question, malloc in a function

: If I allocate space to a local variable with malloc() in a function,
: and return() from the function without free() -ing the malloc() -ed memory,
: does the memory return to the heap as the variable passes out of scope?

(Assuming the local variable was the only place the mallocked pointer
was stored, when the local goes out of the scope without freeing
the pointer, the block becomes 'garbage.')

In ordinary cases: NO. C does not require the compiler or runtime to
support or provide any kind of garbage collection. And many features
of the language make complete and safe garbage collection impossible.

There are garbage collecting versions of C around, but you would
probably know it if you had one.

--

the Queen who straits, the Queen of strife;|          Cupertino, California
with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
she brings the choice of birth or knife.   |         I don't use no smileys



Wed, 04 Nov 1998 03:00:00 GMT  
 question, malloc in a function


Quote:
> Hey all-

> If I allocate space to a local variable with malloc() in a function,
> and return() from the function without free() -ing the malloc() -ed memory,
> does the memory return to the heap as the variable passes out of scope?

        Sorry, I have no time to check the code: The answer is NO. The
pointer is lost, since it is a local variable, but the heap space
allocated, remains allocated: That's memory leakage.




Mon, 09 Nov 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie question: malloc() function

2. simple malloc question in functions

3. C question : redefining a new malloc() calling standard malloc()

4. to malloc() or not to malloc(), that is the question

5. Dynamic memory malloc'ed in a function.

6. malloc within function

7. malloc wrapper function, is it alright to abort ?

8. Confusion about treating malloc char* as char[] in function

9. ? Pointers, functions and malloc

10. malloc() inside functions

11. Help with function 'malloc'

12. free() after malloc() in a function

 

 
Powered by phpBB® Forum Software