simple malloc question in functions 
Author Message
 simple malloc question in functions

hello,
I have a question regarding malloc. I was under the assumption that
variables declared in functions were local to that function and is freed
when the function is exited. I was writing a program which declared a
variable in a function and allocated space using malloc(). After about
letting the program run for a few minutes, i discovered that the program
started swapping to the hard drive for virtual memory (I use djgpp). The
problem was fixed when I called free() before exiting the function. Does
the memory still persist when allocated with malloc() (or similar
dynamically allocating function) even when it exits the function? Thank
you for any help.



Thu, 09 Sep 1999 03:00:00 GMT  
 simple malloc question in functions

Quote:

>hello,
>I have a question regarding malloc. I was under the assumption that
>variables declared in functions were local to that function and is freed
>when the function is exited.

Automatic variables are created when the *block* they are defined in is
entered and they are destroyed when that block is exited. For example:

void foo(int x)   /* Function parameters are created on entry to the function
                     and destroyend when it exits */
{
    static int t; /* static variables always exist throughout the lifetime
                     of the program */

    int y;        /* An automatic variable at the outermost block level in
                     a function in effect is created when then function is
                     entered and destroyed when the function returns */

    {
        int z;    /* An automatic variable in an inner block is created
                     when the *block* is entered and destroyed when the
                     block is left (execution falls out the bottom, the
                     block is jumped out of, or a return is executed within
                     the block.
    }

Quote:
}
>I was writing a program which declared a
>variable in a function and allocated space using malloc(). After about
>letting the program run for a few minutes, i discovered that the program
>started swapping to the hard drive for virtual memory (I use djgpp). The
>problem was fixed when I called free() before exiting the function. Does
>the memory still persist when allocated with malloc() (or similar
>dynamically allocating function) even when it exits the function? Thank
>you for any help.

malloc'd memory persists until you free it. For example in:

void *bar(void)
{
    void *p =- malloc(100);

    return p;

Quote:
}

p itself is a pointer variable, it is automatic and is destroyed when the
function returns. However what it *points to* is an object allocated by
malloc() and that is not automatic. It remains in existence until it is
freed. The pointer value returned by bar() can be used by the caller to
reference the malloc'd object.

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


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



Thu, 09 Sep 1999 03:00:00 GMT  
 simple malloc question in functions

[...]
: malloc'd memory persists until you free it. For example in:

: void *bar(void)
: {
:     void *p =- malloc(100);

:     return p;
: }
[...]

This typo is corrected only because it's part of code.  There shouldn't
be a '-' after the '=' before malloc().



Thu, 09 Sep 1999 03:00:00 GMT  
 simple malloc question in functions


Quote:
>I have a question regarding malloc. I was under the assumption that
>variables declared in functions were local to that function and is freed
>when the function is exited. I was writing a program which declared a
>variable in a function and allocated space using malloc(). After about
>letting the program run for a few minutes, i discovered that the program
>started swapping to the hard drive for virtual memory (I use djgpp). The
>problem was fixed when I called free() before exiting the function. Does
>the memory still persist when allocated with malloc() (or similar
>dynamically allocating function) even when it exits the function? Thank
>you for any help.

Dynamically allocated memory is not automatically freed when you
exit the function unless the function is main() and your program
exits. The memory will presist throughtout the program life unless
you explicitly free it. It is good discipline, to free all allocated
storage as soon as possible. Freed storage can be reallocated,
making better use of a limited resource.
Al Bowers                                
Tampa, FL

http://www.gate.net/~abowers/index.html


Thu, 09 Sep 1999 03:00:00 GMT  
 simple malloc question in functions

# hello,
# I have a question regarding malloc. I was under the assumption that
# variables declared in functions were local to that function and is freed
# when the function is exited. I was writing a program which declared a
# variable in a function and allocated space using malloc(). After about
# letting the program run for a few minutes, i discovered that the program
# started swapping to the hard drive for virtual memory (I use djgpp). The
# problem was fixed when I called free() before exiting the function. Does
# the memory still persist when allocated with malloc() (or similar
# dynamically allocating function) even when it exits the function? Thank
# you for any help.

Yes, it persists. Every object in C has a storage class. Automatic
variable have storage class "automatic", which means their existence
begins with the end of their declaration and ends on the closing brace
of their respective compound statement.

Objects in malloc()ed, calloc()ed or realloc()ed memory have storage
class "allocated", which means their existence begins with a successful
return from one of these functions and ends when free() or realloc()
is called for the respective pointer (and of course, upon program
termination.)

The Standard is 20km away and I'm writing this from memory.
More knowlegeable language lawyers please correct me.

        Jens
--
Jens Schweikhardt  http://www.uni-stuttgart.de/People/schweikhardt/home.html
SIGSIG -- signature too long (core dumped)



Fri, 10 Sep 1999 03:00:00 GMT  
 simple malloc question in functions

Quote:

> hello,
> I have a question regarding malloc. I was under the assumption that
> variables declared in functions were local to that function and is freed
> when the function is exited. I was writing a program which declared a
> variable in a function and allocated space using malloc().

This does *NOT* apply to "malloc()". You are responsible for releasing
the
memory allocated by "malloc()" yourself by calling "free()".
Only the local variables disappear when leaving a function.

Quote:
> After about
> letting the program run for a few minutes, i discovered that the program
> started swapping to the hard drive for virtual memory (I use djgpp). The
> problem was fixed when I called free() before exiting the function. Does
> the memory still persist when allocated with malloc() (or similar
> dynamically allocating function) even when it exits the function?

See above: the memory alloocated by "malloc()" is not freed
automatically
at runtime, only at program termination is the total amount of allocated
memory released and returned to the system.

If you want to know more about dynamic memory, please read the
"Section 7. Memory Allocation" in the c.l.c FAQ.

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.htm or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup regularly.

Stephan



Sat, 11 Sep 1999 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. a very simple malloc question

2. simple question about malloc & free

3. SIMPLE malloc & pointer question

4. Newbie question: malloc() function

5. question, malloc in a function

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

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

8. Simple question about function return

9. Simple question: Member function templates in template classes under VC++ 5.0

10. Simple question about Pointers to Functions

11. Function Timing Simple Question

12. a simple simple question...

 

 
Powered by phpBB® Forum Software